How to use docker compose cp command to copy files

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to use the docker compose cp command to copy files and directories between a service container and the local filesystem. We will begin by setting up a simple Docker Compose project, including installing Docker Compose and creating a basic docker-compose.yml file with a service based on the Ubuntu image.

Following the project setup, you will practice copying a file from the service container to your local machine, and then copying a file from your local machine into the service container. Finally, you will learn how to copy an entire directory from the service container to your local filesystem, demonstrating the flexibility of the docker compose cp command for managing data within your Docker Compose environments.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker(("Docker")) -.-> docker/ImageOperationsGroup(["Image Operations"]) docker(("Docker")) -.-> docker/VolumeOperationsGroup(["Volume Operations"]) docker/ContainerOperationsGroup -.-> docker/run("Run a Container") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/exec("Execute Command in Container") docker/ImageOperationsGroup -.-> docker/pull("Pull Image from Repository") docker/VolumeOperationsGroup -.-> docker/cp("Copy Data Between Host and Container") subgraph Lab Skills docker/run -.-> lab-555075{{"How to use docker compose cp command to copy files"}} docker/ps -.-> lab-555075{{"How to use docker compose cp command to copy files"}} docker/exec -.-> lab-555075{{"How to use docker compose cp command to copy files"}} docker/pull -.-> lab-555075{{"How to use docker compose cp command to copy files"}} docker/cp -.-> lab-555075{{"How to use docker compose cp command to copy files"}} end

Prepare a simple Docker Compose project

In this step, we will prepare a simple Docker Compose project. Since Docker Compose is not pre-installed in the LabEx environment, we will first install it. Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application's services. Then, with a single command, you create and start all the services from your configuration.

First, let's install Docker Compose. We will download the Docker Compose binary and make it executable.

sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Now, let's verify the installation by checking the Docker Compose version.

docker-compose --version

You should see output similar to Docker Compose version v2.20.2.

Next, we will create a simple Docker Compose project. We will create a directory for our project and then create a docker-compose.yml file inside it. This file will define a simple service using the ubuntu image.

mkdir ~/project/my-compose-app
cd ~/project/my-compose-app
nano docker-compose.yml

In the nano editor, paste the following content into the docker-compose.yml file:

version: "3.8"
services:
  my-service:
    image: ubuntu:latest
    command: tail -f /dev/null

Press Ctrl + X, then Y, and Enter to save and exit the editor.

Let's break down the docker-compose.yml file:

  • version: '3.8' specifies the Docker Compose file format version.
  • services: defines the services that make up your application.
  • my-service: is the name of our service.
  • image: ubuntu:latest specifies the Docker image to use for this service. We are using the latest version of the Ubuntu image.
  • command: tail -f /dev/null is the command that will be executed when the container starts. This command keeps the container running indefinitely without consuming significant resources, which is useful for testing and debugging.

Before starting the service, we need to pull the ubuntu:latest image.

docker pull ubuntu:latest

Now, we can start the service using the docker-compose up command. The -d flag runs the containers in detached mode, meaning they will run in the background.

docker-compose up -d

You should see output indicating that the service is being created and started.

Finally, let's check the status of the running service using the docker-compose ps command.

docker-compose ps

You should see output showing that the my-service container is running.

Copy a file from a service container to the local filesystem

In this step, we will learn how to copy a file from a running Docker container to your local filesystem. This is a common task when you need to retrieve logs, configuration files, or other data generated inside a container.

We will use the docker cp command for this purpose. The docker cp command copies files/folders between a container and the local filesystem. The syntax is docker cp <container>:<src_path> <dest_path> to copy from a container to the local filesystem, and docker cp <src_path> <container>:<dest_path> to copy from the local filesystem to a container.

First, let's create a simple file inside our running my-service container. We will use the docker exec command to run a command inside the container. The docker exec command runs a new command in a running container.

Make sure you are in the ~/project/my-compose-app directory.

cd ~/project/my-compose-app
docker-compose exec my-service bash -c "echo 'This is a test file inside the container.' > /tmp/container_file.txt"

This command executes a bash shell inside the my-service container and then runs the echo command to create a file named /tmp/container_file.txt with some content.

Now, let's copy this file from the container to our local filesystem. We will copy it to the current directory (~/project/my-compose-app).

docker-compose cp my-service:/tmp/container_file.txt .

The docker-compose cp command is similar to docker cp, but it works with service names defined in your docker-compose.yml file. my-service is the name of our service, /tmp/container_file.txt is the path to the file inside the container, and . represents the current directory on our local filesystem.

After running the command, you should see the container_file.txt file in your ~/project/my-compose-app directory. Let's verify its existence and content.

ls container_file.txt
cat container_file.txt

You should see the filename listed and the content "This is a test file inside the container." printed to the console.

Copy a file from the local filesystem to a service container

In this step, we will learn how to copy a file from your local filesystem into a running Docker container. This is useful when you need to add configuration files, scripts, or other data to a container after it has been started.

We will again use the docker cp command, but this time in the opposite direction: docker cp <src_path> <container>:<dest_path>.

First, let's create a simple file on our local filesystem in the ~/project/my-compose-app directory.

cd ~/project/my-compose-app
echo "This file is from the local filesystem." > local_file.txt

Now, let's copy this local_file.txt from our local filesystem into the /tmp/ directory inside the my-service container.

docker-compose cp local_file.txt my-service:/tmp/

The docker-compose cp command is used here. local_file.txt is the path to the file on our local filesystem, my-service is the name of our service, and /tmp/ is the destination directory inside the container.

To verify that the file was copied successfully, we will execute a command inside the container to check for the file and display its content.

docker-compose exec my-service cat /tmp/local_file.txt

This command executes the cat /tmp/local_file.txt command inside the my-service container. You should see the content "This file is from the local filesystem." printed to your console, confirming that the file was copied correctly.

Copy a directory from a service container to the local filesystem

In this step, we will learn how to copy an entire directory from a running Docker container to your local filesystem. This is useful for retrieving application output directories, log directories, or any other directory structure generated within the container.

We will use the docker cp command again, similar to copying a file, but specifying a directory path.

First, let's create a directory and some files inside it within our running my-service container. We will use docker-compose exec to run commands inside the container.

Make sure you are in the ~/project/my-compose-app directory.

cd ~/project/my-compose-app
docker-compose exec my-service bash -c "mkdir /tmp/container_dir && echo 'File 1' > /tmp/container_dir/file1.txt && echo 'File 2' > /tmp/container_dir/file2.txt"

This command creates a directory named /tmp/container_dir inside the my-service container and then creates two files, file1.txt and file2.txt, within that directory.

Now, let's copy this entire directory from the container to our local filesystem. We will copy it to the current directory (~/project/my-compose-app).

docker-compose cp my-service:/tmp/container_dir .

The docker-compose cp command is used here. my-service is the name of our service, /tmp/container_dir is the path to the directory inside the container, and . represents the current directory on our local filesystem.

After running the command, you should see a new directory named container_dir in your ~/project/my-compose-app directory. Let's verify its existence and list its contents.

ls container_dir
ls container_dir/file1.txt container_dir/file2.txt

You should see the container_dir listed, and then the names of the two files inside it (file1.txt and file2.txt) should be listed.

Finally, let's clean up the Docker Compose services we started.

docker-compose down

This command stops and removes the containers, networks, and volumes created by docker-compose up.

Summary

In this lab, we learned how to use the docker compose cp command to copy files and directories between a service container and the local filesystem. We began by preparing a simple Docker Compose project, which involved installing Docker Compose and creating a docker-compose.yml file defining a basic service using the Ubuntu image.

Following the project setup, we practiced copying a file from the service container to the local filesystem, copying a file from the local filesystem into the service container, and finally, copying an entire directory from the service container to the local filesystem. These steps demonstrated the practical application of the docker compose cp command for managing data transfer within a Docker Compose environment.