Updated: Jan 25, 2022
In this tutorial, I will cover the main aspects of containers which is the building block of the Docker architecture. Among these aspects, I will cover:
How to get information on the Docker client and Docker engine.
Check container logs and processes.
Containers, Images and the difference between them.
Basic Container commands.
So the first thing that we want to do is check the versions of our Docker CLI and engine. The ideal scenario is that the returned values will be the same, and you want them to be the latest versions to get the most updated features.
Let's run the docker version command to see its output:
PS C:\> docker version
As you can see here, the output provides only basic information, So let's get a little more information out of our Docker engine:
PS C:\> docker info
If you take a close look at the output below, you will see that you will have a lot of details about our configuration and setup for our engine. For example, we can see that we have eight containers that only 6 of them are running or that we have a total of five images
Image Vs. Container
Let's start with some theory on images and containers before running commands. An image is an application we want to run; it includes all the Libraries, binaries, and configurations needed to run the application, while a container is an instance of an image running as a process. While working with images and containers, you can run many containers based on the same image.
Starting a new container
I will create a new container and use some Docker commands to manage that container.
PS C:\> docker container run --publish 80:80 nginx
When running this command, Docker searched for the latest image 'Nginx from Docker Hub (next time, he will use the image downloaded to our local Docker Server) and started a new container from that image. In addition, we opened port 80 on the Host IP that will route all external traffic to the container IP).
Now that we created a new Nginx container, open a web browser and enter "Localhost" to check that our new container is up and running:
In addition, we can check for more details on this container by running the docker ps command:
Now let's Stop this container from running using the stop command:
PS C:\> docker container stop f291a6283496
When using the 'ls' command we will see only the containers currently running without containers that are stopped. So, if you want to see all the available containers, you can use the -a command:
When creating a new container, Docker creates the container name randomly. This is not a good practice when you have tens or hundreds of containers running in your host. So a good practice is to use meaningful names that make some sense. to do it, we can specify the name while creating the container:
λ docker container run --publish 80:80 --detach --name webhost nginx
And here is the new container with the exact name we provided:
To check the container logs, we will use the logs command:
λ docker container logs webhost
Displaying the running processes of a container
if you want to check what is the process is currently running on a container, you can run the top command:
How to remove multiple containers
We can remove containers one by one or remove all of them using the rm command. So let's see a scenario where we have two containers (One is running and one is stopped):
Now. I will try to remove both containers using the rm command:
as you can see, the command removed the container that is not running but failed to remove the container that is currently running with the error "Error response from daemon: You cannot remove a running container b2ea3c8877c9626407b53940183f72017a62450ef16626ff693e400459fd3378. Stop the container before attempting removal or force remove."
So let's add the 'force' attributes that will remove all containers regardless of their running status:
What happens when Docker is running
When we create a new image using the Docker run command, there are a few things that go behind the screen and are worth mentioning here:
Docker looks for that image locally on the machine (image cache) and doesn't find the image.
Docker then looks for that image in a remote image repository (Docker Hub).
When the image is found in the repository, it downloads the latest version and stores it locally on the image cache.
Once the image is ready, it creates a new container based on that image and prepares to start it.
It gives it a virtual IP on a private network inside the Docker engine to access that image.
If we specify the --publish command as we did in the example above (--publish 80:80), it will open port 80 on the host and forward it to port 80 in the container.