Docker

Docker Attach to Running Container

Docker is a collection of infrastructure solutions that provide many programs in containers using OS-level emulation. Containers seem to be separate from others and include their independent programs, modules, and configuration settings. Containers can interact with each other over definite interfaces. This article will show how a user can attach docker with some running containers in the Ubuntu 20.04 system. Let’s start with the launch of the shell and update the system with the “apt” package. The command has been executed in the shell, as shown in the image.

Now the system is already updated, let’s install the docker utility in our Ubuntu 20.04 system. For this, we will be using the “snap” utility of our system. For that, you must have a snap utility or package installed on the system. So, we have used the “install” query in the shell with the keyword “docker” by the use of the “snap” utility. After the execution, the system started downloading the docker using snap, as shown below.

After a while, it will be mounted, and the shown line will be displayed on the shell.

We have to use the systemctl command to start the docker utility. So, the command has been used with the name “docker” while utilizing the word “start” to make it start and the word “enable” to make it active in our system

When we check for the docker’s current status, the systemctl command shows us that the docker is active and running in our system, as displayed in the image below.

You need to add the curl SSL key for the docker we have installed. You have to utilize the below-demonstrated command in the image within your shell terminal as we did. You will get the result with the word “OK” as shown.

Let’s try to connect with the docker and its containers via the “ps” command. Running this command may lead you to a permission denied error, as we have got in the image attached.

You don’t need to panic. This error can be resolved with a simple single command of “chmod”. This command will be used to get permission for the “docker.sock” file held in our system. It may ask for your sudo password, and you will see it working.

Now, when you run docker’s “ps” command in the shell, it will show you the output having some column names. Currently, there are no containers found so far in our docker. The “-a” flag shows the total containers in our system, i.e., zero. The “-l” flag will show the last used container in our docker, which is also empty. There, we need to create one.

For the creation of the container, we need images for docker. We can acquire these images on the docker website and can be pulled within the docker using the pull command in the shell. While checking, we have found that our docker has no images as per the command.

Using the pull command, let’s start pulling the images in our docker utility. So, we will be pulling more than 2 to 3 images. So, we have utilized the docker pull command to get the “hello-world” image of docker. As shown below, it will take 1 minute to pull and download it in the system.

Then, we have downloaded another image named “busy box” via the pull query.

As shown in the attached photo, the most generic “Ubuntu” image has been pulled out.

Now, we can check out the downloaded docker images in our shell terminal using the docker “images” query. The command shows the 3 downloaded images with their specific information, i.e., name of the image, tag, image ID, date created, and size.

You need to run the images using the docker “run” query as below.

Now, you can see that our system contains 3 containers, but none is active yet.

You can change the names of your docker containers created by their respective images via the “run” command with some flags. You have to utilize the “—name” flag along with the new name of a container with the image name as shown below. You can see it not only changes the name but also starts our container. When we tried the “list” command in it, it showed all the available folders of this container.

When you try the docker “ps” command to check the currently running containers on your system, you will get the below result. You can see the docker name is One in this command.

Open another tab in the terminal and use the docker “start” command with the new name of a container, i.e., One. You will see, it will return the name of a container which means the container is started. If you want to attach your docker with the specific Container, you can utilize the docker “attach” command with the name of a container. You will see that the specific container “One” will be started as we have listed the data held in it.

You can pause the container for a while via the docker “pause” command as did beneath. When you come back to the last tab of the terminal, your container “One” will stop working, and you will find yourself unable to add any command in it. You have to use the docker “unpause” command to release it.

To check the container’s statistics, you can use the “stats” command.

The stats will be displayed on the shell as below.

You can remove the container of your choice via the docker “rm” command.

Conclusion:

This article explains and demonstrates attaching a docker utility to some of its running containers. We have covered the installation of docker, downloading docker images, i.e., creating containers, attaching docker with containers, and some useful commands. We are hoping for the best feedback, and we did our best.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.