How to see the file created in a docker container?

2019-07-25 19:03发布

问题:

I have a python script which generates outputs in a text file.I have built a docker image to run the script.While running the image its completing successfully but I cant see the output text file.

My Docker file is

From python:2.7
LABEL maintainer="ABC"
LABEL description="This Dockerfile creates MTA image"
WORKDIR /Temp
ADD script_v1.py connect.txt /Temp/
RUN pip install pexpect
CMD [ "python", "/Temp/script_v1.py" ]

In script_v1 script i have mentioned the /Temp location to generate he output

    sys.stdout=open("/Temp/EXPORT_OP.txt","w")

I am running the image from windows docker host in Linux container mode.

回答1:

I see at least two simple ways of achieving what you want:

  1. Use a bind mount. In short, you'll make a directory in your host filesystem available to the container, and you'll use its path to save the file. The output will be available in that folder.

  2. Use docker cp after the container exits to copy the file from a location inside the container into your host filesystem.

Another way, which will not make the file available directly on your host filesystem, will be to docker exec -it <container> bash, which will leave you with an open terminal inside the container, which you can use to cd into the correct directory and cat the file.



回答2:

As Yury as suggested, another way is to try using volumes. Start your container with a volume mapping a folder on the host to a folder in the container as shown below

docker run -d --name container_name -v /path/on/your/host:/path/in/your/container

This way, any changes in those two paths will be available to both the host and the container. You can also alter your container's start command CMD as shown below.

CMD python /Temp/script_v1.py && tail -f /path/to/your/file

This will allow you to see changes done to the file by using the docker logs command as shown below

docker logs -f container_name

I hope you find this helpful



标签: docker