LoginSignup
1
2

DockerのPython公式イメージにおけるタグの指定について

Last updated at Posted at 2024-01-08

1. はじめに

私が行った研究において,Pythonイメージのタグについて多くのことを知ることができたので,今回得た知識をまとめてみました.なお,自分でタグをつけるという使い方もありますが,今回はDocker Hubからイメージをpullしてくる際に指定できるタグについて触れていこうと思います.

2. タグとは

Docker Hubからイメージをpullしてくる際に指定できるタグには,主にバージョンを指定するという役割があります.タグを指定するときは,以下のように指定します.

Pythonイメージのバージョン3.9をpullする場合
docker image pull python:3.9

コロン(:)以降がタグの内容となっています.
本記事では,Pythonのバージョンを3.9または最新バージョンを用いていきます.

3. タグの種類

タグでは主にバージョンを指定しますが,そうでない場合もあります.それらは厳密にはバージョンを指定するものではあるのですが,直接数値を指定するものではないため,それぞれ固有の指定の仕方をする必要があります.

3-1. latest

タグにlatestと指定すると,そのイメージの最新バージョンがpullされます.

python:latest
$ docker image pull python:latest

3-2. buster

タグにbusterと指定すると,ベースイメージがDebianのバージョン10であるPythonイメージがpullされます.以下のようなタグを指定すると,pythonバージョンが3.9で,Debianバージョンが10のPythonイメージがpullされます.なお,busterというのは,Debianのバージョン10のコードネームで,Debianには,バージョンごとにコードネームが与えられています.

python:3.9-buster
$ docker image pull python:3.9-buster

3-3. bullseye

タグにbullseyeと指定すると,ベースイメージがDebianのバージョン11であるPythonイメージがpullされます.以下のようなタグを指定すると,pythonバージョンが3.9で,Debianバージョンが11のPythonイメージがpullされます.

python:3.9-buster
$ docker image pull python:3.9-bullseye

3-4. bookworm

bookwormは,2023年12月26日時点のDebianの最新バージョンです.タグにbookwormと指定すると,ベースイメージがDebianのバージョン12であるPythonイメージがpullされます.以下のようなタグを指定すると,pythonバージョンが3.9で,Debianバージョンが12のPythonイメージがpullされます.

python:3.9-buster
$ docker image pull python:3.9-bullseye

3-5. slim

タグにslimと指定すると,よく用いられるライブラリ以外を除外した,サイズが削減されたPythonイメージがpullされます.指定は以下のように行います.

python:3.9-slim
$ docker image pull python:3.9-slim

また,以下のように詳細にタグを指定することもできます.

python:3.9-slim-bookworm
$ docker image pull python:3.9-slim-bookworm

このように指定すると,pythonのバージョン3.9の,ベースイメージがbookwormの,slimイメージがpullされます.

3-6. alpine

タグにalpineと指定すると,Alpine LinuxをベースイメージとしたPythonイメージがpullされます.指定は以下のように行います.

python:3.9-alpine
$ docker image pull python:3.9-alpine

このイメージは,slim以上にライブラリが除外されており,サイズがかなり小さくなっています.しかし,pythonとの相性が悪く,速度が遅くなってしまったり,必要なライブラリを入れる手間が大きくなってしまったりと,デメリットも多くあります.
また,alpineにもバージョンがあり,これを指定することもできます.

python:3.9-alpine3.19
$ docker image pull python:3.9-alpine3.19

3-7. windowsservercore

タグにwindowsservercoreと指定すると,windows環境用のPythonイメージがpullされます.指定は以下のように行います.

python:3.9-windowsservercore
$ docker image pull python:3.9-windowsservercore

4. タグを指定しない場合

これまで,様々なタグの指定の仕方を紹介してきましたが,逆に,タグを指定しない場合はどのようになるのでしょうか.これは,実行ログを見ると分かります.

python
$ docker image pull python
Using default tag: latest
latest: Pulling from library/python
bc0734b949dc: Pull complete
b5de22c0f5cd: Pull complete
917ee5330e73: Pull complete
b43bd898d5fb: Pull complete
7fad4bffde24: Pull complete
d685eb68699f: Pull complete
107007f161d0: Pull complete
02b85463d724: Pull complete
Digest: sha256:3733015cdd1bd7d9a0b9fe21a925b608de82131aa4f3d397e465a1fcb545d36f
Status: Downloaded newer image for python:latest
docker.io/library/python:latest

実行開始直後に,Using default tag: latestと表示されています.つまり,タグを指定しない=latest,最新版が指定されるということです.実は,これはDebianのバージョンも同じようになっています.
python:3.9イメージと,python:3.9-bookwormイメージのレイヤを比較してみます.

python:3.9
$ docker image pull python:3.9
3.9: Pulling from library/python
bc0734b949dc: Pull complete
b5de22c0f5cd: Pull complete
917ee5330e73: Pull complete
b43bd898d5fb: Pull complete
7fad4bffde24: Pull complete
cd0903c43c21: Pull complete
b85288e0cb16: Pull complete
7a97f6368ea6: Pull complete
Digest: sha256:30678bb79d9eeaf98ec0ce83cdcd4d6f5301484ef86873a711e69df2ca77e8ac
Status: Downloaded newer image for python:3.9
docker.io/library/python:3.9
python:3.9-bookworm
$ docker image pull python:3.9-bookworm
3.9-bookworm: Pulling from library/python
bc0734b949dc: Pull complete
b5de22c0f5cd: Pull complete
917ee5330e73: Pull complete
b43bd898d5fb: Pull complete
7fad4bffde24: Pull complete
cd0903c43c21: Pull complete
b85288e0cb16: Pull complete
7a97f6368ea6: Pull complete
Digest: sha256:30678bb79d9eeaf98ec0ce83cdcd4d6f5301484ef86873a711e69df2ca77e8ac
Status: Downloaded newer image for python:3.9-bookworm
docker.io/library/python:3.9-bookworm

3~10行目のコロンより左側の文字列をそれぞれ比べてみると,同じ文字列が表示されています.これは,各レイヤーが共通のものであることを意味しています.この結果から,python:3.9と指定してpullを実行した場合,python:3.9-bookworm,つまりDibianの最新版をベースイメージに使ったpython:3.9イメージがpullされます.

5. 終わりに

本記事では,私が研究を通じて得たpythonイメージのタグについての知識をまとめてみました.この内容が何かの役に立てたのなら幸いです.

1
2
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
1
2