Copy file to Docker from Realhost

Source: Internet
Author: User
Tags docker run

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:

    1. When you create an image, add a file to the image
    2. When creating container, pass the file through the volumes parameter
    3. When creating container, pass the file through the environment parameter

Copy file to Docker from Realhost

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.