Docker has become a go-to tool for developers. It allows you to containerize applications, making them easier to manage and deploy. However, managing files in Docker involves tasks like copying files into containers, retrieving files from containers, or even adding files during the image build process. That’s where the docker cp command comes in handy. The docker cp command allows you to copy files and directories between your host system and a Docker container. This command is essential when you need to add, update, or retrieve files from your containers.
In this guide, we’ll show you how to copy files to and from docker container using docker cp command.
Table of Contents
Basic Syntax and Options
The syntax of the docker cp command is simple.
# docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH
Here’s what each part means.
- SRC_PATH: The source path of the file or directory on the host.
- CONTAINER: The name or ID of the container.
- DEST_PATH: The destination path inside the container.
You can also copy files from the container to the host by reversing the paths:
# docker cp CONTAINER:SRC_PATH DEST_PATH
Copying Files from Host to Docker Container
Now, let’s see how to use docker cp to copy files from your host to a Docker container.
Example 1: Copying a Single File
Suppose you have a configuration file on your host that you need inside a container. You can copy it with this command:
# docker cp /tmp/nginx.conf mycontainer:/etc/nginx/nginx.conf
This command copies nginx.conf from your host to the /etc/nginx directory inside the container named mycontainer.
Example 2: Copying an Entire Directory
What if you need to copy a whole directory? It’s just as easy.
# docker cp /mnt/backup mycontainer:/opt/
This command copies your host’s backup directory to the /opt directory inside the container.
Copying Files from Docker Container to Host
Sometimes, you need to get files out of a container. Maybe you want to retrieve logs, backups, or generated data. The docker cp command also handles this.
Here’s how you do it.
# docker cp mycontainer:/var/log/mylog.log /tmp/
This command copies mylog.log from the container to your host’s /tmp/ directory.
Copying Files to Docker Image from Dockerfile
You can also copy files directly into a Docker image during the build process. This is done using the COPY instruction in a Dockerfile.
Example:
Suppose you have a simple Python web application that you want to containerize. Your project directory structure looks like this.
my-python-app/
│
├── app.py
├── requirements.txt
└── Dockerfile
Here’s how you might write the Dockerfile to copy these files into your Docker image.
FROM python:3.9-slim
# Set the working directory inside the container
WORKDIR /app
# Copy the requirements file into the container
COPY requirements.txt .
# Install the dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application code into the container
COPY . .
# Specify the command to run the application
CMD ["python", "app.py"]
In the above Dockerfile, the COPY instruction copies all files from the my-python-app directory to the Docker image. This is a crucial step when setting up an image that requires specific files.
Next, navigate to the Dockerfile’s directory and run the docker build command to build the image.
# docker build -t my-python-app .
This command builds the Docker image and tags it as my-python-app.
Now, run a container from the above image.
# docker run -d -p 5000:5000 my-python-app
This command runs the container in detached mode and maps port 5000 on your host to port 5000 in the container.
Conclusion
The docker cp command is essential for managing files in Docker containers. Whether you’re copying files from the host to the container, retrieving them from the container, or adding files during the image build process, this command makes your life easier.
FAQs
1. Will copying files overwrite existing files in the Docker container?
Yes, if the destination path already contains files with the same names, docker cp will overwrite those files without warning.
2. Can I copy multiple files at once from the host to a Docker container?
Yes, you can copy multiple files by specifying a directory or using a wildcard (*) to copy multiple files at once.
3. How do I check if the files have been successfully copied into the container?
After copying, you can enter the container using docker exec and verify the files.
4. Can I copy files from the host to a Docker container without using docker cp?
Yes, you can mount a directory from the host into the container using the -v or --mount option when starting the container.