如何在 Dockerfile 中设置镜像名称? [英] How to set image name in Dockerfile?

查看:262
本文介绍了如何在 Dockerfile 中设置镜像名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以在构建自定义图像时设置图像名称,如下所示:

You can set image name when building a custom image, like this:

docker build -t dude/man:v2 . # Will be named dude/man:v2

有没有办法在Dockerfile中定义镜像的名字,所以docker build命令中就不用提了?

Is there a way to define the name of the image in Dockerfile, so I don't have to mention it in the docker build command?

推荐答案

Dockerfile 中不支持对图像进行标记.这需要在您的构建命令中完成.作为一种解决方法,您可以使用标识目标映像名称的 docker-compose.yml 进行构建,然后运行 ​​docker-compose build.一个示例 docker-compose.yml 看起来像

Tagging of the image isn't supported inside the Dockerfile. This needs to be done in your build command. As a workaround, you can do the build with a docker-compose.yml that identifies the target image name and then run a docker-compose build. A sample docker-compose.yml would look like

version: '2'

services:
  man:
    build: .
    image: dude/man:v2

也就是说,有人反对使用 compose 进行构建,因为它不适用于 swarm 模式部署.因此,您将重新运行您在问题中给出的命令:

That said, there's a push against doing the build with compose since that doesn't work with swarm mode deploys. So you're back to running the command as you've given in your question:

docker build -t dude/man:v2 .

就个人而言,我倾向于在我的文件夹 (build.sh) 中使用一个小的 shell 脚本进行构建,该脚本传递任何参数并在其中包含图像的名称以节省输入.对于生产,构建由 ci/cd 服务器处理,该服务器在管道脚本中具有图像名称.

Personally, I tend to build with a small shell script in my folder (build.sh) which passes any args and includes the name of the image there to save typing. And for production, the build is handled by a ci/cd server that has the image name inside the pipeline script.

这篇关于如何在 Dockerfile 中设置镜像名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆