Http://blog.e3rp4y.me/blog/2014/05/23/copy-file-from-host-to-docker.html
------------------------------------------------------------
Docker is a Linux container management software.
Let's talk about several ways to copy files from a host to Docker today.
Before sharing, let's look at how strong the Docker community needs to be for this issue (JU).
Below begin today the sharing of high (TU) Large (yuan) (FEI).
1. Adding files via build Docker image
Docker image is created by Dockerfile. The specific creation process can be referred to here.
When writing dockerfile, we can add files to the Docker image with the required files by ADD
keyword.
FROM 3scale/openresty## add your supervisor openresty configADD openresty.conf /etc/supervisor/conf.d/# Add your appADD . /var/wwwCMD ["supervisor"]
Reference from 3scale/openresty
The keyword in this dockerfile ADD
is to add this machine to a folder in Docker image /var/www
.
2.-v/--volume parameters via the Docker Run command
Let's say we need to share the/data directory of this machine in the/mnt directory of Docker, and we can use this command:
$ touch /data/bilibala$ docker run -v /data:/mnt -i -t ubuntu bash[email protected]:/# ls /mntbilibala
This command can be used to bind folders in the Startup container.
3. Binding Directories via API
In fact, this method is essentially the same as 2, but the only difference is that the API docker run
divides the command into two steps, namely: create_container
and start
in, the directories that create_container
need to be mounted are defined by the volumes
parameters. In start
, binds
parameter binding.
The following is a simple example:
#!/usr/bin/env python2.7import dockerc = docker.Client()container = c.create_container(‘ubunt‘, command=‘bash‘, volumes=[‘/mnt‘], tty=True, stdin_open=True)c.start(container[‘Id‘], binds={‘/data‘:‘/mnt‘})
This creates a container that is mounted on the /data
directory.
4. Passing files through environment variables
This is my own invention of the small trick, because in the use volumes
of parameters, found that docker some instability. It is often impossible to delete. Therefore, the file is transferred through the environment variable when it is created.
The file is base64
encoded and then passed through create_container
the parameters of the method environment
into the container, and then decoded into the container in the appropriate path.
5. Summary
In general, there are three different ways to pass files from host to container.
respectively:
- When you create an image, add a file to the image
- When creating container, pass the file through the volumes parameter
- When creating container, pass the file through the environment parameter
Copy file to Docker from Realhost