11
13

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 5 years have passed since last update.

続 multi stageなDockerfileで中間イメージにタグをつける

Posted at

multi stageなDockerfileで中間イメージにタグをつける - Qiita

以前書いた方法だと、tagをつける為にビルドする必要がありました。
これだと、ビルドステージが重い場合に時間がかかるので、別の方法でやることにしました。

結論

Tag Intermediate Build Stages (multi-stage build) - General Discussions - Docker Forums

↑ここに書かれているようにラベルをつけて、そのラベルを後から使います。

multi-stageなDockerfile

# build stage
FROM rust:1.26.1-slim-stretch AS build-stage
LABEL build_stage=true

RUN mkdir -p /app
WORKDIR /app

COPY . /app
RUN cargo build --release

# runtime stage
FROM debian:stretch-slim

COPY --from=build-stage /app/target/release/multi-stage /usr/bin/multi-stage

ENTRYPOINT ["/usr/bin/multi-stage"]

LABEL build_stage=true

これを後で使います。
ビルドしてみます。

❯ docker build -t multi .
Sending build context to Docker daemon  10.75kB
Step 1/9 : FROM rust:1.26.1-slim-stretch AS build-stage
 ---> b918abbd27e1
Step 2/9 : LABEL build_stage=true
 ---> Running in fecf0500419f
Removing intermediate container fecf0500419f
 ---> f1b19a013ed6
Step 3/9 : RUN mkdir -p /app
 ---> Running in c1c48548df47
Removing intermediate container c1c48548df47
 ---> 9d92571aafe5
Step 4/9 : WORKDIR /app
 ---> Running in 16ccc4b9ac47
Removing intermediate container 16ccc4b9ac47
 ---> d0b30d55a142
Step 5/9 : COPY . /app
 ---> b9f676a65421
Step 6/9 : RUN cargo build --release
 ---> Running in 81b1e65a3555
    Updating registry `https://github.com/rust-lang/crates.io-index`
 Downloading regex v1.0.0
 Downloading utf8-ranges v1.0.0
 Downloading thread_local v0.3.5
 Downloading aho-corasick v0.6.4
 Downloading regex-syntax v0.6.0
 Downloading memchr v2.0.1
 Downloading lazy_static v1.0.1
 Downloading unreachable v1.0.0
 Downloading void v1.0.2
 Downloading libc v0.2.42
 Downloading ucd-util v0.1.1
   Compiling libc v0.2.42
   Compiling void v1.0.2
   Compiling ucd-util v0.1.1
   Compiling regex v1.0.0
   Compiling lazy_static v1.0.1
   Compiling utf8-ranges v1.0.0
   Compiling unreachable v1.0.0
   Compiling thread_local v0.3.5
   Compiling regex-syntax v0.6.0
   Compiling memchr v2.0.1
   Compiling aho-corasick v0.6.4
   Compiling multi-stage v0.1.0 (file:///app)
    Finished release [optimized] target(s) in 21.8 secs
Removing intermediate container 81b1e65a3555
 ---> 525373ad0be0
Step 7/9 : FROM debian:stretch-slim
 ---> 26f0bb790e25
Step 8/9 : COPY --from=build-stage /app/target/release/multi-stage /usr/bin/multi-stage
 ---> 9430c7ee67c3
Step 9/9 : ENTRYPOINT ["/usr/bin/multi-stage"]
 ---> Running in 61660ed57369
Removing intermediate container 61660ed57369
 ---> 986818ec9ad3
Successfully built 986818ec9ad3
Successfully tagged multi:latest
❯ docker images
REPOSITORY                                 TAG                                IMAGE ID            CREATED              SIZE
multi                                      latest                             986818ec9ad3        About a minute ago   61.8MB
<none>                                     <none>                             525373ad0be0        About a minute ago   991MB

LABELを使ってFilterしてIDを取得してタグをつける

❯ docker images --filter 'label=build_stage=true'
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              525373ad0be0        3 minutes ago       991MB

これで、build-stageにしたイメージが取得できます。
では、これにオプションをつけてIDのみ取得し、そのIDにタグをつけます。

❯ docker tag $(docker images --filter 'label=build_stage=true' -q | head -n 1) multi:build-stage-cache
  • -q オプションでIDのみ取得
  • 複数ある可能性があるので、 head -n 1 で先頭の1個だけ取得
  • ↑のIDを使って、docker tagを実行
❯ docker images
REPOSITORY                                 TAG                                IMAGE ID            CREATED             SIZE
multi                                      latest                             986818ec9ad3        6 minutes ago       61.8MB
multi                                      build-stage-cache                  525373ad0be0        6 minutes ago       991MB

めでたくタグをつけることができました。
一回全部ビルドしてしまって、後からタグをつけた方が、ビルド回数が減って速くなりますね。

11
13
1

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
11
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?