1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Dockerfile の COPY の挙動

Last updated at Posted at 2024-09-29

目的

Dockerfile の COPY の挙動を理解する。

説明

ファイルのコピー

公式ドキュメント

image.png

ファイルを特定のディレクトリの中にコピーするときは、ディレクトリの終わりに / (スラッシュ)をつける必要がある。
つまり、ファイルのコピーには、下記の2パターンの方法がある。

(1) コピー先にディレクトリを指定する方法

# Dockerfile

# create /abs/test.txt
COPY test.txt /abs/

(2) の方法と違って、こっちの方法は、下記のようにまとめて複数のファイルをコピーできる。

# Dockerfile

# create /usr/src/things/file1.txt
# create /usr/src/things/file2.txt
COPY file1.txt file2.txt /usr/src/things/

(2) コピー先にファイルを指定する方法

# Dockerfile

# create /abs/test.txt
COPY test.txt /abs/test.txt

ディレクトリのコピー

公式ドキュメント

image.png

コピー元がディレクトリの時は、そのディレクトリの中身が、コピー先のディレクトリの中に展開される。
下記のように書くことで、ホストマシンの./web/ディレクトリの中身が、コンテナの/MyApp/web/ディレクトリの中に展開される。

# Dockerfile
COPY ./web/ /MyApp/web/

おまけ

docker cp コマンド(リンクはこちら)とは挙動が異なるので注意。

1
0
0

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?