Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未使用docker卷将容器内容复制到docker主机_Docker_Docker Volume - Fatal编程技术网

未使用docker卷将容器内容复制到docker主机

未使用docker卷将容器内容复制到docker主机,docker,docker-volume,Docker,Docker Volume,我正在容器中运行python脚本,该容器正在生成output.txt文件。我只想在容器中运行这个python脚本一次,output.txt应该在我的Docker主机中可用,但不会复制在Docker卷命令文件下运行的脚本 我的Dockerfile [root@server test]# cat Dockerfile FROM centos RUN yum install -y https://centos7.iuscommunity.org/ius-release.rpm RUN yum inst

我正在容器中运行python脚本,该容器正在生成output.txt文件。我只想在容器中运行这个python脚本一次,output.txt应该在我的Docker主机中可用,但不会复制在Docker卷命令文件下运行的脚本

我的Dockerfile

[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]#
我需要有output.txt在这里的docker卷路径在命令中给出

[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_和_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\u和\u-writing\u file.py的同一目录中创建
output.txt
,无论您在何处。

当您运行
CMD[“python”、“/app/16-reading\u和\u writing\u file.py”
时,您当前的工作目录是
//code>

因此,必须在
/
下创建
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")

这将帮助您在同一目录中创建
output.txt
,无论您身在何处,16-reading\u和\u-writing\u file.py都存在于该目录中。

非常感谢,实际上,我最初包含了workdir,但删除了它,然后考虑将python命令作为/app/scriptName.py运行,并认为output.txt只会在那里创建。再次感谢。非常感谢,实际上我最初包含了workdir,但删除了,然后考虑将python命令作为/app/scriptName.py运行,并认为output.txt只会在那里创建。再次感谢。