Linux/Ubuntu Server

[docker] file 주고받기

Implementation details


CONTAINER:PATH -> HOSTPATH

To copy files or folders from a container to the host we have a native `docker cp` command available since the Docker 1.0.

For example, if we have a container named kickass_yonathand wish to copy /home/data.txtfrom this container to the host’s current directory, it can be done the following way:


$ docker cp kickass_yonath:/home/data.txt .

HOSTPATH -> CONTAINER:PATH

To copy files from the host to a container one can use the `docker exec` command available since Docker 1.3.0.

If have a test.txt file and wish to copy it to /home/e1/e2 inkickass_yonath container, first of all, we create a directory structure in the container with mkdir:


$ docker exec -i kickass_yonath sh -c ‘mkdir -p /home/e1/e2'

Next, copy the file by redirecting input and output:

$ docker exec -i kickass_yonath sh -c 'cat > /home/e1/e2/test.txt' < ./test.txt

When executed, the file is transferred from the current directory to the container.

CONTAINER1:PATH CONTAINER2:PATH

This use-case can be implemented by combining the both cases we listed above and using the host as the intermediate storage.


First of all, copy file to some temporary directory on the host:

$ docker cp kickass_yonath:/home/e1/e2/test.txt /tmp/tmp.pGlwFknjlF

Next, transfer it from the temp directory to the other container:

$ docker exec -i ecstatic_colden sh -c ‘cat > /home/test.txt’ < /tmp/tmp.pGlwFknjlF/test.txt

The same technique can be used to copy directories of any .


Source Code

The script `dcp.sh` for copying files and folders between docker containers can be downloaded from the github repo. See the included readme file for the details of usage.

NOTE: This shell script doesn't intend to replace the extended `cp` command in the upcoming Docker 1.7, it just implements some of the functionality we’re missing right now. It is encouraged to use the extended `docker cp` in the upcoming Docker instead of this script.

Minimum requirements:

  • Docker 1.3.0
  • bash shell available on the system.

Known limitations:

  • works only for running containers.

Tested on:

  • Ubuntu 14.04, Docker 1.6.0


,

알림

이 블로그는 구글에서 제공한 크롬에 최적화 되어있고, 네이버에서 제공한 나눔글꼴이 적용되어 있습니다.

카운터

Today :
Yesterday :
Total :

태그