Docker command: cp

Learn how to use the Docker cp command to copy files between containers and the local filesystem, making it easy to share and synchronize resources.

Docker command: cp
Docker command: cp

Introduction

The Docker command cp is used to copy files or folders between a Docker container and the local filesystem. It provides a convenient way to transfer data between the container and the host machine, making it easy to share files and synchronize resources.

Copying Files to a Container

To copy files or folders from the local filesystem to a running container, use the following command:

docker cp <local_path> <container_name>:/<container_path>

Let's break down the command:

  • <local_path>: Specifies the path of the file or folder on the local filesystem.
  • <container_name>: Specifies the name or ID of the container you want to copy files to.
  • <container_path>: Specifies the destination path inside the container where the files will be copied.

For example, let's say you have a file named app.js located in the /var/www directory on your local filesystem, and you want to copy it to a running container named my-container inside the /usr/src/app directory. You would use the following command:

docker cp /var/www/app.js my-container:/usr/src/app/

This command will copy the app.js file to the /usr/src/app directory inside the my-container container.

Copying Files from a Container

To copy files or folders from a running container to the local filesystem, use the following command:

docker cp <container_name>:/<container_path> <local_path>

Let's break down the command:

  • <container_name>: Specifies the name or ID of the container you want to copy files from.
  • <container_path>: Specifies the path inside the container where the files are located.
  • <local_path>: Specifies the destination path on the local filesystem where the files will be copied.

For example, let's say you have a file named result.txt located inside the /usr/src/app directory of a running container named my-container, and you want to copy it to the /var/www directory on your local filesystem. You would use the following command:

docker cp my-container:/usr/src/app/result.txt /var/www/

This command will copy the result.txt file from the my-container container to the /var/www directory on your local filesystem.

Summary

The docker cp command is a handy tool for copying files or folders between a Docker container and the local filesystem. Whether you need to transfer files to a container or extract files from a container, this command provides an efficient way to synchronize resources and share data.

With the knowledge of the cp command, you can now easily manage file transfers between your Docker containers and the host machine. Experiment with different file paths and container names to copy files according to your requirements.

Thank you for reading this blog post. Keep exploring and mastering Docker commands to enhance your containerized development workflow. Happy coding!