使用 Docker 和 Docker Compose 标记图像
创建 Docker 镜像时,最佳做法是为其指定一个描述性且有意义的名称。 此过程使识别和管理图像更易于管理,尤其是在处理许多图像时。
本文将讨论在 Docker 和 Docker Compose 中标记图像。
使用 Docker Build 在 Docker 中标记图像
Docker 不支持在 Dockerfile 中标记图像。 因此,我们使用 docker build
命令来标记图像。
要命名 Docker 映像,您可以在运行 docker build
命令时使用 -t 或 --tag 选项。
示例代码:
$ docker build -t my-image .
在此命令中,**-t** 选项将图像的名称指定为** my-image**。 图像的名称在存储图像的注册表中必须是唯一的。
如果我们尝试使用现有名称构建图像,构建将失败并显示错误消息。
命名 Docker 镜像时,最好使用反向域名表示法来命名。 这是因为 Docker 自动为镜像提供了一个层次化且唯一的命名空间。
比如我们公司的域名是example.com,你可以将你的Docker镜像命名为com.example.my-image。
在 Docker Compose YAML 文件中标记图像
我们无法像 vanilla Docker 那样在 Docker Compose 中标记图像。 然而,唯一的区别是我们可以在 docker-compose.yml 文件中标记图像。
YML 文件示例:
version: "3"
services:
web:
image: my-image:v3
现在,要构建图像,我们可以使用相同的 docker build
命令并使用 -t 或 --tag 选项指定图像的名称和版本。
示例代码:
$ docker compose build -t my-image:v3 .
在此命令中,**-t** 选项将映像的名称和版本指定为 my-image:v3
。 命令末尾的句点.
建立构建上下文,构建上下文是我们在Docker Compose中可以在系统内部定位YAML文件的目录。
在构建镜像时,Docker Compose 会读取 YAML 文件,并使用 image 指令将指定的名称和版本添加到镜像中。 然后我们可以使用 docker images 命令查看标记图像的列表。
示例代码:
docker images
输出:
Copy code
REPOSITORY TAG IMAGE ID CREATED SIZE
my-image v3 abc123 9 days ago 1GB
总体而言,为 Docker 镜像提供一个描述性且有意义的名称对于便于识别和管理至关重要。 使用反向域名表示法和标签可以帮助您为图像创建分层且唯一的命名空间,并允许您快速识别图像的不同版本。
相关文章
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
发布时间:2025/03/26 浏览次数:165 分类:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
发布时间:2025/03/26 浏览次数:131 分类: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
发布时间:2025/03/26 浏览次数:107 分类: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
发布时间:2025/03/26 浏览次数:97 分类: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
发布时间:2025/03/26 浏览次数:124 分类: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
发布时间:2025/03/26 浏览次数:202 分类: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
发布时间:2025/03/26 浏览次数:87 分类: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
发布时间:2025/03/26 浏览次数:140 分类: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