Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 338
gvlasov Avatar asked Aug 16 '16 23:08

gvlasov


People also ask

How do I name a docker image?

You can rename your docker image by docker tag command. Use the below given command to do that. To rename docker container, use the rename sub-command as shown, in the following example, we renaming the container discourse_app to a new name disc_app.

How do I tag an image in Dockerfile?

You can use build.sh script, which contains like this: #!/usr/bin/env bash if [ $# -eq 0 ] then tag='latest' else tag=$1 fi docker build -t project:$tag . Run ./build.sh for creating image project:latest or run ./build.sh your_tag to specify image tag.

Can you change the name of a docker image?

Your answerYou can rename your docker image by docker tag command.


2 Answers

How to build an image with custom name without using yml file:

docker build -t image_name . 

How to run a container with custom name:

docker run -d --name container_name image_name 
like image 139
salehinejad Avatar answered Sep 21 '22 12:09

salehinejad


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.

like image 22
BMitch Avatar answered Sep 22 '22 12:09

BMitch