670

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?

2
  • 19
    For anyone curious, using FROM... AS name does NOT work Commented Jun 21, 2019 at 3:12
  • Workaround using Docker and a Makefile; and, an alternative with buildah stackoverflow.com/a/75538276/124486 Hope you find this answer a bit more useful then telling you that can't or telling you the same command as in the question. Commented Feb 22, 2023 at 21:56

7 Answers 7

576

Workaround using docker-compose

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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...
103

Workaround using docker-compose

Updated: "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

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 

3 Comments

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"
@SeanMcCarthy With your current score you could edit the answer and put the explanation into it.
good idea. i did it.
27

My Dockerfile alone solution is adding a shebang line:

#!/usr/bin/env -S docker build . --tag=dude/man:v2 --network=host --file FROM ubuntu:22.04 # ... 

Then chmod +x Dockerfile and ./Dockerfile is to go. I even add more docker build command line arguments like specifying a host network.

NOTE: env with -S/--split-string support is only available for newer coreutils versions.

1 Comment

This is an elegant solution, but comes with a small caveat: Because Docker sees the shebang line as a comment, you cannot use parser directives in such a file (such directives must be at the very start of the file, and not even comments may precede them).
8

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 .

4 Comments

Downvoting because naming the image in the build command (via --tag or -t) is exactly what the OP is trying to avoid. Using a different name for the Dockerfile doesn't change that.
Please, share reference.
Not sure I understand what you mean. The OP is literally stating "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?" in the question. However, your answer does exactly that: defining the name of the image in the docker build command. So it's not answering the question. I'm not sure what reference you are looking for.
I may look into the question editing history. I don't think I misunderstood the question when I put it. The question may be changed after I share my method.
6

Workaround using Docker (and a Makefile)

Generally in Docker you can't say what you want the image to be tagged as in the Dockerfile. So what you do is

  • Create a Dockerfile
  • Create a Makefile
    .PHONY: all all: docker build -t image_name . 
  • Use make instead of invoking docker build directly

Or, use buildah

But here is a better idea... Don't build images with Docker! Instead build them with buildah, the new build tool provided by the podman crew which uses shell (or any language), allows building in the cloud easily (without using a different project like kaniko), and allows rootless building of images! At the end of the build script just save the image inside with buildah commit. Here is what it looks like.

#!/bin/sh # Create a new offline container from the `alpine:3` image, return the id. ctr=$(buildah from "alpine:3") # Create a new mount, return the path on the host. mnt=$(buildah mount "$ctr") # Copy files to the mount cp -Rv files/* "$mnt/" # Do some things or whatever buildah config --author "Evan Carroll" --env "FOO=bar" -- "$ctr" # Run a script inside the container buildah run "$ctr" -- /bin/sh <<EOF echo "This is just a regular shell script" echo "Do all the things." EOF # Another one, same layer though buildah run "$ctr" -- /bin/sh <<EOF echo "Another one!" echo "No excess layers created as with RUN." EOF # Commit this container as "myImageName" buildah commit -- "$ctr" "myImageName" 

Now you don't have to hack around with a Makefile. You have one shell script that does everything, and is far more powerful than a Dockerfile.


Side note, buildah can also build from Dockerfiles (using buildah bud), but this short coming is with the Dockerfile. So that won't help.

Comments

0

You can combine some of the suggestions made in other answers.

Here's an example docker-compose.yaml:

Pick and choose the bits you want.

version: '3' name: webscraper-project-name services: webscraper: container_name: webscraper-container-name build: . image: example/webscraper-image:latest volumes: - /webscraper-volume restart: unless-stopped 

Example Dockerfile. (Less relevant to you, probably)

from python 3.12-bookworm workdir /webscraper copy . . entrypoint ["python3", "webscraper_main.py"] 

Comments

-3

Go to terminal and run the below mentioned command :

docker build -t imagenameHere:tagHere .

1 Comment

That's what the OP wants to avoid.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.