592

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
  • 16
    For anyone curious, using FROM... AS name does NOT work
    – Rufus
    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. Feb 22 at 21:56

5 Answers 5

512

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.

1
  • 8
    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 4, 2022 at 9:09
85

Workaround using docker-compose

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
1
  • 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" Sep 24, 2019 at 13:53
8

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.

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 .

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.