How to copy files between a host and a Docker container

3 years ago 302

If you're caller to processing with Docker containers, determination are a fewer tricks you're going to privation to person up your sleeve. Jack Wallen shows you however to transcript files from big to container.

docker-new.jpg

Image: o_m/Shutterstock

Your concern depends connected containers. If not now, it will. Your developers cognize this, and they're either acceptable for the task oregon they're gearing up for it. For those who don't know, instrumentality improvement isn't rather arsenic elemental arsenic modular exertion development. With containers, determination are considerably much moving parts progressive and much cooks successful the kitchen.

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

But erstwhile you've settled connected a process, it tin spell smoothly. For that, it's each a substance of knowing however casual Docker containers are to enactment with. Take, for instance, the process of copying files from the big to a moving container. It's rather easy.

Why would you bash this? It's really simple. A developer mightiness privation to constitute codification extracurricular of the instrumentality (say, successful an IDE). Or possibly much than 1 bundle technologist is moving connected the project, but lone 1 subordinate of the squad has entree to the moving container. In specified a case, the squad members would stock their code, and the pb would past request to get the caller codification into the container. Sure, the pb could chopped and paste, but wherefore not conscionable transcript the caller record with a azygous command?

Let maine amusement you however this is done.

What you'll need

To marque this work, you'll request a moving lawsuit of Docker and a idiosyncratic that's a subordinate of the docker group. That's it. Let's get our instrumentality on.

How to deploy NGINX

We're going to show this utilizing the tried and existent NGINX container. Why? Because it's the easiest mode to spot the results of this transcript command. 

First, let's deploy the NGINX container. We'll deploy it successful detached mode listening to outer larboard 8080 and interior larboard 80. Our instrumentality sanction volition beryllium docker-nginx and is deployed with the command:

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

Once the instrumentality is up and running, constituent a web browser to http://SERVER:8080 (where SERVER is the IP code of instrumentality host). You should spot the NGINX invited leafage (Figure A).

Figure A

copyfilea.jpg

The NGINX Welcome leafage displayed by our moving container.

How to transcript a record to the moving container

Let's make a caller Welcome leafage for the container. Create this record with the command:

nano index.html

In that file, paste the following:

<!DOCTYPE html> <html> <body> <h1>Hello, TechRepublic</h1> </body> </html>

Save and adjacent the file. 

Now, we tin transcript the caller scale record into the container. The happening to cognize astir the NGINX Docker instrumentality is that the papers basal (the directory from which web pages are served) is /usr/share/nginx/html (instead of /var/www/html). So that's our destination. The bid to transcript files is:

docker cp FILE CONTAINER:DESTINATION

Where FILE is the record sanction to beryllium copied, CONTAINER is the sanction of the moving container, and DESTINATION is the directory wrong the instrumentality that volition location the caller file. In our instance, the bid would be:

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

After copying the file, refresh your browser leafage and the invited leafage should present accidental Hello, TechRepublic (Figure B).

Figure B

copyfileb.jpg

Our caller NGINX invited leafage has been copied to the moving container.

You tin usage the aforesaid bid to transcript an full directory arsenic well. Say you've created a caller website called ecommerce and you privation to transcript that to the NGINX papers root. You'd bash that with a akin command:

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

The supra bid would transcript the directory and each of its contents.

And that's each determination is to copying files and folders from a big to a moving Docker container. 

Subscribe to TechRepublic's How To Make Tech Work connected YouTube for each the latest tech proposal for concern pros from Jack Wallen.

Developer Essentials Newsletter

From the hottest programming languages to the jobs with the highest salaries, get the developer quality and tips you request to know. Weekly

Sign up today

Also see

Read Entire Article