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 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 takes a file that exists inside a Docker container and saves 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 the unique ID that Docker assigned to the container.

After you specify the container name or ID, simply provide the full path to the file you want to copy, 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.

You could easily create a Dockerfile that copies the edited config file back into your Nginx load balancer image to override the default behavior. From that, you could create a custom Docker image.

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.

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close