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

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 :)

Related

How to remove docker image entry (but not the image) with <none> tag which has multiple non-none tag entries

I have a number of docker images with multiple tags.
Say there are 3 entries (as shown by docker image ls | fgrep image) with the same ID:
image:tag1
image:tag2
image:<none>
How do i remove the 'image3:<none>' entry but keep the other 2 entries?
you can't
'docker image rm image3:<none>' or
'docker image rm image3:'
and i don't want to do 'docker image rm ID'
I've looked at a number of stackoverflow entries but none seem to cover this situation.
The following worked for me:
Tag the image ID to any arbitrary tag with the same image name. For example, if your image is called my_image:<none> and has image ID 0a279023f465, execute docker tag 0a279023f465 my_image:delete_me.
Review the list of docker images and see that my_image:<none> has been replaced by my_image:delete_me.
You can now delete the tag, e.g. docker image rm my_image:delete_me.
Review the list of docker images and see that both my_image:<none> and my_image:delete_me are gone, but other tags remain intact.

docker ps - show image ID instead of name

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

How to get rid of a huge local Docker image?

Caveat: I'm new to Docker user
I pulled a docker image to my machine:
sudo docker pull docker.local:5000/rhel7/24_GB_docker_image
This image is 24GB in size, and I'd like to delete it from my machine, without deleting it from docker.local machine.
Are there documents/URLs that would point me in the correct direction to achieve the above?
Use docker rmi. It deletes a local image (see https://docs.docker.com/engine/reference/commandline/rmi)
# find the image ID
docker images --filter reference=docker.local:5000/rhel7/24_GB_docker_image
# delete image
docker rmi <image ID>
To clean up not used images, containers, etc. you can use the docker system prune command.Itwill ask you what you want to delete.

Docker - list images by tag

Docker docs give:
docker image ls [OPTIONS] [REPOSITORY[:TAG]]
but this assumes you'll specify a repo.
How do you list local images just by tag?
Nice question! I was playing with docker image ls and have found that you do not have to specify repository. The following actually work:
# displays images with tag: latest
docker image ls *:latest
# displays all images but not those with: <none>
docker image ls *:*
edit:
docker image ls *:latest has some inconsistencies. I noticed that some images are not displayed whereas docker image ls | grep latest shows them. I'll get back if I find out why...
Update by Santhosh V:
It looks like a special character needs to be specified in the filter like docker image ls *\*:* for considering a \ in the image name
You can't just with the docker CLI on it's own. The CLI only supports listing all images (including or excluding intermediate layers), images matching a repo, or images matching a repo:tag.
In a shell then you can pipe to grep as has been mentioned in comments, otherwise you will have to parse the output of whichever method you use to list images.
Use:
#docker images | grep [tag]
You will use the Docker API, see the doc
https://docs.docker.com/registry/spec/api/#listing-image-tags
see also
https://gist.github.com/kizbitz/175be06d0fbbb39bc9bfa6c0cb0d4721
If the Docker API is on
http://myregistry.com
on the port 2375, then you will do something like
curl http://myregistry.com:2375/images/json
you will find in /images/json the same info as
docker images
will show
The following works on Docker version 20.10.6, build 370c289
docker images --filter=reference='*:sometag'
The official documentation can help you with more such filters.

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