21

When I build an image using docker build:

docker build .

I get an image with a random name.

How to create a docker image with a custom name?

I already know how to set the name in the Dockerfile, but I'm not sure how to use it in the build command.

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • I'm posting this following to [this meta post](https://meta.stackoverflow.com/questions/361666/technically-off-topic-answer-but-seems-to-be-helpful?noredirect=1#comment546609_361666). – Maroun Jan 09 '18 at 16:44
  • This is somewhat deceptive, as the answer to "how do set the image name in the Dockerfile?" is **you can't**. – TemporalWolf Jan 10 '18 at 23:07
  • Possible duplicate of [Name a docker at build and how to retrieve it](https://stackoverflow.com/questions/41621672/name-a-docker-at-build-and-how-to-retrieve-it) – TemporalWolf Jan 10 '18 at 23:51

1 Answers1

29

You can use the -t flag, as shown in the documentation (or run docker build --help to learn about the options you have).

You should do:

docker build -t my-image .

Now the image is created with the name my-image:

$ docker images
REPOSITORY                  TAG                 IMAGE ID            CREATED             SIZE
my-image                    latest              43070bef9dfa        2 minutes ago       464MB
Maroun
  • 94,125
  • 30
  • 188
  • 241