While my previous article titled,“Docker: Installing Docker and Understanding basic docker commands”, provides detailed information about how to install docker and some basic commands, In this article, we are going to see some advanced docker commands.
We will discuss the topics one by one.
1. Giving a name to your container
As explained in my previous article, when we use “docker run -it <image_name>, it looks for the image locally, if it doesn’t find it locally, it will download that image from docker hub, will run the container and will go inside it. See below:
In the whole process, the docker assigns a random name to the container. Like in the below example, “hungry_kare” is the name docker has assigned to the container.
So, what if you want to give your container a name? You can do so by using the “- – name” option.
2. Docker Attach
The “docker attach” command lets you go inside a running container, however when we do so, we are attached to the main process of that container. So, when we log out from that container, it sends a SIGKILL signal and the container gets stopped( Which may not be ideal though).
So, when do we use the docker attach command?
— Well, Use it in scenarios when your container has some issues and you would like to see the standard output and standard error.
As per docker documentation- “Use
docker attach to attach your terminal’s standard input, output, and error (or any combination of the three) to a running container using the container’s ID or name. This allows you to view its ongoing output or to control it interactively, as though the commands were running directly in your terminal.”
3. Docker Pause
The docker pause command suspends all processes in the specified containers.
On Linux, this uses the cgroups freezer. Traditionally, when suspending a process the SIGSTOP signal is used, which is observable by the process being suspended. With the cgroups freezer, the process is unaware, and unable to capture, that it is being suspended, and subsequently resumed
So it means that the processes in the container stop running, and they are able to be resumed later. Possible use is to pause resource-intensive tasks that you can resume at a later date.
4. Docker Unpause
Use the “docker unpause” command to unpause a paused container.
5. Docker Restart
Use “docker restart” to start a stopped container. This command will not create a new container, but, will start the same container we want to bring up( This will retain the container id).
6. Docker kill
In my previous article, we have seen how to stop a running container by using “docker stop <container_id>”. This command will perform the graceful shutdown of a running container.
But, what if the container is not going to stop or not responding. In those scenarios, use the “docker kill” command instead.
7. Docker commit
Let’s suppose you are working with a container and have made some modifications to it (eg, installed some packages, made some configuration changes) and now want to retain those changes in the image. Here, the docker commit command comes to our rescue.
The “docker commit” command lets you create an image from a container.
In the below example, when we log in to the container and try to run “ifconfig” command, it doesn’t work. So, we use “YUM” package manager to install the package:
Now, we exit out of the container.
In the below example, the docker commit command will retain the changes that we have made in the container by creating a new image. we are giving the new image a name called — “centos-net”
8. Docker Push
Let’s suppose you have a running container called “centosv1”
Now you have modified the container and have committed the changes and created a new image ( As described in the previous section).
Note: In order to push your image on docker hub, it’s mandatory to have your image name in the following format :
Like in our case, the image name is: “rajeevkghosh/centos-nettools”
Now perform the below steps to push your image on “Docker Hub”:
- Run the command “docker login” and put your docker hub credentials:
- Now run the command “docker push <Image_name>”:
- Now visit “https://hub.docker.com/” and log in with your credentials. You will see your image in your Docker Hub Repository
That’s it. we are done !!