Skip to main content

Docker: Copying Files

·1 min

Copying files with Docker can be confusing. Let’s check out some common docker cp commands for copying files with Docker.

Start with docker ps to obtain your relevant container IDs.

Container to Host #

One specific file can be copied to the container like:

docker cp container_id:./bar/foo.txt .

Container to Host

Host to Container #

One specific file can be copied from the container like:

docker exec -i container_id sh -c 'cat > ./bar/foo.txt' < ./foo.txt

or

docker cp foo.txt mycontainer:/foo.txt

Host to Container

Container to Container #

From a container to a container mixes 1 and 2:

docker cp container_id1:./bar/foo.txt .
docker exec -i container_id2 sh -c 'cat > ./bar/foo.txt' < ./foo.txt

Container to Container