Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to manage containers using the Docker client

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use the Docker client to manage containers, which has a certain reference value. Interested friends can refer to it. I hope you will learn a lot after reading this article.

Traditionally, when managing physical servers or virtual machines, we only need to connect to RDP (when running Windows) or SSH (when running Linux) to access the console and then execute tasks from there. However, the container is designed to be ready for production. In theory, we should not manage them on a daily basis like the previous IT staff. However, it is also important to understand how to perform such troubleshooting tasks and to understand the technology.

Run the container

The first mistake that IT people can make when creating containers is to forget the relationship between the image and the container. One relationship between the two is that an image allows N containers to be created. Keep in mind that the image is read-only, and when we create the container, there is a thin read-write layer that allows us to write data to the container, and that data will persist throughout the container life cycle.

Another important point is that the container is designed to run a single application, and if the application is terminated, we can assume that the container will also be stopped.

Why would you say that? Let's give a simple example: we want to execute docker run alpine and container exit immediately, so we use-it, which forces it to interact and use the current terminal. Because I want this session to run in the background, I use the keyboard key Ctrl + p, and then Ctrl + Q, which is repeated four times, as shown in the following figure.

The challenge is: if you do not specify the name of the container, then Docker's own algorithm will create a name for you, but if we try to copy a file to a specific container, even though they share the same image and have the same application, we still need to be able to identify the container. Because it's very likely that we won't remember that name or container ID information later.

If we copy a file to the first container (such as cranky_pike), then only this container will have that file, assuming you don't remember the container name or ID, the best solution in this case is to habitually use switches (switch)-name to label the corresponding container, which can be used in creating containers.

Since we are in the current scene, if we need to return the shell of any given container, we can take advantage of the docker container attach command.

Note: you can use the container name or any string as the unique identity of the container. We don't need to enter the entire container ID, in which case I can just use the letter "f" because this container is the only one that starts with "f".

My current container list is empty. Why?

The Docker client command docker container list lists only the active containers. To get a list of all containers, simply add the switch, all, at the end of the same Docker client command (as shown in the following figure).

Now what should I do if I want to go back to a specific container to check something or grab a file? The first thing is to start the container and then return to the session. The beauty is that the commands used to create can also be used to start the container (which makes sense), so there is no need to pass additional parameters. Using the following Docker client command, we can start any stopped container.

Docker container start docker container attach

Is there a way to automatically clean up unused containers?

No. If we use the switch rm when creating the container, we set a flag on the container that will be removed as soon as the container exits. You need to be very careful when using this option.

Docker run-it-name-hostname-rm alpine

If you want to delete all containers with one line of code, here is the following (the first two lines and the last line are just to show the logic for creating a line). Keep in mind that we are deleting all containers, so it is at your own risk to use it.

Docker container listdocker container list-aqdocker container rm $(docker container list-aq)-fdocker container list

The-name switch is helpful, but the name inside the VM is not defined

Here is how to solve this problem. Using the-- name parameter defines the label for a given container. If we want to configure the name in the container, we need an extra parameter (--hostname). Examples of syntax for containers are highlighted below. We connect to the shell of the given container and check the name used in the Linux container.

Docker run-it-hostname container002-name container002 alpine

Execute commands on the container

Sometimes when managing containers, it is easier to run commands on the container, saving time for troubleshooting elsewhere. We can use the docker container exec command to trigger a specific command on any given container.

For illustration purposes, the first two commands (hostname and ipconfig) are executed on the host. To get the information directly from the container, we can use the following command:

Docker container exec

Connect to the console (Linux and Windows)

When using the Windows container (Windows operating system), we can execute cmd, which gives us a command prompt for the container. The following Docker client command provides the container console labeled webserver.

Docker container exec-it webserver cmd

If you want to use Linux, we just need to change the command command. The following command is available for the Linux container:

Docker container exec-it / bin/sh

If we use PowerShell when executing the Docker command, the first thing we will notice is the black background of CMD. All other commands executed on the following image are executed at the container level. If you are not sure, the name of the machine is also a good indication, and you can use tasklist to retrieve the number of processes.

Managing containers: need to be in depth

After reading this article, does it mean that we know container management like the back of our hand? Of course not! We're just getting started! If you want to continue exploring and trying new things, try these two Docker client commands:

Docker container-helpdocker container run-help Thank you for reading this article carefully. I hope the article "how to use Docker client to manage containers" shared by the editor will be helpful to you. At the same time, I also hope you will support us and follow the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report