Copying files to and from containers
- Version
- 29
- Review
- Draft
Summary#
docker cp copies files or directories between your local filesystem and a running or stopped container. Put the container name before the colon in the container path.
Syntax#
docker cp ./input.txt CONTAINER:/tmp/input.txt
docker cp CONTAINER:/tmp/output.txt ./output.txt
docker cp CONTAINER:/path/to/dir/. ./existing-directory
Example#
The example uses a local alpine:3 image.
(
set -eu
name="dlr-docker-commands-copy-$$"
dir=$(mktemp -d)
trap 'docker rm -f "$name" >/dev/null 2>&1 || true; rm -rf -- "$dir"' EXIT
printf 'copied through a stopped container\n' > "$dir/input.txt"
docker create --pull never --name "$name" alpine:3 >/dev/null
docker cp "$dir/input.txt" "$name:/tmp/input.txt"
docker cp "$name:/tmp/input.txt" "$dir/output.txt"
cat "$dir/output.txt"
)
Expected output#
copied through a stopped container
Common mistakes#
Create missing parent directories before you copy. When the destination already exists as a directory, a source ending in /. copies the contents into it. A source without /. copies the directory itself into it.
Version applicability#
Checked with Docker Engine 29 on Linux containers. Paths on the container side are relative to the container's root, even if you omit the leading slash.
Sources#
- Attribution
- DevLookup contributors
- License
- CC-BY-4.0