Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

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!

question from:https://stackoverflow.com/questions/41520614/docker-tag-vs-name-clarification

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...