copy docker files to local machine code example

Example 1: copy file from docker container to host

docker cp <containerId>:/file/path/within/container /host/path/target

Example 2: copy file to docker container

docker cp foo.txt mycontainer:/foo.txt

Example 3: docker cp volume

Usage
docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

To copy data from the volume to the host, use a temporary container that has the volume mounted.

CID=$(docker run -d -v hello:/hello busybox true)
docker cp $CID:/hello ./
To copy a directory from the host to volume

cd local_dir
docker cp . $CID:/hello/
Then clean up the temporary container.

docker rm $CID

Example 4: dockerfile copy specific files

COPY ["__BUILD_NUMBER", "README.md", "gulpfile", "another_file", "./"]
		All the files and folders, the last is destination

Tags: