How to set image name in Docker file?

You can set the image name when creating a custom image, for example:

docker build -t dude/man:v2 . # Will be named dude/man:v2 

Is there a way to determine the image name in the Dockerfile, so I don't need to mention it in the docker build ?

+172
docker tags dockerfile
Aug 16 '16 at 23:37
source share
4 answers

Image tagging is not supported in Dockerfile. This should be done on your build team. As a workaround, you can build using docker-compose.yml, which identifies the name of the target image, and then run docker-compose build . An example docker-compose.yml will look like

 version: '2' services: man: build: . image: dude/man:v2 

However, there is a push against building an assembly with compose, as this does not work in swarm mode. So, you are back to running the command, as you asked in your question:

 docker build -t dude/man:v2 . 

Personally, I usually collect a small shell script in my folder (build.sh), which passes any arguments and includes the name of the image to preserve typing. And for production, the assembly is processed by the ci / cd server, whose name is inside the pipeline script.

+149
Aug 17 '16 at 1:08
source share

How to create an image with a custom name without using the yml file:

 docker build -t image_name . 

How to start a container with a custom name:

 docker run -d --name container_name image_name 
+203
Mar 13 '17 at 13:58 on
source share

Here is another version if you need 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 up --build 
+18
Jan 31 '18 at 4:37
source share

You can do this using 'AS' FROM Ubuntu: 16.04 AS FooImage

0
Apr 08 '19 at 14:58
source share



All Articles