How to copy files between a host and a Docker container

If you're new to developing with Docker containers, there are a few tricks you're going to want to have up your sleeve. Jack Wallen shows you how to copy files from host to container.

docker-new.jpg

Image: o_m/Shutterstock

Your business depends on containers. If not now, it will. Your developers know this, and they're either ready for the task or they're gearing up for it. For those who don't know, container development isn't quite as simple as standard application development. With containers, there are considerably more moving parts involved and more cooks in the kitchen.

SEE: Kubernetes: A cheat sheet (free PDF) (TechRepublic)

But once you've settled on a process, it can go smoothly. For that, it's all a matter of knowing how easy Docker containers are to work with. Take, for instance, the process of copying files from the host to a running container. It's quite easy.

Why would you do this? It's actually simple. A developer might want to write code outside of the container (say, in an IDE). Or maybe more than one software engineer is working on the project, but only one member of the team has access to the running container. In such a case, the team members would share their code, and the lead would then need to get the new code into the container. Sure, the lead could cut and paste, but why not just copy the new file with a single command?

Let me show you how this is done.

What you'll need

To make this work, you'll need a running instance of Docker and a user that's a member of the docker group. That's it. Let's get our container on.

How to deploy NGINX

We're going to demonstrate this using the tried and true NGINX container. Why? Because it's the easiest way to see the results of this copy command. 

First, let's deploy the NGINX container. We'll deploy it in detached mode listening to external port 8080 and internal port 80. Our container name will be docker-nginx and is deployed with the command:

docker run --name docker-nginx -p 8080:80 -d nginx

Once the container is up and running, point a web browser to http://SERVER:8080 (where SERVER is the IP address of container host). You should see the NGINX welcome page (Figure A).

Figure A

copyfilea.jpg

The NGINX Welcome page displayed by our running container.

How to copy a file to the running container

Let's create a new Welcome page for the container. Create this file with the command:

nano index.html

In that file, paste the following:

<!DOCTYPE html>
<html>
<body>

<h1>Hello, TechRepublic</h1>

</body>
</html>

Save and close the file. 

Now, we can copy the new index file into the container. The thing to know about the NGINX Docker container is that the document root (the directory from which web pages are served) is /usr/share/nginx/html (instead of /var/www/html). So that's our destination. The command to copy files is:

docker cp FILE CONTAINER:DESTINATION

Where FILE is the file name to be copied, CONTAINER is the name of the running container, and DESTINATION is the directory within the container that will house the new file. In our instance, the command would be:

docker cp index.html docker-ngxin:/usr/share/nginx/html

After copying the file, refresh your browser page and the welcome page should now say Hello, TechRepublic (Figure B).

Figure B

copyfileb.jpg

Our new NGINX welcome page has been copied to the running container.

You can use the same command to copy an entire directory as well. Say you've created a new website called ecommerce and you want to copy that to the NGINX document root. You'd do that with a similar command:

docker cp ecommerce docker-nginx:/usr/share/nginx/html

The above command would copy the directory and all of its contents.

And that's all there is to copying files and folders from a host to a running Docker container. 

Subscribe to TechRepublic's How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Also see