Build, tag, and publish an imageイメージをビルド、タグ付け、公開する
Explanation説明
In this guide, you will learn the following:このガイドでは、以下のことを学びます:
- Building images - the process of building an image based on a
Dockerfileイメージのビルド -Dockerfileに基づいてイメージをビルドするプロセス - Tagging images - the process of giving an image a name, which also determines where the image can be distributed画像にタグを付ける - 画像に名前を付けるプロセスであり、これにより画像の配布先が決まります。
- Publishing images - the process to distribute or share the newly created image using a container registry画像を公開する - 新しく作成された画像をコンテナレジストリを使用して配布または共有するプロセスです。
Building images画像のビルド
Most often, images are built using a Dockerfile. The most basic docker build command might look like the following:最も一般的に、画像はDockerfileを使用してビルドされます。最も基本的な docker build コマンドは次のようになります:
docker build .The final . in the command provides the path or URL to the build context. At this location, the builder will find the Dockerfile and other referenced files.コマンドの最後の . は、ビルドコンテキスト へのパスまたはURLを提供します。この場所で、ビルダーは Dockerfile と他の参照ファイルを見つけます。
When you run a build, the builder pulls the base image, if needed, and then runs the instructions specified in the Dockerfile.ビルドを実行すると、ビルダーは必要に応じてベースイメージをプルし、その後Dockerfileに指定された指示を実行します。
With the previous command, the image will have no name, but the output will provide the ID of the image. As an example, the previous command might produce the following output:前のコマンドでは、画像に名前が付けられませんが、出力には画像のIDが提供されます。例えば、前のコマンドは次のような出力を生成するかもしれません:
$ docker build .
[+] Building 3.5s (11/11) FINISHED docker:desktop-linux
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 308B 0.0s
=> [internal] load metadata for docker.io/library/python:3.12 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/6] FROM docker.io/library/python:3.12 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 123B 0.0s
=> [2/6] WORKDIR /usr/local/app 0.0s
=> [3/6] RUN useradd app 0.1s
=> [4/6] COPY ./requirements.txt ./requirements.txt 0.0s
=> [5/6] RUN pip install --no-cache-dir --upgrade -r requirements.txt 3.2s
=> [6/6] COPY ./app ./app 0.0s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:9924dfd9350407b3df01d1a0e1033b1e543523ce7d5d5e2c83a724480ebe8f00 0.0s
With the previous output, you could start a container by using the referenced image:前の出力を使用して、参照された画像を使ってコンテナを起動することができます:
docker run sha256:9924dfd9350407b3df01d1a0e1033b1e543523ce7d5d5e2c83a724480ebe8f00
That name certainly isn't memorable, which is where tagging becomes useful.その名前は確かに記憶に残りませんので、タグ付けが役立ちます。
Tagging images画像にタグを付ける
Tagging images is the method to provide an image with a memorable name. However, there is a structure to the name of an image. A full image name has the following structure:画像にタグを付けることは、画像に記憶に残る名前を付ける方法です。しかし、画像の名前には構造があります。完全な画像名は以下の構造を持っています:
[HOST[:PORT_NUMBER]/]PATH[:TAG]HOST: The optional registry hostname where the image is located. If no host is specified, Docker's public registry atdocker.iois used by default.HOST: 画像が存在するオプションのレジストリホスト名。ホストが指定されていない場合、デフォルトでDockerの公開レジストリdocker.ioが使用されます。PORT_NUMBER: The registry port number if a hostname is providedPORT_NUMBER: ホスト名が提供されている場合のレジストリポート番号PATH: The path of the image, consisting of slash-separated components. For Docker Hub, the format follows[NAMESPACE/]REPOSITORY, where namespace is either a user's or organization's name. If no namespace is specified,libraryis used, which is the namespace for Docker Official Images.PATH: スラッシュで区切られたコンポーネントからなる画像のパス。Docker Hubの場合、フォーマットは[NAMESPACE/]REPOSITORYに従い、namespaceはユーザーまたは組織の名前です。namespaceが指定されていない場合、libraryが使用され、これはDocker公式イメージのnamespaceです。TAG: A custom, human-readable identifier that's typically used to identify different versions or variants of an image. If no tag is specified,latestis used by default.TAG: 通常、画像の異なるバージョンやバリアントを識別するために使用されるカスタムの人間が読める識別子です。タグが指定されていない場合、デフォルトでlatestが使用されます。
Some examples of image names include:画像名のいくつかの例は次のとおりです:
nginx, equivalent todocker.io/library/nginx:latest: this pulls an image from thedocker.ioregistry, thelibrarynamespace, thenginximage repository, and thelatesttag.nginx、これはdocker.io/library/nginx:latestに相当します:これはdocker.ioレジストリから、librarynamespace、nginx画像リポジトリ、およびlatestタグをプルします。docker/welcome-to-docker, equivalent todocker.io/docker/welcome-to-docker:latest: this pulls an image from thedocker.ioregistry, thedockernamespace, thewelcome-to-dockerimage repository, and thelatesttagdocker/welcome-to-docker、これはdocker.io/docker/welcome-to-docker:latestに相当します:これはdocker.ioレジストリから、dockernamespace、welcome-to-docker画像リポジトリ、およびlatestタグをプルします。ghcr.io/dockersamples/example-voting-app-vote:pr-311: this pulls an image from the GitHub Container Registry, thedockersamplesnamespace, theexample-voting-app-voteimage repository, and thepr-311tagghcr.io/dockersamples/example-voting-app-vote:pr-311:これはGitHub Container Registryから、dockersamplesnamespace、example-voting-app-vote画像リポジトリ、およびpr-311タグをプルします。
To tag an image during a build, add the -t or --tag flag:ビルド中に画像にタグを付けるには、-tまたは--tagフラグを追加します:
docker build -t my-username/my-image .
If you've already built an image, you can add another tag to the image by using the docker image tag command:すでにイメージをビルドしている場合は、docker image tag コマンドを使用して、イメージに別のタグを追加できます。
docker image tag my-username/my-image another-username/another-image:v1
Publishing imagesイメージの公開
Once you have an image built and tagged, you're ready to push it to a registry. To do so, use the docker push command:イメージがビルドされ、タグ付けされたら、それをレジストリにプッシュする準備が整います。そのためには、docker push コマンドを使用します。
docker push my-username/my-image
Within a few seconds, all of the layers for your image will be pushed to the registry.数秒以内に、イメージのすべてのレイヤーがレジストリにプッシュされます。
Requiring authentication認証が必要
Before you're able to push an image to a repository, you will need to be authenticated. To do so, simply use the docker login command.リポジトリにイメージをプッシュする前に、認証される必要があります。 そのためには、単にdocker login コマンドを使用してください。
Try it out試してみる
In this hands-on guide, you will build a simple image using a provided Dockerfile and push it to Docker Hub.このハンズオンガイドでは、提供されたDockerfileを使用してシンプルなイメージをビルドし、それをDocker Hubにプッシュします。
Set upセットアップ
Get the sample application.サンプルアプリケーションを取得します。
If you have Git, you can clone the repository for the sample application. Otherwise, you can download the sample application. Choose one of the following options.Gitがある場合は、サンプルアプリケーションのリポジトリをクローンできます。そうでない場合は、サンプルアプリケーションをダウンロードできます。次のオプションのいずれかを選択してください。
Use the following command in a terminal to clone the sample application repository.ターミナルで次のコマンドを使用して、サンプルアプリケーションのリポジトリをクローンします。
$ git clone https://github.com/docker/getting-started-todo-appDownload the source and extract it.ソースをダウンロードして解凍します。
Download and install Docker Desktop.Docker Desktopをダウンロードしてインストールします。
If you don't have a Docker account yet, create one now. Once you've done that, sign in to Docker Desktop using that account.まだDockerアカウントを持っていない場合は、今すぐ作成してください。それが完了したら、そのアカウントを使用してDocker Desktopにサインインします。
Build an imageイメージをビルド
Now that you have a repository on Docker Hub, it's time for you to build an image and push it to the repository.Docker Hubにリポジトリができたので、イメージをビルドしてリポジトリにプッシュする時が来ました。
Using a terminal in the root of the sample app repository, run the following command. Replace
YOUR_DOCKER_USERNAMEwith your Docker Hub username:サンプルアプリのリポジトリのルートでターミナルを使用して、次のコマンドを実行します。YOUR_DOCKER_USERNAMEをあなたのDocker Hubのユーザー名に置き換えてください。$ docker build -t YOUR_DOCKER_USERNAME/concepts-build-image-demo .As an example, if your username is
mobywhale, you would run the command:例として、あなたのユーザー名がmobywhaleの場合、次のコマンドを実行します:$ docker build -t mobywhale/concepts-build-image-demo .Once the build has completed, you can view the image by using the following command:ビルドが完了したら、次のコマンドを使用してイメージを表示できます:
$ docker image lsThe command will produce output similar to the following:コマンドは次のような出力を生成します:
REPOSITORY TAG IMAGE ID CREATED SIZE mobywhale/concepts-build-image-demo latest 746c7e06537f 24 seconds ago 354MBYou can actually view the history (or how the image was created) by using the docker image history command:実際に、docker image history コマンドを使用して、履歴(またはイメージがどのように作成されたか)を表示できます:
$ docker image history mobywhale/concepts-build-image-demoYou'll then see output similar to the following:次のような出力が表示されます:
IMAGE CREATED CREATED BY SIZE COMMENT f279389d5f01 8 seconds ago CMD ["node" "./src/index.js"] 0B buildkit.dockerfile.v0 <missing> 8 seconds ago EXPOSE map[3000/tcp:{}] 0B buildkit.dockerfile.v0 <missing> 8 seconds ago WORKDIR /app 8.19kB buildkit.dockerfile.v0 <missing> 4 days ago /bin/sh -c #(nop) CMD ["node"] 0B <missing> 4 days ago /bin/sh -c #(nop) ENTRYPOINT ["docker-entry… 0B <missing> 4 days ago /bin/sh -c #(nop) COPY file:4d192565a7220e13… 20.5kB <missing> 4 days ago /bin/sh -c apk add --no-cache --virtual .bui… 7.92MB <missing> 4 days ago /bin/sh -c #(nop) ENV YARN_VERSION=1.22.19 0B <missing> 4 days ago /bin/sh -c addgroup -g 1000 node && addu… 126MB <missing> 4 days ago /bin/sh -c #(nop) ENV NODE_VERSION=20.12.0 0B <missing> 2 months ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B <missing> 2 months ago /bin/sh -c #(nop) ADD file:d0764a717d1e9d0af… 8.42MBThis output shows the layers of the image, highlighting the layers you added and those that were inherited from your base image.この出力はイメージのレイヤーを示しており、追加したレイヤーとベースイメージから継承したレイヤーを強調表示しています。
Push the imageイメージをプッシュする
Now that you have an image built, it's time to push the image to a registry.イメージが構築されたので、イメージをレジストリにプッシュする時が来ました。
Push the image using the docker push command:次のdocker push コマンドを使用してイメージをプッシュします:
$ docker push YOUR_DOCKER_USERNAME/concepts-build-image-demoIf you receive a
requested access to the resource is denied, make sure you are both logged in and that your Docker username is correct in the image tag.requested access to the resource is deniedというメッセージが表示された場合は、ログインしていることと、イメージタグに正しいDockerユーザー名が設定されていることを確認してください。After a moment, your image should be pushed to Docker Hub.しばらくすると、あなたのイメージはDocker Hubにプッシュされるはずです。
Additional resources追加リソース
To learn more about building, tagging, and publishing images, visit the following resources:ビルド、タグ付け、イメージの公開について詳しく学ぶには、以下のリソースを訪れてください:
- What is a build context?ビルドコンテキストとは何ですか?
- docker build referencedocker build リファレンス
- docker image tag referencedocker image tag リファレンス
- docker push referencedocker push リファレンス
- What is a registry?レジストリとは何ですか?
Next steps次のステップ
Now that you have learned about building and publishing images, it's time to learn how to speed up the build process using the Docker build cache.イメージのビルドと公開について学んだので、次はDockerビルドキャッシュを使用してビルドプロセスを高速化する方法を学ぶ時です。
Using the build cache