はじめに

Dockerを使用していると様々なイメージをビルドする機会があるのだが、数が多くなるにつれてディスク容量を圧迫していく。
そのため、一つ一つのイメージが小さくして使用量を減らしていくことを考え docker-slimでの軽量化を試みた。

環境

1
2
Mac OS Sonoma 14.5
Homebrew 4.3.1

docker-slimについて

docker-slimのインストール

1
brew install docker-slim
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
brew install docker-slim
==> Downloading https://ghcr.io/v2/homebrew/core/docker-slim/manifests/1.40.11
######################################################################################################################################################## 100.0%
==> Fetching docker-slim
==> Downloading https://ghcr.io/v2/homebrew/core/docker-slim/blobs/sha256:7b4aabd09784a08f074d2d2957142b6e9cb85d7197b3705d74bd30d9c9f01952
######################################################################################################################################################## 100.0%
==> Pouring docker-slim--1.40.11.sonoma.bottle.tar.gz
🍺  /usr/local/Cellar/docker-slim/1.40.11: 8 files, 66.3MB
==> Running `brew cleanup docker-slim`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

docker-slimを使う

今回は、以前作成した HugoブログをDockerで構築する2のイメージを軽量化する。

軽量化したいコンテナイメージ

1
2
3
4
~/workspace/blog-hugo$docker ps                                                                                                               [master|… ]
CONTAINER ID   IMAGE                COMMAND                   CREATED        STATUS          PORTS                    NAMES
b4b98591d947   blog-hugo-hugo       "hugo server --bind …"   2 months ago   Up 20 minutes   0.0.0.0:1313->1313/tcp   blog-hugo-hugo-1
082d153dbbe8   blog-hugo-tcardgen   "tail -f /dev/null"       2 months ago   Up 20 minutes                            blog-hugo-tcardgen-1

IDと現在のコンテナイメージサイズ

1
2
3
4
5
6
~/workspace/blog-hugo$docker images blog-hugo-hugo                                                                                            [master|… ]
REPOSITORY       TAG       IMAGE ID       CREATED        SIZE
blog-hugo-hugo   latest    03b7c5dd095c   3 months ago   2.29GB
~/workspace/blog-hugo$docker images blog-hugo-tcardgen                                                                                        [master|… ]
REPOSITORY           TAG       IMAGE ID       CREATED        SIZE
blog-hugo-tcardgen   latest    ee9b3f491796   3 months ago   1.52GB

slimコマンドを使ってイメージを作成

docker-slimslimコマンドを使うとのこと。
早速使ってみる。

1
slim

対話式になっており、Enterを押すと、hintが出てくる。

1
2
3
4
5
6
7
8
>>> 
>>> 
     exit             Exit app                                                                                                                            
     xray             Shows what's inside of your container image and reverse engineers its Dockerfile                                                    
     lint             Analyzes container instructions in Dockerfiles                                                                                      
     build            Analyzes, profiles and optimizes your container image auto-generating Seccomp and AppArmor security profiles                        
     merge            Merge two container images (optimized to merge minified images)                                                                     
     images           Get information about container images                                                            
1
build --target blog-hugo-hugo:latest --http-probe-off
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
>>> build --target blog-hugo-hugo:latest --http-probe-off 
cmd=build info=exec message='changing continue-after from probe to nothing because http-probe is disabled' 
cmd=build info=exec message='changing continue-after to enter' 
cmd=build state=started
cmd=build info=params rt.as.user='true' keep.perms='true' tags='' image-build-engine='internal' target.type='image' target.image='blog-hugo-hugo:latest' continue.mode='enter' 
cmd=build state=image.inspection.start
cmd=build info=image id='sha256:03b7c5dd095ceb7ca748993e3a6ddb5b0c4849319195c0f08afae634a9a9b239' size.bytes='2294014518' size.human='2.3 GB' 
cmd=build info=image.stack index='0' name='blog-hugo-hugo:latest' id='sha256:03b7c5dd095ceb7ca748993e3a6ddb5b0c4849319195c0f08afae634a9a9b239' 
cmd=build info=image.exposed_ports list='1313/tcp' 
cmd=build state=image.inspection.done
cmd=build state=container.inspection.start
cmd=build info=container name='slimk_25391_20240523054757' id='c19ea26452b2864f85c3876894c06ff7550dc78115dfea068433cbe679077a25' status='created' 
cmd=build info=container status='running' name='slimk_25391_20240523054757' id='c19ea26452b2864f85c3876894c06ff7550dc78115dfea068433cbe679077a25' 
cmd=build info=container message='obtained IP address' ip='172.17.0.2' 
cmd=build info=cmd.startmonitor status='sent' 
cmd=build info=event.startmonitor.done status='received' 
cmd=build info=container target.port.info='1313/tcp => 0.0.0.0:55010' message='YOU CAN USE THESE PORTS TO INTERACT WITH THE CONTAINER' name='slimk_25391_20240523054757' id='c19ea26452b2864f85c3876894c06ff7550dc78115dfea068433cbe679077a25' target.port.list='55010' 
cmd=build info=continue.after mode='enter' message='provide the expected input to allow the container inspector to continue its execution' 
cmd=build prompt='USER INPUT REQUIRED, PRESS <ENTER> WHEN YOU ARE DONE USING THE CONTAINER'

cmd=build state=container.inspection.finishing
cmd=build state=container.inspection.artifact.processing
cmd=build state=container.inspection.done
cmd=build state=building message="building optimized image" engine=internal 
cmd=build state=completed
cmd=build info=results status='MINIFIED' by='19.46X' size.original='2.3 GB' size.optimized='118 MB' 
cmd=build info=results has.data='true' image-build-engine='internal' image.name='blog-hugo-hugo.slim' image.size='118 MB' image.id='sha256:390353669295b8a8cb687adfa4c3e2bde0384157e6b8b4e762a0c42625d51c8a' image.digest='sha256:ec30d71099eca70b51264f6deb280cad53c155f2cd6f991b9454bce092a69bd5' 
cmd=build info=results artifacts.location='/tmp/slim-state/.slim-state/images/03b7c5dd095ceb7ca748993e3a6ddb5b0c4849319195c0f08afae634a9a9b239/artifacts' 
cmd=build info=results artifacts.report='creport.json' 
cmd=build info=results artifacts.dockerfile.reversed='Dockerfile.reversed' 
cmd=build info=results artifacts.seccomp='blog-hugo-hugo-seccomp.json' 
cmd=build info=results artifacts.apparmor='blog-hugo-hugo-apparmor-profile' 
cmd=build state=done
cmd=build info=commands message='use the xray command to learn more about the optimize image' 
cmd=build info=report file='slim.report.json' 

容量のチェック 2.29GB118MB に削減されている・・・!

1
2
~/workspace/blog-hugo$docker images | grep slim                                                                                               [master|… ]
blog-hugo-hugo.slim                      latest               390353669295   43 seconds ago   118MB

このイメージは使えるのか?

一旦コンテナを全部削除して。

1
2
3
~/workspace/blog-hugo$docker rm -f $(docker ps -aq)                                                                                           [master|… ]
b4b98591d947
082d153dbbe8

イメージを指定してコンテナを起動する。

compose.yml を書き換える。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# https://gohugo.io/installation/linux/#docker
services:
  hugo:
+   image: blog-hugo-hugo.slim 
-   build:
-     context: ./Docker/hugo
    volumes:
      - .:/src
    ports:
      - "1313:1313"
  tcardgen:
    build: 
      context: ./Docker/tcardgen
    volumes:
      - .:/blog

コンテナを立ち上げる。

1
make up

docker compose up -dに相当するコマンド

http://localhost:1313にアクセスすると問題なくいける。

参考

おわりに

肥大化しているDockerイメージが前々から気になっていたので、容量削減ができて良かった。
ただ、slimだと 削減したイメージのDockerfileの生成はしてくれないのだろうか。
そのあたりは今後確認していきたい。
あとは、Docker desktopの拡張機能にある、slim.aiというのもコンテナイメージの削減をしてくれる機能らしい。
これも調査をしてみて使ってみようと思う。