In this guide, you will learn the following:
Building images - the process of building an image based on a DockerfileTagging images - the process of giving an image a name, which also determines where the image can be distributedPublishing images - the process to distribute or share the newly created image using a container registryBuilding imagesMost often, images are built using a Dockerfile. The most basic docker build command might look like the following:
docker build .The final . in the command provides the path or URL to thebuild context. At this location, the builder will find the Dockerfile and other referenced files.
When you run a build, the builder pulls the base image, if needed, and then runs the instructions specified in the 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:
$ docker build .[+] Building 3.5s (11/11) FINISHED docker:desktop-linux => [internal] load build definition from Dockerfile0.0s => => transferring dockerfile: 308B0.0s => [internal] load metadata for docker.io/library/python:3.12 0.0s => [internal] load .dockerignore0.0s => => transferring context: 2B 0.0s => [1/6] FROM docker.io/library/python:3.120.0s => [internal] load build context0.0s => => transferring context: 123B0.0s => [2/6] WORKDIR /usr/local/app0.0s => [3/6] RUN useradd app0.1s => [4/6] COPY ./requirements.txt ./requirements.txt0.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:9924dfd9350407b3df01d1a0e1033b1e543523ce7d5d5e2c83a724480ebe8f000.0sWith the previous output, you could start a container by using the referenced image:
docker run sha256:9924dfd9350407b3df01d1a0e1033b1e543523ce7d5d5e2c83a724480ebe8f00That name certainly isn't memorable, which is where tagging becomes useful.
Tagging imagesTagging 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 at docker.io is used by default.PORT_NUMBER: The registry port number if a hostname is providedPATH: 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, library is used, which is the namespace for Docker Official Images.TAG: A custom, human-readable identifier that's typically used to identify different versions or variants of an image. If no tag is specified, latest is used by default.Some examples of image names include:
nginx, equivalent to docker.io/library/nginx:latest: this pulls an image from the docker.io registry, the library namespace, the nginx image repository, and the latest tag.docker/welcome-to-docker, equivalent to docker.io/docker/welcome-to-docker:latest: this pulls an image from the docker.io registry, the docker namespace, the welcome-to-docker image repository, and the latest tagghcr.io/dockersamples/example-voting-app-vote:pr-311: this pulls an image from the GitHub Container Registry, the dockersamples namespace, the example-voting-app-vote image repository, and the pr-311 tagTo tag an image during a build, add the -t or --tag flag:
docker build -t my-username/my-image .If you've already built an image, you can add another tag to the image by using thedocker image tag command:
docker image tag my-username/my-image another-username/another-image:v1Publishing imagesOnce you have an image built and tagged, you're ready to push it to a registry. To do so, use thedocker push command:
docker push my-username/my-imageWithin 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 thedocker login command.
Try it outIn this hands-on guide, you will build a simple image using a provided Dockerfile and push it to Docker Hub.
Set upGet 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.
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 the sourceDownload and install 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.
Build an imageNow that you have a repository on Docker Hub, it's time for you to build an image and push it to the repository.
Using a terminal in the root of the sample app repository, run the following command. Replace YOUR_DOCKER_USERNAME with your Docker Hub username:
$ docker build -t /concepts-build-image-demo .As an example, if your username is mobywhale, you would run the command:
$ 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 TAGIMAGE IDCREATED SIZEmobywhale/concepts-build-image-demolatest746c7e06537f24 seconds ago354MBYou can actually view the history (or how the image was created) by using thedocker image history command:
$ docker image history mobywhale/concepts-build-image-demoYou'll then see output similar to the following:
IMAGE CREATED CREATED BY SIZE COMMENTf279389d5f018 seconds agoCMD ["node" "./src/index.js"]0Bbuildkit.dockerfile.v0 8 seconds agoEXPOSE map[3000/tcp:{}] 0Bbuildkit.dockerfile.v08 seconds agoWORKDIR /app8.19kBbuildkit.dockerfile.v0 4 days ago /bin/sh -c #(nop) CMD ["node"] 0B 4 days ago /bin/sh -c #(nop) ENTRYPOINT ["docker-entry…0B 4 days ago /bin/sh -c #(nop) COPY file:4d192565a7220e13…20.5kB 4 days ago /bin/sh -c apk add --no-cache --virtual .bui…7.92MB 4 days ago /bin/sh -c #(nop) ENV YARN_VERSION=1.22.19 0B 4 days ago /bin/sh -c addgroup -g 1000 node && addu…126MB 4 days ago /bin/sh -c #(nop) ENV NODE_VERSION=20.12.0 0B 2 months ago/bin/sh -c #(nop) CMD ["/bin/sh"] 0B 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 imageNow that you have an image built, it's time to push the image to a registry.
Push the image using thedocker push command:
$ docker push /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.
After a moment, your image should be pushed to Docker Hub.
Additional resourcesTo learn more about building, tagging, and publishing images, visit the following resources:
What is a build context?docker build referencedocker image tag referencedocker push referenceWhat is a registry?Next stepsNow 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.
Using the build cache