20
12

More than 1 year has passed since last update.

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

Last updated at Posted at 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

チャットメンバー募集

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

Twitter

20
12
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
20
12