Docker --tag vs --name clarification

Solution 1:

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.

Solution 2:

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