Tagging images with Docker and Docker Compose
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 build
the command to tag the image.
To name your Docker image, you can docker build
use 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 build
command 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:https://www.jiyik.com/en/xwzj/opersys_10080.html
Related Articles
Creating a database user with Docker Postgres
Publish Date:2025/03/24 Views:166 Category:Docker
-
When developing applications, we usually use database management systems such as PostgreSQL, MySQL, MongoDB, etc. to record application data. Docker helps us run an instance of these application database management systems. This helps save
Adding unsafe registry keys in Docker
Publish Date:2025/03/24 Views:130 Category:Docker
-
While it is highly recommended to secure your registry using a Transport Layer Security (TLS) certificate issued by a known Certificate Authority (CA), we have the option of using our insecure registry over an unencrypted Hypertext Transfer
List all images in Docker Registry V2
Publish Date:2025/03/24 Views:131 Category:Docker
-
After several iterations, Docker Registry was upgraded from version 1 to version 2. Especially being new, some commands needed to be included or adequately documented on its official documentation website. An example is getting a list of im
Executing multiple commands in Docker-Compose
Publish Date:2025/03/24 Views:152 Category:Docker
-
Docker makes it easier for developers to build, test, and deploy applications without worrying about dependencies by packaging them in standardized units called containers. Docker-compose is an advanced, must-have tool for managing multi-co
View logs for a specific Docker Compose service
Publish Date:2025/03/24 Views:60 Category:Docker
-
When using docker-compose up, we can see the logs of all containers in the YAML file; however, if we specify a specific container service, the output will not show any service dependencies in the log. Therefore, this article will explore ho
Differences between Docker and Docker Compose
Publish Date:2025/03/24 Views:147 Category:Docker
-
Both docker and docker compose docker run Docker containers. The two Docker constructs are comparable, but that's about it. This article will discuss docker compose the main differences between docker and . Differences between Docker and Do
Recreate the container from the new image using Docker-Compose
Publish Date:2025/03/24 Views:178 Category:Docker
-
As we develop our applications, we often make changes or add more features to make the application more effective for the different users who interact with the application. When using Docker, we need to ensure that the changes or features w
Exposing multiple ports in a Docker container
Publish Date:2025/03/24 Views:187 Category:Docker
-
There are different types of communication on the Internet, the most common ones include file transfers, sending emails, and serving web pages. To make this communication possible, we make use of port numbers that help identify the type of
Using Docker network host commands
Publish Date:2025/03/24 Views:51 Category:Docker
-
Docker containers work by leveraging network drivers that are created during the installation of Docker. The default drivers available to us include bridge and host networking. When we create containers without specifying a network, they ar