Carla
Carla

Reputation: 3380

Docker --tag vs --name clarification

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!

Upvotes: 58

Views: 13525

Answers (3)

sekrett
sekrett

Reputation: 1275

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.

Upvotes: 24

gile
gile

Reputation: 5976

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

Upvotes: 25

Andreas J&#228;gle
Andreas J&#228;gle

Reputation: 12240

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.

Upvotes: 77

Related Questions