LoginSignup
18
13

More than 5 years have passed since last update.

[docker] COPY とADD の違いを試してみた2

Posted at

本記事は、Docker2 Advent Calendar 2016 の19日目の記事になります。

概要

以前、COPY とADD の違いを試してみた という記事を書いたがそれの残タスクがあったのでそれを試してみる。
※ version が以前とはだいぶ違うが
以前のも動くことは後ほど検証 or 以前の記事をversion をあげて書き直す

検証内容

コピー元をURL 指定、ワイルドカード指定

環境

  • OS : cent7.3
cat /etc/redhat-release
  # CentOS Linux release 7.3.1611 (Core)
  • docker : 1.10.3
[root@docker-engine centos]# docker version
  # Client:
  #  Version:         1.10.3
  #  API version:     1.22
  #  Package version: docker-common-1.10.3-59.el7.centos.x86_64
  #  Go version:      go1.6.3
  #  Git commit:      3999ccb-unsupported
  #  Built:           Thu Dec 15 17:24:43 2016
  #  OS/Arch:         linux/amd64
  # 
  # Server:
  #  Version:         1.10.3
  #  API version:     1.22
  #  Package version: docker-common-1.10.3-59.el7.centos.x86_64
  #  Go version:      go1.6.3
  #  Git commit:      3999ccb-unsupported
  #  Built:           Thu Dec 15 17:24:43 2016
  #  OS/Arch:         linux/amd64

※ install はこちらを参照

ファイル構成

[root@docker-engine work]# tree /tmp/work/
  # /tmp/work/
  # ├── fuga-dir
  # │   ├── fuga-file
  # │   └── fuga.tar
  # ├── funga
  # └── hoge-dir
  #     ├── hoge-file
  #     └── hoge.zip
  # 
  # 2 directories, 5 files

[root@docker-engine work]# ls -Rl /tmp/work/
  # /tmp/work/:
  # total 12
  # drwxr-xr-x 2 root root 4096 Dec 19 17:26 fuga-dir
  # -rw-r--r-- 1 root root    6 Dec 19 17:27 funga
  # drwxr-xr-x 2 root root 4096 Dec 19 17:27 hoge-dir
  # 
  # /tmp/work/fuga-dir:
  # total 16
  # -rw-r--r-- 1 root root     9 Dec 19 17:25 fuga-file
  # -rw-r--r-- 1 root root 10240 Dec 19 17:25 fuga.tar
  # 
  # /tmp/work/hoge-dir:
  # total 8
  # -rw-r--r-- 1 root root   9 Dec 19 17:26 hoge-file
  # -rw-r--r-- 1 root root 164 Dec 19 17:27 hoge.zip

test1 : URL を指定してCOPY

Dockerfile
FROM centos:7
MAINTAINER hihihiroro

RUN mkdir /tmp/work

COPY http://example.com/index.html /tmp/work/example.html

build

[root@docker-engine work]# cd /tmp/work/
[root@docker-engine work]# docker build -t copy-url .
  # Sending build context to Docker daemon 17.92 kB
  # Step 1 : FROM centos:7
  #  ---> 67591570dd29
  # Step 2 : MAINTAINER hihihiroro
  #  ---> Running in 1baa509382b6
  #  ---> ec1a2737296f
  # Removing intermediate container 1baa509382b6
  # Step 3 : RUN mkdir /tmp/work
  #  ---> Running in 52a56ea88420
  #  ---> 412a7e8f4fb5
  # Removing intermediate container 52a56ea88420
  # Step 4 : COPY http://example.com/index.html /tmp/work/example.html
  # Source can't be a URL for COPY

結果確認 : test1

Source can't be a URL for COPY

COPY ではURL をSource に使えないのでimage のbuild に失敗した。

test2 : URL を指定してADD

Dockerfile
FROM centos:7
MAINTAINER hihihiroro

RUN mkdir /tmp/work

ADD http://example.com/index.html /tmp/work/example.html

build

[root@docker-engine work]# cd /tmp/work/
[root@docker-engine work]# docker build -t add-url .
  # Sending build context to Docker daemon 17.92 kB
  # Step 1 : FROM centos:7
  #  ---> 67591570dd29
  # Step 2 : MAINTAINER hihihiroro
  #  ---> Running in 31f2758088ce
  #  ---> c6415c5c5827
  # Removing intermediate container 31f2758088ce
  # Step 3 : RUN mkdir /tmp/work
  #  ---> Running in ef4b04f5016c
  #  ---> 8d58fed49187
  # Removing intermediate container ef4b04f5016c
  # Step 4 : ADD http://example.com/index.html /tmp/work/example.html
  # Downloading  1.27 kB
  # 
  #  ---> 8b697cfa387c
  # Removing intermediate container c4551b9e2f16
  # Successfully built 8b697cfa387c

結果確認 : test2

[root@docker-engine work]# docker run --rm -it add-url /bin/bash
[root@8ed50e3289c6 /]# tree /tmp/work
  # /tmp/work
  # `-- example.html
  # 
  # 0 directories, 1 file
[root@8ed50e3289c6 /]# ls -Rl /tmp/work/
  # /tmp/work/:
  # total 4
  # -rw------- 1 root root 1270 Aug  9  2013 example.html

URL 先からFile を取得してきて、指定したフォルダにファイルを置いてくれている。

test3 : ワイルドカード指定でCOPY

Dockerfile
FROM centos:7
MAINTAINER hihihiroro

RUN mkdir /tmp/work

COPY fu* /tmp/work/

build

[root@docker-engine work]# cd /tmp/work/
[root@docker-engine work]# docker build -t wild-copy .
  # Sending build context to Docker daemon 17.92 kB
  # Step 1 : FROM centos:7
  #  ---> 67591570dd29
  # Step 2 : MAINTAINER hihihiroro
  #  ---> Running in 2a50eca80fa3
  #  ---> 134a3911d9ba
  # Removing intermediate container 2a50eca80fa3
  # Step 3 : RUN mkdir /tmp/work
  #  ---> Running in 3667ceea90ec
  #  ---> bd93f183a910
  # Removing intermediate container 3667ceea90ec
  # Step 4 : COPY fu* /tmp/work/
  #  ---> ab013b9f30ff
  # Removing intermediate container 5baf64af9ae5
  # Successfully built ab013b9f30ff

結果確認 : test3

[root@docker-engine work]# docker run -it --rm wild-copy /bin/bash
[root@b417c938d0ea /]# tree /tmp/work
  # /tmp/work
  # |-- fuga-file
  # |-- fuga.tar
  # `-- funga
  # 
  # 0 directories, 3 files
[root@b417c938d0ea /]# ls -Rl /tmp/work/
  # /tmp/work/:
  # total 20
  # -rw-r--r-- 1 root root     9 Dec 19 08:25 fuga-file
  # -rw-r--r-- 1 root root 10240 Dec 19 08:25 fuga.tar
  # -rw-r--r-- 1 root root     6 Dec 19 08:27 funga

fu から始まるファイルがコピーされていることを確認できた。

test4 : ワイルドカード指定でADD

Dockerfile
FROM centos:7
MAINTAINER hihihiroro

RUN mkdir /tmp/work

ADD fu* /tmp/work/

build

[root@docker-engine work]# cd /tmp/work/
[root@docker-engine work]# docker build -t wild-add .
  # Sending build context to Docker daemon 17.92 kB
  # Step 1 : FROM centos:7
  #  ---> 67591570dd29
  # Step 2 : MAINTAINER hihihiroro
  #  ---> Running in c3de41790144
  #  ---> 40d4a96bd53e
  # Removing intermediate container c3de41790144
  # Step 3 : RUN mkdir /tmp/work
  #  ---> Running in db4ed718baa1
  #  ---> ccce242c1c3e
  # Removing intermediate container db4ed718baa1
  # Step 4 : ADD fu* /tmp/work/
  #  ---> 1601bf04b91c
  # Removing intermediate container a21b0b91868c
  # Successfully built 1601bf04b91c

結果確認 : test4

[root@docker-engine work]# docker run -it --rm wild-add /bin/bash
[root@db0ac8aa9ab7 /]# tree /tmp/work
  # /tmp/work
  # |-- fuga-file
  # |-- fuga.tar
  # `-- funga
  # 
  # 0 directories, 3 files
[root@db0ac8aa9ab7 /]# ls -Rl /tmp/work/
  # /tmp/work/:
  # total 20
  # -rw-r--r-- 1 root root     9 Dec 19 08:25 fuga-file
  # -rw-r--r-- 1 root root 10240 Dec 19 08:25 fuga.tar
  # -rw-r--r-- 1 root root     6 Dec 19 08:27 funga

copy と同じくfu から始まるファイルがコピーされていることを確認できた。
ただし、ファイル名を指定した時とは違く、tar は展開されていない。

まとめ

共通点

  • ワイルドカード指定の場合、COPYADD 両方共ファイルをコピーする。(ただし、ADD はファイルしていでないとtar は展開されない)

差分

  • ADD はURL をSOURCE に使うことができる。COPY はURL をSOURCE として使うことはできない。

参考

COPY DOCS
ADD DOCS

18
13
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
13