Dockerfile contains a Python script that writes to an output file, but the output file is not created on the container


It's my first question, so be gentle :)

I've got a Dockerfile as follows:

FROM centos:latest
Maintainer Liz Miller
LABEL description="Image Built with Dockerfile."
RUN yum -y update
RUN yum -y install python-setuptools
RUN easy_install supervisor
RUN mkdir -p /var/log/supervisor
RUN yum -y install which
RUN yum -y install git
RUN yum install python
CMD ["python", "/"]

And the python script is:

text_file = open('output.txt', 'w')
text_file.write('Hello World')

When I build the image from this Dockerfile, the image is being built with the script and runs it, I don't see the output.txt. It's just not being created. What is missing? How do I solve this?

  1. Change your text_file to /log/output.txt

  2. Build image doesn't run CMD.

  3. After your build the image, you run your container, do

    docker run -it --rm -v $(pwd)/log:/log imagename

Your script write inside the container and exit right the way. So you won't see it in your current directory. The way to solve this is mount a host directory into the container, and let the script write inside it, so that the data can be persistent.