Upload a file to a Docker container

Upload a file to a Docker container

There are situations where you want to copy a file from your machine to a running Docker container or vice versa. Especially in cases when the Docker container does not use a volume that is bound to your local file system.  For these situations you can use one of the approaches described below.

Use Pipes

Pipes are a fantastic way passing data from one process to another. The following example uses cat to read the file content of the missing_data.sql and hands it over to the running Docker container.

cat missing_data.sql | \ 
docker exec -i <your container name> \
sh -c 'cat > /missing_data.sql'

Once this process has been completed you will find the file inside the container at /missing_data.sql.

Use the Docker Binary

Nevertheless copying can be achieved by using pipes, I recommend to use the copy utility of the Docker binary. It is much easier to ready and simpler to write.

docker cp missing_data.sql <container-id>:/missing_data.sql

I hope this helped you exchanging files with Docker containers

Silvio Wangler

Silvio Wangler

Embrach, Switzerland