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

More than 3 years have passed since last update.

[Docker]COPYとADDについて

Posted at

はじめに

本記事では、DockerfileのCOPYADDについて記述します。

COPY

build contextにあるDockerfile以外のファイルをDocker imageやコンテナに持っていくことができる。

単純にファイルフォルダをコピーする場合。

COPY <ホストにあるコピーしたいファイル名> <コンテナ内にコピーしたものを入れるためのディレクトリ>

ADD

tarファイルなど圧縮ファイルをコピーして使う場合。

COPYの流れ

build contextをもとにDockerdeamonDockerfileからDockerimageを作ります。

ビルドコンテキストの理解より

docker buildコマンドを実行したときの、カレントなワーキングディレクトリのことを ビルドコンテキスト(build context)と呼びます。

Dockerimageを構築するには、Dockerfilebuild contextを渡します。

そこからDockerimageをdocker run -it ~~をすることで、
コンテナにてDockerfileを記述することができます。

今回は、anotherというファイルを一緒に起動します。
コンテナ内にもanotherを入れるために、COPYを記述します。

FROM ubuntu:latest
↑私は、ubuntuをよく使うのでこうします。

RUN mkdir /another_dir
↑コンテナ上で、another_dirというanotherファイルを入れるためのディレクトリを作ります。

COPY another /another_dir/
↑「build context」内にあるanotherをCOPYして、コンテナ内の「another_dir」にanotherを入れます。
% docker build .

% docker run -it image名 bash

このような流れになります。

ADDの流れ

Dockerimageに移した時に、圧縮ファイルを解凍して、コンテナに渡します。

FROM ubuntu:latest
↑私は、ubuntuをよく使うのでこうします。

ADD compressed.tar /
↑tarファイルをDockerimageに移したときに解答して、コンテナに渡す。
% mkdir another_dir
ホストに「another_dir」というディレクトリを作ります。

% echo 'aaa' > aaa
「aaa」というファイルを作った上、「aaa」というテキストを入力しておきます。

% tar -cvf compressed.tar another_dir
「another_dir」を圧縮ファイルにする。ホスト上に「compressed.tar」が出来上がる

% docker build .
ここで、Dockerfileには、「ADD」の記載があるので、圧縮ファイルもimageへ。

% docker run -it image名 bash
コンテナにて解凍されたファイル「aaa」が存在でき、「another_dir」というディレクトリの中に入っている状態になる。

このような流れになります。

以上です。

終わりに

少しずつですが、インプットとアウトプットを交互にやり理解ができるようになってきました。
これも最高の講座に出会えたおかげや。。。

私の学習している動画はこちらです。(有料)
最高に分かりやすいのでオススメです。
米国AI開発者がゼロから教えるDocker講座

明日もDockerやってくぞー!

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