简体   繁体   English

python在docker容器中创建目录并写入文件

[英]python creating directories and writing files inside docker container

I am writing a docker file of an existing python script. 我正在编写现有python脚本的docker文件。

My docker file : 我的码头工人文件:

FROM python:2.7.11
ADD ./ test_project/
RUN mkdir /test_project/download/
RUN chmod -R 777 /test_project/download
WORKDIR test_project
RUN ls -ltr
CMD [ "python", "report/test.py"]`

Part of my python script test.py that creates the directory and subdirectories 我的python脚本test.py的一部分,用于创建目录和子目录

os.makedirs(download_directory)

Here "download_directory" = /test_project/download/reports/2017-01-24/ 此处“下载目录” = / test_project / download / reports / 2017-01-24 /

When running outside of docker on my local, it creates the directory on my local file system. 在本地上的docker外运行时,它将在本地文件系统上创建目录。
However, when running inside the docker, it is not creating the directory in docker container. 但是,在docker内部运行时,它不会在docker容器中创建目录。 I looked at the directory in the docker image and it only has the directory that I created from Dockerfile but not the sub directories which should be created from python script. 我查看了docker镜像中的目录,它仅包含我从Dockerfile创建的目录,而没有应从python脚本创建的子目录。
Any idea on what I might be missing? 对我可能会丢失的东西有任何想法吗?

---Update--- ---更新---
Another thing I found: 我发现的另一件事:

  1. Build the docker image using: "docker build -t test_project". 使用以下命令构建docker映像:“ docker build -t test_project”。
  2. Run it using "docker run test_project" 使用“ docker run test_project”运行它

When I do this and after the run go inside the image bash by: 当我这样做时,在运行之后,通过以下方式进入图像重击:

docker run --rm -it test_project bash

I do not see my directories created by python script. 我没有看到由python脚本创建的目录。 However, after building the project I first go inside the image bash by using the same command as above and then execute the script using: 但是,在构建项目之后,我首先使用与上述相同的命令进入图像bash,然后使用以下命令执行脚本:

python test_project/test.py

Directories are created. 目录已创建。

Is it because after every run container is getting destroyed and I can't see the files/directories being written? 是因为在每个运行容器被销毁之后,我看不到正在写入的文件/目录吗?

Don't use docker run , use docker exec . 不要使用docker run ,请使用docker exec

docker exec runs a command in an already-running container. docker exec在已运行的容器中运行命令。
docker run creates a new container every time you run it. 每次docker run都会创建一个新容器。

Here, you created two containers. 在这里,您创建了两个容器。 Also, when you started "bash" on the second docker run call ( docker run --rm -it test_project bash ), you overrode your dockerfile's CMD so your script didn't run on that container when it was created. 另外,当您在第二个docker run --rm -it test_project bash docker run调用( docker run --rm -it test_project bash )上启动“ bash”时,您将覆盖dockerfile的CMD,因此脚本在创建时不会在该容器上运行。 This is why you didn't see the files you expected (in the second container) until you manually ran your python script. 这就是为什么直到您手动运行python脚本后才看到所需文件的原因(在第二个容器中)。

If you are trying to log into the container you created with docker run test_project , then you need to do this: 如果您尝试登录使用docker run test_project创建的容器,请docker run test_project ,那么您需要这样做:

  1. Find the (probably random) name of the specific container you just started, running the image "test_project": 找到刚刚启动的特定容器的名称(可能是随机的),运行映像“ test_project”:

$ docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f357de526da7 test_project "python report/test.py" 5 minutes ago Up 5 minutes imaginary_curie

In this case, we see the name of this container is 'imaginary_curie' 在这种情况下,我们看到此容器的名称为'imaginary_curie'

  1. Now open a shell on that container: docker exec -it imaginary_curie bash 现在在该容器上打开一个外壳: docker exec -it imaginary_curie bash

See also: 也可以看看:

docker run --help

and

docker exec --help

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM