How to copy files from host system to a docker container using netcat

A simple way to copy files from the docker’s host system to a docker container is by using netcat command. First make sure that nc command is available within your docker container by installation of nectcat package. In the following scenario we are going to transfer file myfile.txt to a docker container with a container ID eg.e350390fd549.

Destination Docker Container

First, using interactive shell within a docker container execute nc command to listen on some arbitrary port which will be used to receive file. In this case the port number is 7555:

root@e350390fd549:~# nc -l -p 7555 > /root/myfile.txt

Source Host System

Next, get the container’s IP address:

# docker inspect -f '{{ .NetworkSettings.IPAddress }}' e350390fd549

Using another terminal session on your host system copy file myfile.txt to a docker container using nc command and its IP address as a destination:

# nc 7555 < myfile.txt

All done. You can confirm that the file was transferred correctly to your docker container using md5sum:

# md5sum myfile.txt 
d41d8cd98f00b204e9800998ecf8427e  myfile.txt
# docker exec -it e350390fd549 md5sum /root/myfile.txt
d41d8cd98f00b204e9800998ecf8427e  /root/myfile.txt