600

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?

Martin Schröder
  • 4,176
  • 7
  • 47
  • 81
gvlasov
  • 18,638
  • 21
  • 74
  • 110
  • 17
    For anyone curious, using [FROM... AS name](https://docs.docker.com/engine/reference/builder/#from) does NOT work – Rufus Jun 21 '19 at 03:12
  • Workaround using Docker and a Makefile; and, an alternative with `buildah` https://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. – Evan Carroll Feb 22 '23 at 21:56

6 Answers6

518

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.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
BMitch
  • 231,797
  • 42
  • 475
  • 450
  • 9
    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... – F1Linux Apr 04 '22 at 09:09
86

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
David Dehghan
  • 22,159
  • 10
  • 107
  • 95
  • 10
    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" – Sean McCarthy Sep 24 '19 at 13:53
  • 1
    @SeanMcCarthy With your current score you could edit the answer and put the explanation into it. – goulashsoup Aug 17 '23 at 17:07
  • good idea. i did it. – David Dehghan Aug 19 '23 at 00:18
12

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.

tsingakbar
  • 197
  • 1
  • 6
5

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 .

Cloud Cho
  • 1,594
  • 19
  • 22
0

Go to terminal and run the below mentioned command :

docker build -t imagenameHere:tagHere .

ozz
  • 3
  • 3
-1

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.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468