简体   繁体   中英

python creating directories and writing files inside docker container

I am writing a docker file of an existing python script.

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

os.makedirs(download_directory)

Here "download_directory" = /test_project/download/reports/2017-01-24/

When running outside of docker on my local, it creates the directory on my local file system.
However, when running inside the docker, it is not creating the directory in docker container. 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.
Any idea on what I might be missing?

---Update---
Another thing I found:

  1. Build the docker image using: "docker build -t test_project".
  2. Run it using "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. 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:

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 exec runs a command in an already-running container.
docker run creates a new container every time you run it.

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. This is why you didn't see the files you expected (in the second container) until you manually ran your python script.

If you are trying to log into the container you created with docker run test_project , then you need to do this:

  1. Find the (probably random) name of the specific container you just started, running the image "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'

  1. Now open a shell on that container: docker exec -it imaginary_curie bash

See also:

docker run --help

and

docker exec --help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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