Dockerfile とは
- Dockerfileから自動的にイメージをビルドすることができる
- Dockerfileでイメージを生成するとき、既存のイメージのレイヤ上に新しい書き込み可能なレイヤを追加する
- Dockerfileで生成されるコンテナは、一時的であるべきことが推奨される
- ビルドキャッシュを利用できる場合は、利用する
他にもいろいろ留意点がありますが、割愛します
https://docs.docker.jp/develop/develop-images/dockerfile_best-practices.html#understand-build-context
Dockerfileからイメージ・コンテナを生成する
Dockerfileの中身
# FROMでイメージを指定
FROM httpd:latest
# コンテナがコマンドを開始するディレクトリを指定
WORKDIR /usr/local/apache2/htdocs/
# COPYでホストからファイルをイメージにコピーする
COPY materials_httpd/index.html index.html
※ホスト側は相対パスで指定とし、コンテナ側はフルパスで指定すること
Dockerfileからイメージを作成する
$ docker build --tag dev_image_httpd .
[+] Building 0.3s (8/8) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 312B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/httpd:latest 0.0s
=> [1/3] FROM docker.io/library/httpd:latest 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 139B 0.0s
=> CACHED [2/3] WORKDIR /usr/share/nginx/html 0.0s
=> [3/3] COPY materials_httpd/index.html index.html 0.1s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:89b00d234120c1db18e1622204a195700827412b2d648cf7ea36e84c14a599b3 0.0s
=> => naming to docker.io/library/dev_image_httpd 0.0s
What's Next?
View a summary of image vulnerabilities and recommendations → docker scout quickview
自作イメージからコンテナを起動
$ docker images -f "reference=dev_image_httpd:latest"
$ docker run -d -p 8054:80 --rm --name dev_container_httpd dev_image_httpd
57128ce9ec436b2d7fd5c1f9a39fcee49c0b841010879c30bea1c9e415911c1d
ステータスを確認
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7a749c21bac2 dev_image_httpd "httpd-foreground" 23 seconds ago Up 22 seconds 0.0.0.0:8054->80/tcp dev_container_httpd
確認
$ curl localhost:8054
hip, hip array!
cat, cat, cat, cat, cat array!
追加された読み取り専用レイヤーを確認
docker history dev_image_httpd
IMAGE CREATED CREATED BY SIZE COMMENT
762b29f5371d 2 hours ago COPY materials_httpd/index.html index.html #… 47B buildkit.dockerfile.v0
<missing> 2 hours ago WORKDIR /usr/local/apache2/htdocs/ 0B buildkit.dockerfile.v0
<missing> 2 months ago CMD ["httpd-foreground"] 0B buildkit.dockerfile.v0
<missing> 2 months ago EXPOSE map[80/tcp:{}] 0B buildkit.dockerfile.v0
<missing> 2 months ago COPY httpd-foreground /usr/local/bin/ # buil… 138B buildkit.dockerfile.v0
<missing> 2 months ago STOPSIGNAL SIGWINCH 0B buildkit.dockerfile.v0
<missing> 2 months ago RUN /bin/sh -c set -eux; savedAptMark="$(a… 81.7MB buildkit.dockerfile.v0
<missing> 2 months ago ENV HTTPD_PATCHES= 0B buildkit.dockerfile.v0
<missing> 2 months ago ENV HTTPD_SHA256=fa16d72a078210a54c47dd5bef2… 0B buildkit.dockerfile.v0
<missing> 2 months ago ENV HTTPD_VERSION=2.4.58 0B buildkit.dockerfile.v0
<missing> 2 months ago RUN /bin/sh -c set -eux; apt-get update; a… 11MB buildkit.dockerfile.v0
<missing> 2 months ago WORKDIR /usr/local/apache2 0B buildkit.dockerfile.v0
<missing> 2 months ago RUN /bin/sh -c mkdir -p "$HTTPD_PREFIX" && … 0B buildkit.dockerfile.v0
<missing> 2 months ago ENV PATH=/usr/local/apache2/bin:/usr/local/s… 0B buildkit.dockerfile.v0
<missing> 2 months ago ENV HTTPD_PREFIX=/usr/local/apache2 0B buildkit.dockerfile.v0
<missing> 2 months ago /bin/sh -c #(nop) CMD ["bash"] 0B
<missing> 2 months ago /bin/sh -c #(nop) ADD file:b86ae1c7ca3586d8f… 74.8MB
イメージ削除の削除
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
57128ce9ec43 dev_image_httpd "httpd-foreground" 2 hours ago Up 2 hours 0.0.0.0:8054->80/tcp dev_container_httpd
$ docker stop dev_container_httpd
dev_container_httpd
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
$ docker rmi dev_image_httpd
Untagged: dev_image_httpd:latest