Introduction
Docker is a powerful tool for creating, deploying, and managing containers. A common scenario when using Docker involves building dependencies or compiling software inside a container on a Continuous Integration (CI) server and then transferring the resulting artifacts back to the host machine. This tutorial will guide you through various methods of copying files from a Docker container to your host system, as well as vice versa.
Understanding Docker Containers
Before diving into file transfer techniques, it’s essential to understand what Docker containers are. A Docker container is a lightweight and portable package that includes everything needed to run an application: code, runtime, libraries, environment variables, and configuration files. This encapsulation ensures that the application will work seamlessly in any environment.
Method 1: Using docker cp
The simplest way to transfer files between a Docker container and the host is by using the docker cp
command. This method works for both copying from the container to the host and vice versa.
Copying Files from Container to Host
To copy a file from a running or stopped container to your host machine, use:
docker cp <containerId>:/path/in/container /host/path/target
<containerId>
: The unique identifier for the Docker container. You can obtain this by runningdocker ps -a
./path/in/container
: The path within the container from where you want to copy files./host/path/target
: The target location on your host machine.
Example:
sudo docker cp goofy_roentgen:/out_read.jpg .
This command copies out_read.jpg
from the goofy_roentgen
container to the current directory on your host system.
Copying Files from Host to Container
To copy files from your host machine into a Docker container, you can reverse the order:
docker cp /host/path/source <containerId>:/path/in/container
Example:
docker cp C:/Users/someone/Desktop/output/server-test.png d86844abc129:/app/server/output/
Method 2: Using docker create
and docker cp
For cases where you don’t need to run a container, using docker create
can be beneficial. This method creates an image layer without starting the container, allowing file copying with minimal overhead.
docker create --name dummy IMAGE_NAME
docker cp dummy:/path/to/file /dest/on/host
docker rm -f dummy
This approach is efficient when you only need to prepare a filesystem for copying files but don’t want to run any processes in the container.
Method 3: Using Docker Volumes
Mounting a volume provides a direct way to share directories between your host and containers. This method is particularly useful for continuous file syncing during builds or tests.
Setting Up a Volume
mkdir artifacts
docker run -i -v ${PWD}/artifacts:/artifacts IMAGE_NAME sh << COMMANDS
# Build software here
cp <artifact> /artifacts
COMMANDS
When the container exits, all files in /artifacts
are available on your host.
Handling User Permissions
To address potential permission issues between the host and container user IDs, specify the UID:
docker run -i -v ${PWD}:/working_dir -w /working_dir -u $(id -u) IMAGE_NAME sh << COMMANDS
# Commands here will respect the current user's permissions
COMMANDS
Conclusion
Transferring files between Docker containers and host systems is a common requirement in software development, especially when utilizing CI/CD pipelines. By leveraging docker cp
, docker create
, and volumes, you can efficiently manage file transfers tailored to your workflow needs.
These methods not only help streamline the process but also ensure that artifacts are reliably transferred without compromising security or usability. Choose the method that best fits your use case for optimal performance in your Docker-based projects.