2

My Dockerfile looks like follows;

FROM python:2.7 COPY . /temp WORKDIR /temp CMD ["python", "app.py", "100"] 

My Python file looks like this,

import sys print "This is the name of the script: ", sys.argv[0] print "Number of arguments: ", len(sys.argv) print "The arguments are: " , str(sys.argv) file = open("log.log", "a") x = int(sys.argv[1]) print "received value of x is: ", x if x < 100: file.write('entered value is less than 100: Exiting with exit code 0; SUCEESS') elif x == 100: file.write('entered value is equal to 100: Exiting with exit code 1; SUCEESS') else: file.write('entered value is more than 100: Exiting with exit code 2; FAILED') 

The way I build this container is as follows: docker build python-container .

The way I run the container is as follows: docker run -v ~/logs/:app python-container

What I want is to mount the container directory in which log in generated to my host so that the log file generated by the python is available on my host computer.

But unfortunately, this is not happening,

Debug done by me:

  1. I initially used the python in ENTRYPOINT but it seems the commands in ENTRYPOINT are compressed and bundled in the container and then the container is mounted and thus it failed.

  2. I kept the container running forever by using the ENTRYPOINT ["tail", "-f", "/dev/null"] but still it failed.

  3. I also used docket volumes, created volumes, mounted them and then run the containers and still the log is not available outside.

My question here is what am I missing?

Ultimately, my aim is to have different containers running different python jobs and generating different log files in a specific directory on my host...

I would really appreciate any help in this matter...

1 Answer 1

3

COPY command isn't what you are looking for, it just copies the directory into the container.

What you are looking for is to mount a volume.

https://docs.docker.com/storage/volumes/

Edit for comment below :

Just try this :

On host :

mkdir /tmp/stack_demo touch /tmp/stack_demo/log.txt docker run -ti -v /tmp/stack_demo:/tmp/stack_demo alpine:latest /bin/sh

In container : echo "log from inside container" > /tmp/stack_demo/log.txt

Quit container using ctrl + D, then on host :

cat /tmp/stack_demo/log.txt => log from inside container

Sign up to request clarification or add additional context in comments.

2 Comments

COPY is used just to get my python script inside the container, the log.log is then generated by that script which I want on the host container. I have used volumes but then the files are not actually shared with the host right? I have docker ssh default and then access the logs which I do not want. I want the files to be available on the HOST machine itself and not on a VM on the host machine...
the problem is how you mount the volume : -v ~/logs/:app this basically share /logs on host to /temp/app in container. Since you are writing to /temp/log.log you don't see any modification

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.