So what is a docker image? A docker image is read-only layer consisting of binaries libraries and the software which the container shall execute.
A docker image can have as a base another docker image. Actually they are a read only template.
Once created they are stored in your file system.
To view the docker images currently installed on your system you can issue
Let’s download an image
docker pull ubuntu
By default your images are downloaded through DockerHub. Also you can upload your images to DockerHub too, if you want to share them.
In cases you want to share your images privately then you have to check the private option on image hosting on dockerhub or other providers such as Google, Amazon, Azure etc.
Since we have just downloaded an image, we will list again our images. Now we will go a bit further and issue
docker images -a
What you can see is that some images don’t have any name on them. Those images are called intermediate images. Behind the scenes the image that you use consists of many other images. Actually it is a parent-child relation. An intermediate image might be contained in two different images due to common dependencies and binaries.
So next time you download an image the intermediate files and your image will be downloaded.
Now let’s filter some of our images.
Current filters supported are
dangling, label, before, since, reference.
Most probably the reference is the one you are gonna use the most.
docker images --filter=reference='*ubuntu*'
What you actually do with the reference filter is filtering based on the image reference and the pattern you specified.
Now let’s build an image. To build an image you need to create a Dockerfile. The dockerfile will specify the image which you will inherit from and any extra action you need to do.
Here’s the content of the Dockerfile
FROM ubuntu ARG username ENV USERNAME $username ENTRYPOINT echo $USERNAME
An argument is passed and the default command that will be executed when the container is running would be to echo the USERNAME environmental variable which is set based on the argument.
So let’s build the image
docker build --build-arg username=john .
As you can see you have your intermediate images printed and the segment images. Try building it again and the same steps and ids will fill your screen, changes and new intermediates will happen only if you change your Dockerfile.
Next step is to check all our images.
Seems like our image is different that the others. The others have name and tags whilst ours has the autogenerated image id.
Let’s add one
docker tag d88ef0502ecf ubuntu-hello:1.0
Also we can do the tagging while we build the image
docker build --build-arg username=john --tag ubuntu-hello:1.0 .
As you can see I have put a version on the image. For your application most probably you are going to have more than one images created therefore you can tag your images in order to keep track of the versions.
Now time to clean up.
docker rmi ubuntu-hello:1.0
This one will not be successful if you have already run the image. You can force it’s deletion by using the -f argument but is not graceful so don’t do it. Instead delete the containers created from this image and then delete the images.
Also be aware that when you issue rmi using tags, if your image has been tagged with more multiple tags then your tags get removed from the image until the image remains with one tag. If your image has only one tag left then the image gets removed as well.