Understanding Tags and Docker CLI.
In this series, we are exploring the cool world of containers. To catch you up, we experimented with ditching docker and making it on our own in this containerized world, only to realize soon that Docker was our one true love, and life was hard without it. So we are now back together, trying to understand it better.
In this post, we will go over the concept of tags and how Docker CLI can be our friend.
If you have ever come across Git tags, you can think of Docker tags to be similar, in the sense that they convey some information about a specific image that you might be accessing or building. With the help of these tags then, you can refer to an exact version of an image in the future.
Some basic rules apply to naming tags. A tag name must be valid ASCII and may contain lowercase and uppercase letters, digits, underscores, periods and dashes. A tag name should not start with a period or a dash and may contain a maximum of 128 characters.
With that out of the way, let's see how we can build an image with a tag attached:
$ docker build -t USERNAME/IMAGE_NAME:TAG_NAME .
It's also possible to attach multiple tags to an image like so:
$ docker build -t USERNAME/IMAGE_NAME:latest -t USERNAME/IMAGE_NAME:v3.0 .
You can thing of
USERNAME/IMAGE_NAME like it appears in repos. In the above commands we are directing Docker daemon to fetch the Docker file present in the current directory ( and so the
. at the end) and build it with the specified tags.
Docker also has the
tag command to apply tags to a
SOURCE_IMAGE, the common syntax for this is:
$ docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Let's see how this common syntax can morph with various options available:
1.Referencing images by ID:
Let's say you had a local image with ID:
0e7427665393 and you wished to add it to the
alpine repo with
version12.0, you'd do something like so:
$ docker tag 0e7427665393 alpine/httpd:version12.0
2.Referencing images by name:
To reference by name, simply replace the
ID with the
NAME of the local image:
$ docker tag myLocalImage alpine/myLocalImage:version12.0
3.Referenced by Name and Tag:
To tag a local image with name
myLocalImage and tag
test into the
alpine repo with
version1.0.test , we can do:
$ docker tag myLocalImage:test alpine/myLocalImage:version1.0.test
Note: If no tags are specified explicitly, the implicit tag applied is
With that we have covered the foundational understanding of
tags and some ways they are commonly used in. Now, let's move on to some other cool features of the Docker CLI.
The Docker CLI is one of the major component of the Docker Engine which, basically is a client-server application. The CLI uses Docker's REST API to interact with Docker daemon through scripting or direct CLI commands. Let's dive into some CLI commands you'll need the most. (Feel free to use the list below as a quick reference guide when working with containers.)
pull allows you to pre-fetch a container image:
$ docker pull ubuntu:14.04
push an image or a repository to a registry:
$ docker push YOURHUBUSERNAME/REPO_IMAGE_NAME # to push to Docker hub $ docker push REGISTRY_HOSTNAME:PORT/USERNAME/REPO_IMAGE_NAME #to push to any registry on a host
Lists out a ton of information about what's happening inside your container, which can be helpful while debugging.
$ docker inspect [OPTIONS] NAME|ID [NAME|ID...]
Well, you guessed it, it pauses and un-pauses your containers. Here's an example to try:
$ docker pause <ID or name> $ docker ps # see container paused $ docker unpause <ID or name> $ docker ps # see container running again
Please do not confuse this with
run command. The
run starts a new container whereas
exec will run the following command in an already running container.
$ docker ps # grab the name or ID $ docker exec <ID or name> ps aux # see it output all the running processes of the container
This allows you to see how the Docker image's layer composition has changed over time aka
history of an image.
$ docker history IMAGE
If you were interested to find out system wide information regarding the Docker installation, including things like the kernel version, number of containers and images, etc.
info would be your command:
$ docker info [OPTIONS]
To see all processes running on a container:
$ docker top <CONTAINER_ID>
ps command lists all containers, including the ones that have been stopped. To remove a container from this list, we can use the
rm command like so:
$ docker rm <CONTAINER_ID OR CONTAINER_NAME>
But if we wanted to remove an image from our system completely (say, to free up space), we would have to:
$ docker rmi <CONTAINER_ID OR CONTAINER_NAME>
$ docker logs <CONTAINER_ID OR CONTAINER_NAME> #see all 'em logs $ docker restart <CONTAINER_ID OR CONTAINER_NAME> #restart containers $ docker search [OPTIONS] SEARCH_TERM #search the Docker Hub for images, you can use python, node etc. in place of SEARCH_TERM
There you go! With all that listed in one place, you should now have your most referenced Docker cheatsheet :)
In the coming articles, we will get coding and go over the Dockerfile, understand what's port mapping, and look through the layers of a Docker container, but till then happy Coding!