Logo ka1.io
Docker Cheat Sheet: Copying Files

Docker Cheat Sheet: Copying Files

Learn how to quickly copy files from host to Docker containers & vice versa.

January 2, 2022
1 min read
Table of Contents

Introduction

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