gvlasov
gvlasov

Reputation: 20005

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?

Upvotes: 659

Views: 863237

Answers (7)

user2138149
user2138149

Reputation: 16484

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"]

Upvotes: 0

David Dehghan
David Dehghan

Reputation: 24775

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

Upvotes: 100

ozz
ozz

Reputation: 23

Go to terminal and run the below mentioned command :

docker build -t imagenameHere:tagHere .

Upvotes: -3

BMitch
BMitch

Reputation: 263469

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.

Upvotes: 567

Evan Carroll
Evan Carroll

Reputation: 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.

Upvotes: 4

tsingakbar
tsingakbar

Reputation: 456

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.

Upvotes: 24

Cloud Cho
Cloud Cho

Reputation: 1774

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 .

Upvotes: 9

Related Questions