🖥

Dockerfile の FROM AS ( multi-stage build ) で複数のイメージを作成して本番・開発環境などを切り替え

2019/10/19に公開

Dockerfile

  • curl だけ入ったイメージに curl-only という名前をつける
  • curl-only を元に vim も入れたイメージとして curl-with-vim とう名前をつける
from alpine:3.10.2 AS curl-only
run apk add curl

from curl-only AS curl-with-vim
run apk add vim

build

  • --target に AS でつけた名前を指定して docker build する
docker build --target=curl-only -t production .

docker build --target=curl-with-vim -t development .
$ docker images
REPOSITORY                                TAG                 IMAGE ID            CREATED             SIZE
development                               latest              d26a7ed31fc2        11 seconds ago      36MB
production                                latest              6ad1165a2fe5        24 seconds ago      8.34MB

docker run

alpine本体にはcurlもvimもない

$ docker run -it alpine:3.10.2 ash
/ # curl
ash: curl: not found
/ # vim
ash: vim: not found

production image には curl はあるが vim はない

$ docker run -it production ash
/ # curl
curl: try 'curl --help' or 'curl --manual' for more information
/ # vim
ash: vim: not found
/ #

development image には curl も vim もある

$ docker run -it development ash
/ # curl
curl: try 'curl --help' or 'curl --manual' for more information
/ # vim

image

docker-compose

ver 3.4 以上で target が指定できる様子

version: "3.7"
services:
  some:
    build: 
      context: .
      target: curl-with-vim
docker-compose build

docker-compose run some

vim

Dockerfile の案

AS に環境名をつけてしまっても良いかもしれない

from alpine:3.10.2 AS production
run apk add curl

from production AS development
run apk add vim
docker build --target=production -t production .
docker build --target=development -t production .

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/2601

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2019-10-19

Discussion