docker ps - show image ID instead of name - docker

I display running containers using
docker ps command. There is an IMAGE column that shows name of the image that each container was created from. However in the meantime (while containers were running) I have rebuilt some images. The new images have the same names but different IDs. Now I'd like to check from which image specific container was run. I cannot deduce this information using only image name. I need image ID. Is there any possibility to display ID of the image that was used to run specific container?

You can pass multiple container-ids to the docker inspect command and then use the --format to only get the values that you want.
docker inspect --format='{{.Id}} {{.Name}} {{.Image}}' $(docker ps -aq)
This will give you a list of the docker container Ids, names and image IDs that are being used for all of your containers.
asdf1234 /mydockercontainer sha256:abcd1234
https://docs.docker.com/engine/reference/commandline/inspect/

I found that
docker inspect <container-id>
can be used for this purpose.
It displays an image field containing full hash.

docker images
will show you the image names and their IDs

Related

Rename the IMAGE tag of a running container with docker

Just created a docker image with a Dockerfile and started a new docker container using the image previously built. After running the container, I execute "docker ps" and I see the name of the image used by the container in the IMAGE column. This is expected.
After that, I renamed the TAG of my docker image:
docker image tag <old-image> <new-image>
At this point, "docker images" reports two images with exactly the same IMAGE_ID, but different TAG.
Now I remove the initial old-image image.
docker rmi <old-image>
Now "docker images" reports just a single image with TAG new-image. At this point, when I execute docker ps, I see my container is using the IMAGE_ID on the IMAGE column, not the image TAG new-image.
I know if I create a new container, that new container will use the proper image TAG new-image. However, I would like to learn if it's possible to change this, without creating a new container.
Is this possible in some way? How can this be done? Thanks!

Change image tag shown in `docker ps`

I have a container running from a given image.
This image has multiple tags.
If I remove the image tag shown in docker ps, docker ps will show the image digest instead of an image tag.
If I then tag the image with a new tag, docker ps will continue to show the digest.
How do I get docker ps to display a new or different image tag for a given container using that image?
I don't think there's a way. You need to run a new container from the image with new tag.

What is the difference between "docker images ls" and "docker image ls"?

What is the difference between docker images ls and docker image ls (with and without s (plural form))?
I'm confused about these two commands in Docker. docker images ls is listing images in docker, what is the purpose of docker image ls command?
Check the docs:
https://docs.docker.com/engine/reference/commandline/image_ls/
https://docs.docker.com/engine/reference/commandline/images/
docker images list is not an alias to docker image list, docker images is. When calling docker images list, it's the same as docker image list list or docker image list --filter=reference=list, which means filtering the image list with reference that are equal to list — and as you don't have any images containing list, it's returning an empty list. (Read this github discussion by vdemeester and many more https://github.com/docker/cli/issues/887 )
However, when you do docker images image_name, what it does is, it returns all the parameters(list) of image image_name i.e.
REPOSITORY TAG IMAGE ID CREATED SIZE
Earlier you were trying to have docker images ls which means docker image ls ls and the second ls is a list and not an image. Hence if you do docker images it will list down all the images which means it is docker image ls or docker image list. I hope this makes it clear.
docker image ls lists images
docker images xyz lists images with the name xyz. So you usually get empty list for a docker images ls, because ls is treated as name. By the way, we can use a wildcard docker images postgr* . Reference: https://docs.docker.com/engine/reference/commandline/images/#list-images-by-name-and-tag
Well, yes, confusing :)

In Bitbucket Pipeline, how to dynamically detect and start container with newly generated Docker image

Here is my script after a new Docker image is built on the Docker Hub
docker ps // list my docker container so that I know its container ID
docker rm -f 1243432Ds32 //<- how do I dynamically know its id every time?
docker image ls // list the new image, so I can get the image id to remove
docker image rm dfs3423dx//<- how do I dynamically know its id every time?
docker pull myrepo/myprojet:init
docker image ls // list the new image, so I can get the image id to run
docker run -p -d 1280:80 9787dxe243 // <- how do I dynamically know its id every time?
As you can see that I need to run commands manually to get the current and new container/image IDs in order to remove or run them.
When writing a pipeline script, how can I replace the IDs with some dynamic variables to make the whole process automatic?
I found the answer.
When running the image, use --name to specify the container name so that the pipeline script knows what container to remove
For the image name, it's just a format like: repository/name:tag
Problem solved :)

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!
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.
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
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.

Resources