docker cpコマンドでディレクトリをコピーする

How to copy a directory using docker cp command

Takuo Oki
Jan 22 · 4 min read

コンテナ側のパスが存在しない場合

$ ls -l
total 0
drwxrwxrwx 1 takuo takuo 4096 Jan 17 17:00 bar
$ ls -l bar
total 0
-rw-rw-rw- 1 takuo takuo 0 Jan 17 17:00 dummy
$
$ docker create -v /foo --name test alpine:3.8 /bin/true
xxxxx...
$ docker cp ./bar test:/foo/bar
$ docker run -i --volumes-from test ubuntu /bin/bash
ls -l foo
total 0
drwxrwxrwx 1 1000 1000 4096 Jan 17 08:01 bar
ls -l foo/bar
total 0
-rw-rw-rw- 1 1000 1000 0 Jan 17 08:01 dummy

コンテナ側のパスが存在する場合

$ docker create -v /foo/bar --name test2 alpine:3.8 /bin/true
xxxxx...
$ docker cp ./bar test2:/foo/bar
$ docker run -i --volumes-from test2 ubuntu /bin/bash
ls -l foo
total 0
drwxr-xr-x 1 root root 4096 Jan 17 08:00 bar
ls -l foo/bar
total 0
drwxrwxrwx 1 1000 1000 4096 Jan 17 08:01 bar
ls -l foo/bar/bar
total 0
-rw-rw-rw- 1 1000 1000 0 Jan 17 08:01 dummy
$ docker create -v /foo/bar --name test3 alpine:3.8 /bin/true
xxxxx...
$ docker cp ./bar/ test3:/foo/bar
$ docker run -i --volumes-from test3 ubuntu /bin/bash
ls -l foo
total 0
drwxr-xr-x 1 root root 4096 Jan 17 08:00 bar
ls -l foo/bar
total 0
drwxrwxrwx 1 1000 1000 4096 Jan 17 08:01 bar
ls -l foo/bar/bar
total 0
-rw-rw-rw- 1 1000 1000 0 Jan 17 08:01 dummy
$ docker create -v /foo/bar --name test4 alpine:3.8 /bin/true
xxxxx...
$ docker cp ./bar/. test4:/foo/bar
$ docker run -i --volumes-from test4 ubuntu /bin/bash
ls -l foo
total 0
drwxr-xr-x 1 root root 4096 Jan 17 08:00 bar
ls -l foo/bar
total 0
-rw-rw-rw- 1 1000 1000 0 Jan 17 08:01 dummy

VELTRA Engineering

Posts from the VELTRA Engineering team. www.veltra.com

Takuo Oki

Written by

Takuo Oki

Engineer who likes travel, simple code, and something new

VELTRA Engineering

Posts from the VELTRA Engineering team. www.veltra.com