Alberto Bonsanto Alberto Bonsanto -3 years ago 165
Python Question

No such file or directory while running docker

I have a python file
that simply reads
environment variable and sets uses it to create the target's logs directory.

logs_path = os.getenv("LOGS_PATH", "/logs/proxy.log")
fileHandler = logging.FileHandler(logs_path)

My docker file has the following, notice that I create the logs directory and the

CMD mkdir /logs
CMD touch /logs/proxy.log

CMD python

Then I build the docker image and run it, as follows.

docker build -t rank
docker run --rm -it --net=host -p 8000:8000 --env-file=.env-co --name=rank rank

But when I run it, it blows up. It says
IOError: [Errno 2] No such file or directory: '/logs/proxy.log'

Answer Source

CMD and RUN are different things. Use as this:

RUN mkdir /logs
RUN touch /logs/proxy.log

CMD python

CMD is the command that represents the container when running, an image can have only one CMD (the last one is the only valid). RUN is excuted when you do docker build.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download