Docker run doesn't save file created by script

Viewed 420

I have following Dockerfile

FROM python:3.6
MAINTAINER "Alexey Nikitin"


RUN pip install --upgrade pip
RUN pip install tensorflow
RUN pip install numpy pandas 
RUN pip install keras --no-deps

RUN ["mkdir", "/tmp/data"]
RUN ["mkdir", "/var/log/result"]
RUN ["mkdir", "/model"]


ADD ./data /tmp/data
ADD ./model /model

WORKDIR /model
CMD ["python", "predict.py"]

The result of predict.py is text file saved in the same directory. After running docker run -it toxic I see file executing. But when I enter container, I don't see created file.

enter image description here

How can I run container and save file created by script?

2 Answers

From what i understood, your script runs some logic and then finishes. As soon as your script is finished, Docker container exits and all data that was created is deleted. To persist this data you need to use volumes, for example:

docker run -it -v $(pwd):/model toxic

In that case, generated file will be stored in your current directory.

From man docker-run:

DESCRIPTION Run a process in a new container. docker run starts a process with its own file system, its own networking, and its own isolated process tree.

You first docker run invocation creates the file in the filesystem of a container. The second invocation runs as a different container, with its own filesystem. In other words, files in the filesystem of a container are not to be considered persistent.

Volumes are the way you achieve persistency.

In your case, I would use:

docker run -v <path>:/model -it toxic

Now even if the containers are gone, the content of /model inside the container is mapped to <path> in your host machine.

There are more ways in which you can use volumes, please refer to https://docs.docker.com/storage/volumes/

Not related, but just to add: I almost always prefer to add --rm to docker run. It removes the container when run has finished. Otherwise you end up with many ended containers that you need to remove manually at some point.

Related