If you’re new to Docker container development, you should have a few tricks up your sleeve. Jack Wallen shows you how to copy files from the host to the container.
Your business depends on containers. If not now, it will. Your developers know this, and they are either ready for the task or preparing for it. For those who don’t know, container development isn’t quite as easy as developing standard applications. With containers, there are far more moving parts and more cooks in the kitchen.
GO TO Kubernetes: A Cheat Sheet (Free PDF) (TechRepublic)
But once you’ve committed to a process, it can run smoothly. To do this, you have to know how easy it is to handle Docker containers. Take, for example, the process of copying files from the host to a running container. It’s very easy.
Why would you do that It’s actually easy. A developer may want to write code outside of the container (for example, 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 have to transfer 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 to do this.
What you will need
For this to work you need a running instance of Docker and a user who is a member of the Docker group. That’s it. Let’s attach our container.
How to Implement NGINX
We demonstrate this using the proven NGINX container. Why? Because it is the easiest way to see the results of this copy command.
First we deploy the NGINX container. We deploy it in disconnected mode listening on external port 8080 and internal port 80. Our container name is docker-nginx and is provided 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 the container host). You should see the NGINX welcome page (Figure A.).
How to copy a file into the running container
Let’s create a new welcome page for the container. Create this file with the command:
In this file, paste the following:
Save and close the file.
Now we can copy the new index file into the container. What you need to know about the NGINX Docker container is that the document root (the directory that web pages are served from) is / usr / share / nginx / html (instead of / var / www / html). So this is our goal. The command to copy files is:
docker cp FILE CONTAINER:DESTINATION
FILE is the file name to be copied, CONTAINER the name of the current container and DESTINATION the directory within the container in which the new file is saved. In our case 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 be Hi TechRepublic (Figure B.).
You can also use the same command to copy an entire directory. Suppose you have created a new website called E-Commerce and you want to copy it to the NGINX document root. You would do this 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 you need to copy files and folders from a host to a running Docker container.
Subscribe to TechRepublics How to get tech up and running on YouTube for the latest technical advice to business professionals from Jack Wallen.