Alberto Bonsanto Alberto Bonsanto - 1 year ago 68
Python Question

No such file or directory while running docker

I have a python file

server.py
that simply reads
LOGS_PATH
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
proxy.log
file.

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

CMD python server.py


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:

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

CMD python server.py

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