Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker --tag vs --name clarification

Tags:

docker

I'm pretty new to docker and I'm a bit puzzled by the difference between tagging (--tag) an image and assigning it a name (--name). For example, I can see that if I build my custom image out of a Docker file, I can tag it with a name:

sudo docker build --tag=tomcat-admin .
sudo docker run -it tomcat-admin

Passing the name to docker inspect produces a result:

docker inspect tomcat-admin

However it doesn't contain the same attributes of a "named" image:

docker inspect --format '{{ .NetworkSettings.IPAddress }}' tomcat-admin

Template parsing error: template: :1:19: executing "" at <.NetworkSettings.IPA...>: map has no entry for key "NetworkSettings

"

Somebody to shed some light on it? Thanks!

like image 723
Carla Avatar asked Jan 07 '17 10:01

Carla


People also ask

What does tag mean in docker?

Docker tags are mutable named references to Docker images, much like branch refs in Git. They make it easy to pull and run images, and for image authors to roll out updates automatically. For example, to pull the latest Debian GNU/Linux image for the buster release: $ docker pull debian:buster.

Does a docker image need a tag?

To push an image to a private registry and not the central Docker registry you must tag it with the registry hostname and port (if needed).

Can multiple docker images have the same tag?

Yes you can. As long as the images' names are different you can tag them with the same ":tag".


3 Answers

I think you mixed two concepts here, which causes the confusion. On the one hand there is a Docker image which you can think of as a blueprint for starting a container. On the other hand there are containers which are running instances that are based on an image.

When you docker build -t tagname . you are creating an image and tag it with a "name:tag" format usually. So for example, you are building your image as

docker build -t myimage:1.0 .

which creates a new image that is named myimage with a version of 1.0. This is what you will see when you run docker images.

The --name parameter is then used when you create and start a new container based of your image. So for example, you run a new container using the following command:

docker run -it --name mycontainerinstance myimage

This creates a new container based of your image myimage. This container instance is named mycontainerinstance. You can see this when you run docker ps -a which will list the container with its container name mycontainerinstance.

So to better understand the difference, have a look at the docs for building an image and running a container, specifying an image. When reading the docs you will notice which commands target an image and which commands are for containers. You will also see, that there are commands that work for images and containers like docker inspect does.

Inspecting for a network address of course only works when you provide a container name, not an image. In your special case, the container got a generated name, which you can see by running docker ps -a. When you provide this name to the docker inspect command, you will likely see the ip address you wanted.

like image 121
Andreas Jägle Avatar answered Oct 12 '22 02:10

Andreas Jägle


You tag an image

docker build --tag=tomcat-admin .

but you assign a name to a container

docker run -it tomcat-admin

You can assign multiple tags to images, e.g.

docker build --tag=tomcat-admin --tag=tomcat-admin:1.0 .

If you list images you get one line per tag, but they are related to the same image id:

docker images |grep tomcat

tomcat-admin                                    1.0                 955395353827        11 minutes ago      188 MB
tomcat-admin                                    latest              955395353827        11 minutes ago      188 MB

You can tag images also a second time, not just when you build them, so you can keep different image versions.

When you run a container based on a specific image, you can assign it a name, so you can refer it using the name instead than using the containerId.

Obviously you get different attributes by inspecting images and containers. I think it's more clear if you use different name for image tag and container name, e.g.

docker build --tag=tomcat-admin .
docker run -d -ti --name=tomcat-admin-container tomcat-admin

docker inspect tomcat-admin              ==> You inspect the image
docker inspect tomcat-admin-container    ==> You inspect the container
like image 22
gile Avatar answered Oct 12 '22 02:10

gile


The confusing thing is that a tag consists of a name and a tag. In documentation you can see that:

--tag , -t Name and optionally a tag in the ‘name:tag’ format

So if you omit the :tag part, you actually add a name for the image. That's it.

The difference between image names and container names is explained in other's answers.

like image 18
sekrett Avatar answered Oct 12 '22 01:10

sekrett