未使用 docker 卷将容器内容复制到 docker 主机
Container content is not being copied to docker host using docker volume
我正在生成 output.txt 文件的容器内 运行ning python 脚本。我想 运行 这个 python 脚本只在容器中运行一次,并且 output.txt 应该在我的 Docker 主机中可用,但是 运行 低于 docker卷命令文件未被复制。
我的Docker文件
[root@server test]# cat Dockerfile
FROM centos
RUN yum install -y https://centos7.iuscommunity.org/ius-release.rpm
RUN yum install -y python36u python36u-libs python36u-devel python36u-pip
RUN ln -sf /usr/bin/python3.6 /usr/bin/python
RUN mkdir /app
COPY 16-reading_and_writing_file.py /app
RUN python --version
CMD ["python", "/app/16-reading_and_writing_file.py"]
我的python脚本
target3 = open("output.txt",'w')
line1 = "Hello"
line2 = "How Are You"
target3.write(line1)
target3.write("\n")
target3.write(line2)
target3.write("\n")
target3.close()
print ("Hello")
docker 运行 命令
[root@server test]# docker run -it -v /jaydeep/docker_practice/test/:/app jaydeepuniverse/jira
Hello
[root@server test]#
我需要在 docker 命令
中给出的卷路径中有 output.txt
[root@server test]# pwd
/jaydeep/docker_practice/test
[root@server test]# ls -ltrh
total 8.0K
-rwxr-xr-x 1 root root 183 May 17 08:25 16-reading_and_writing_file.py
-rw-r--r-- 1 root root 510 May 17 23:35 Dockerfile
[root@server test]#
请指教
谢谢
当你是运行CMD ["python", "/app/16-reading_and_writing_file.py"]
时,你当前的工作目录是/
.
因此 output.txt
文件会在 /
下创建,而不是在 /app
下创建
所以最好在你的Dockerfile
中使用WORKDIR
来提及你的工作目录
FROM centos
RUN yum install -y https://centos7.iuscommunity.org/ius-release.rpm
RUN yum install -y python36u python36u-libs python36u-devel python36u-pip
RUN ln -sf /usr/bin/python3.6 /usr/bin/python
RUN mkdir /app
WORKDIR /app
COPY 16-reading_and_writing_file.py .
RUN python --version
CMD ["python", "16-reading_and_writing_file.py"]
现在将在 /app
下创建文件
或
在你的python代码中,你可以使用os模块来形成路径
import os
output_file_path = os.path.join(os.path.abspath(__file__), 'output.txt')
target3 = open(output_file_path,'w')
line1 = "Hello"
line2 = "How Are You"
target3.write(line1)
target3.write("\n")
target3.write(line2)
target3.write("\n")
target3.close()
print ("Hello")
这将帮助您在 16-reading_and_writing_file.py 所在的同一目录中创建 output.txt
,无论您身在何处。
我正在生成 output.txt 文件的容器内 运行ning python 脚本。我想 运行 这个 python 脚本只在容器中运行一次,并且 output.txt 应该在我的 Docker 主机中可用,但是 运行 低于 docker卷命令文件未被复制。
我的Docker文件
[root@server test]# cat Dockerfile
FROM centos
RUN yum install -y https://centos7.iuscommunity.org/ius-release.rpm
RUN yum install -y python36u python36u-libs python36u-devel python36u-pip
RUN ln -sf /usr/bin/python3.6 /usr/bin/python
RUN mkdir /app
COPY 16-reading_and_writing_file.py /app
RUN python --version
CMD ["python", "/app/16-reading_and_writing_file.py"]
我的python脚本
target3 = open("output.txt",'w')
line1 = "Hello"
line2 = "How Are You"
target3.write(line1)
target3.write("\n")
target3.write(line2)
target3.write("\n")
target3.close()
print ("Hello")
docker 运行 命令
[root@server test]# docker run -it -v /jaydeep/docker_practice/test/:/app jaydeepuniverse/jira
Hello
[root@server test]#
我需要在 docker 命令
中给出的卷路径中有 output.txt[root@server test]# pwd
/jaydeep/docker_practice/test
[root@server test]# ls -ltrh
total 8.0K
-rwxr-xr-x 1 root root 183 May 17 08:25 16-reading_and_writing_file.py
-rw-r--r-- 1 root root 510 May 17 23:35 Dockerfile
[root@server test]#
请指教
谢谢
当你是运行CMD ["python", "/app/16-reading_and_writing_file.py"]
时,你当前的工作目录是/
.
因此 output.txt
文件会在 /
下创建,而不是在 /app
所以最好在你的Dockerfile
中使用WORKDIR
来提及你的工作目录
FROM centos
RUN yum install -y https://centos7.iuscommunity.org/ius-release.rpm
RUN yum install -y python36u python36u-libs python36u-devel python36u-pip
RUN ln -sf /usr/bin/python3.6 /usr/bin/python
RUN mkdir /app
WORKDIR /app
COPY 16-reading_and_writing_file.py .
RUN python --version
CMD ["python", "16-reading_and_writing_file.py"]
现在将在 /app
或
在你的python代码中,你可以使用os模块来形成路径
import os
output_file_path = os.path.join(os.path.abspath(__file__), 'output.txt')
target3 = open(output_file_path,'w')
line1 = "Hello"
line2 = "How Are You"
target3.write(line1)
target3.write("\n")
target3.write(line2)
target3.write("\n")
target3.close()
print ("Hello")
这将帮助您在 16-reading_and_writing_file.py 所在的同一目录中创建 output.txt
,无论您身在何处。