Writing to a file inside a container

Viewed 562

I have a Python script which runs a query and prints the output to a text file. When I Dockerize it the output file is not getting created. The Dockerfile is as follows:

FROM python:3
ADD cert_lamp.py /
ADD certificates.txt /
RUN pip install pyOpenSSL
RUN pip install numpy
RUN pip install cryptography
RUN pip install idna
CMD [ "python", "./cert_lamp.py" ]

cert.txt is the file where I need the output to be added. This is referenced in the script. How do I add this to the Dockerfile?

1 Answers

you need to use VOLUME to map your docker files to your host files.

FROM python:3
ADD cert_lamp.py /
ADD certificates.txt /
RUN pip install pyOpenSSL
RUN pip install numpy
RUN pip install cryptography
RUN pip install idna
VOLUME /path/to/cert.txt
CMD [ "python", "./cert_lamp.py" ]

and when running your container:

docker run -v hostdirecotry:dockerdirectory my_image_name

P.S. docker image is created layer by layer, it's not really a good practice to write so many lines for a dockerfile. COPY your whole project directory instead of ADD-ing them one by one. and use a requirement.txt file to install the pip packages all at once.

Related