You can use docker container cp command to copy files and directories from docker container to your host.
Note: The same command can be used to copy files from your host to docker container or from one docker container to another docker container.
Syntax:
1 2 3 4 5 6 7 8 |
## Copy from host to docker container docker container cp /some/path/on/host ## Copy from docker container to host docker container cp ## Copy from one docker container to another docker container cp |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
## Create a Dockerfile cat << 'EOF' > Dockerfile FROM ubuntu:latest RUN mkdir -p /root/mydir RUN echo 'hello world' > /root/mydir/myfile.txt EOF ## Create an image from the Dockerfile docker image build -t myimage . ## Run a new docker container docker container run --detach --name mycontainer myimage ## Copy the file from docker container to host. docker container cp mycontainer:/root/mydir/myfile.txt . cat myfile.txt ## Clean up docker container stop mycontainer docker container prune -f |