ChatGPT解决这个技术问题 Extra ChatGPT

How to set image name in 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

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?

For anyone curious, using FROM... AS name does NOT work

s
salehinejad

How to build an image with custom name without using yml file:

docker build -t image_name .

How to run a container with custom name:

docker run -d --name container_name image_name

I'm not going to comment further but the question was "How to set image name in Dockerfile?" Not "How to set docker image name?" That answer was in the question itself.
I've come back to this a couple times, and this answer has helped, despite being technically off-topic
If you are consistent if putting the Dockerfile in a directory with the same name as you want for your image, you can use docker build -t $(basename $PWD) . as your build command. Then you can use CTRL-R search from "build" to find and reuse the command and never have to edit it. You can also make it an alias if you like.
Thank you anyway for this answer. i was spending few hours to get this over internet . I couldn't get a proper answer, one who give name to his image can't go further without this if he need name to his container as well.
B
BMitch

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

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 .

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.


Upvoting this because this actually answers the question in the first sentence "Tagging of the image isn't supported inside the Dockerfile". Only once you know this are you interested in considering workarounds...
S
Stanley.Goldman

Here is another version if you have to reference a specific docker file:

version: "3"
services:
  nginx:
    container_name: nginx
    build:
      context: ../..
      dockerfile: ./docker/nginx/Dockerfile
    image: my_nginx:latest

Then you just run

docker-compose build

I like it that this answer shows the difference between the container_name and the image name:tag. If only there was a little more explanation of what's going on: "container_name" names the container that's ultimately spun up from the image. "image" names and tags the image created, from which the container is built. As others have mentioned, one cannot specify the image name from the Dockerfile, as the OP asked, so we use the docker-compose.yml file instead, and run it with "docker-compose up -d --build"
C
Cloud Cho

With a specific Dockerfile you could try:
docker build --tag <Docker Image name> --file <specific Dockerfile> .
for example
docker build --tag second --file Dockerfile_Second .


V
Varun Kumar

If you want to give a name to the docker images after building it just build it again but give a name this time It works great

docker build -t name_of_image .