Copy log files from Docker container to host after CMD is run

Currently I have a Dockerfile that will build an image with an ENTRYPOINT that allows me to run a command in a CentOS based container using the docker run command. After this command is run, the Docker container is stopped and removed.

However, the command that is run will generate some log files inside the Docker container and these will immediately be lost once the container exits. What is the best practice for preserving these files (preferably so they can be stored on my host machine)?


This may be a solution: Create a named docker volume:

docker volume create --name centos-volume

So the path to his volume on your host is /var/lib/docker/volumes/centos-volume/_data

Than I created something which is hopefully comparable with your app. My dockerfile (copies a script which will create a log in my container. I start the script when I start the container with docker run (like you did)):

FROM centos:7
RUN mkdir /script
COPY create-log.sh /script/
RUN chmod +x /script/create-log.sh

Content of the script: (it creates a logfile in /var/log/anaconda )

#!/bin/bash
touch /var/log/anaconda/my-log.log
echo "hello world" >> /var/log/anaconda/my-log.log

I start my container with:

docker run --rm -ti -v centos-volume:/var/log/anaconda/ my-centos ./script/create-log.sh

So this commands execute the script in my container and mounts the content of my /var/log/anaconda folder of my container to my volume on my host.

Than I check:

cat /var/lib/docker/volumes/centos-volume/_data/my-log.log
hello world

After rerunning the container the second log appears:

docker run --rm -ti -v centos-volume:/var/log/anaconda/ my-centos ./script/create-log.sh

$ sudo su
[root@localhost centos]# cat /var/lib/docker/volumes/centos-volume/_data/my-log.log
hello world
hello world
链接地址: http://www.djcxy.com/p/18270.html

上一篇: 如何将传输缓存保存到泊坞窗的其他主机

下一篇: CMD运行后,将日志文件从Docker容器复制到主机