How do I 'docker copy' a file from container to host?

Docker to host file copy command

For those just looking for the command to use to copy a file from inside a Docker container to the local file system on the host machine, here it is:

docker file copy to host

sudo docker cp container-id:/path/filename.txt ~/Desktop/filename.txt

Docker file copy command

This Docker copy (cp) command will take a file that exists inside a Docker container and save it to your computer’s local file system.

The container-name specified in the example command can be either the name given to the Docker container when it was started, or it can be the unique ID assigned to the container by Docker.

After the container name or ID is specified, simply provide the full path to the file to be copied, and then provide the name of the destination folder on the host system.

By default, the file inside Docker will be saved to the host machine with the same name, but you have the option to rename the file as it is copied.

sudo docker cp container-id:/path/filename.txt ~/Desktop/filename.txt

How to Docker copy files to host

To summarize, follow these steps to copy a file from a Docker container to a host machine:

  1. Obtain the name or id of the Docker container
  2. Issue the docker cp command and reference the container name or id
  3. The first parameter of the docker copy command is the path to the file inside the container
  4. The second parameter of the docker copy command is the location to save the file on the host
  5. Edit and use the file copied from inside the container to your host machine

Docker cp command example

Copying a file from inside a Docker container to the host is a great way to create a custom Docker image.

For example, you could run the official Nginx Docker image and then copy the Nginx configuration file to the local filesystem.

Then you could edit the config file to setup an Nginx reverse proxy or install PHP support.

A Dockerfile that copies the edited config file back into your Nginx load balancer image to override the default behavior could easily be created, and a custom Docker image could be created from that.

This would result in a customized Docker image for your environment that is built upon the best practices and experience of the experts who originally developed the Nginx Docker image.

SearchAppArchitecture
SearchSoftwareQuality
SearchCloudComputing
SearchSecurity
SearchAWS
Close