JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Docker >

Tagging images with Docker and Docker Compose

Author:JIYIK Last Updated:2025/03/25 Views:

When creating a Docker image, it is best practice to give it a descriptive and meaningful name. This process makes identifying and managing images more manageable, especially when dealing with many images.

This article discusses tagging images in Docker and Docker Compose.


Tagging images in Docker using Docker Build

Docker does not support tagging images in Dockerfile. Therefore, we use docker buildthe command to tag the image.

To name your Docker image, you can docker builduse the -t or --tag option when running the command.

Sample code:

$ docker build -t my-image .

In this command, the -t option specifies the name of the image as my-image. The name of the image must be unique within the registry where the image is stored.

If we try to build an image with an existing name, the build will fail with an error message.

When naming Docker images, it is best to use reverse domain name notation. This is because Docker automatically provides a hierarchical and unique namespace for images.

For example, if our company's domain name is example.com, you can name your Docker image com.example.my-image.


Tagging images in Docker Compose YAML files

We cannot tag images in Docker Compose like we do in vanilla Docker. However, the only difference is that we can tag images in the docker-compose.yml file.

Example of a YML file:

version: "3"
services:
    web:
        image: my-image:v3

Now, to build the image, we can use the same docker buildcommand and specify the name and version of the image using the -t or --tag option.

Sample code:

$ docker compose build -t my-image:v3 .

In this command, the -t option specifies the name and version of the image my-image:v3. The period at the end of the command .establishes the build context, which is the directory where we can locate the YAML file inside our system in Docker Compose.

When building an image, Docker Compose reads the YAML file and adds the specified name and version to the image using the image directive. We can then view a list of tagged images using the docker images command.

Sample code:

docker images

Output:

Copy code
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
my-image                     v3                 abc123              9 days ago          1GB

Overall, giving your Docker image a descriptive and meaningful name is critical for easy identification and management. Using reverse domain name notation and tags can help you create a hierarchical and unique namespace for your images and allow you to quickly identify different versions of an image.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Docker deploys nginx php application

Publish Date:2025/03/26 Views:131 Category:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

Publish Date:2025/03/26 Views:97 Category:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

Publish Date:2025/03/26 Views:125 Category:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

Publish Date:2025/03/26 Views:202 Category:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Enforce clean build of images in Docker

Publish Date:2025/03/26 Views:88 Category:Docker

This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho

Running a Docker instance from a Dockerfile

Publish Date:2025/03/26 Views:140 Category:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial