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


currently have dockerfile build image entrypoint allows me run command in centos based container using docker run command. after command run, docker container stopped , removed.

however, command run generate log files inside docker container , these lost once container exits. best practice preserving these files (preferably can stored on host machine)?

this may solution: create named docker volume:

docker volume create --name centos-volume 

so path volume on host /var/lib/docker/volumes/centos-volume/_data

than created comparable app. dockerfile (copies script create log in container. start script when start container docker run (like did)):

from centos:7 run mkdir /script copy create-log.sh /script/ run chmod +x /script/create-log.sh 

content of script: (it creates 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 container with:

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

so commands execute script in container , mounts content of /var/log/anaconda folder of container volume on host.

than check:

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

after rerunning container 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 

Comments