Data Transfer Essentials: Copying Files in and out of Containers with docker cp
A hands-on guide to transferring files and directories between the host machine and Docker containers without mounting volumes.
Published on • November 17, 2026
AI Assistant

Transferring files between your local host filesystem and a Docker container is a common requirement when debugging applications, retrieving generated log files, updating configuration files on the fly, or extracting build artifacts.
The docker cp command provides a straightforward mechanism to copy files and directories into or out of containers without configuring bind mounts or network file transfers.
In this tutorial, we will master the syntax, behavior, trailing slash rules, and permissions of docker cp.
1. Syntax of docker cp
The basic syntax for docker cp mirrors standard Linux cp, with container paths prefixed by CONTAINER:
# Copy from host machine to container
docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH
# Copy from container to host machine
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH
2. Copying Files from Host to Container
To copy a local configuration file or script into a running or stopped container:
docker cp ./nginx.conf web-server:/etc/nginx/nginx.conf
Copying Entire Directories
To copy an entire folder structure into a container:
docker cp ./static-assets/ web-server:/usr/share/nginx/html/
3. Copying Files from Container to Host
To extract logs, core dumps, or generated reports from a container onto your local workstation:
# Copy a single log file
docker cp web-server:/var/log/nginx/error.log ./error.log
# Copy an entire data directory out of the container
docker cp database-container:/var/lib/postgresql/data ./db-backup/
4. Understanding Trailing Slashes and Directory Behavior
docker cp follows specific path destination rules based on trailing slashes (/) and whether the target directory exists.
Case A: Copying a File to an Existing Directory
If the destination path exists and is a folder, the source file is copied inside that directory:
# Result: /app/config.json
docker cp ./config.json my-container:/app/
Case B: Source Path Ends with /. (Contents Only)
If the source directory ends with /., docker cp copies only the contents of the directory, rather than the directory container itself:
# Copies contents of ./assets into /var/www/html/ without creating an 'assets' subfolder
docker cp ./assets/. my-container:/var/www/html/
5. Copying from Stopped Containers
A major advantage of docker cp is that the target container does not need to be running.
You can copy files to or from a stopped container (Exited state) as long as the container has not been deleted with docker rm:
# Extract logs from a crashed container
docker cp crashed-app:/var/log/crash.log ./crash.log
6. Preserving File Attributes (-a / --archive)
By default, docker cp copies files in archive mode, preserving system permissions (POSIX UID/GID, read/write/execute bits) and file timestamps where possible.
To explicitly ensure archive mode (preserving UID/GID metadata):
docker cp -a web-server:/app/data ./local-backup/
7. Container-to-Container Copying (via Pipe)
docker cp cannot copy directly between two containers in a single command. However, you can pipe tar archives between two docker cp invocations using standard OS streams:
docker cp container-a:/app/data - | docker cp - container-b:/app/
The - instructs Docker to stream raw tar archive data via STDOUT and STDIN.
Summary
docker cp is an indispensable tool for ad-hoc file transfers between host filesystems and Docker containers. It works across both running and stopped containers, supports streaming tar pipes, and preserves file attributes.
Takeaway: Use docker cp for ad-hoc file inspection and log extraction, but rely on bind mounts or named volumes for persistent, continuous data sharing in production.