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

What are the basic commands of Docker container management

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the knowledge of "what are the basic commands of Docker container management". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

How do I run the Docker container?

As we all know, the Docker container is just an application process running on the host operating system, so you need an image to run it. A Docker image that runs as a process is called a Docker container. You can load a local Docker image or download it from Docker Hub. Docker Hub is a centralized repository that provides both public and private images for pull operations. The official Docker Hub is located in hub.docker.com. When you instruct the Docker engine to run the container, it will first search for the local image, and if it cannot find it, it will pull the corresponding image from the Docker Hub.

Let's run a Docker image of the Apache web server, such as the httpd process. You need to run the docker container run command. The old command was docker run, but later Docker added a subcommand section, so the new version supports the following commands:

Root@kerneltalks # docker container run-d-p 80:80 httpd Unable to find image 'httpd:latest' locally latest: Pulling from library/httpd 3d77ce4481b1: Pull complete 73674f4d9403: Pull complete d266646f40bd: Pull complete ce7b0dda0c9f: Pull complete 01729050d692: Pull complete 014246127c67: Pull complete 7cd2e04cf570: Pull complete Digest: sha256:f4610c3a1a7da35072870625733fd0384515f7e912c6223d4a48c6eb749a8617 Status: Downloaded newer image for httpd:latest c46f2e9e4690f5c28ee7ad508559ceee0160ac3e2b1688a61561ce9f7d99d682

Docker's run command takes the image name as a mandatory parameter, and there are many optional parameters. The commonly used parameters are:

-d: detach the container from the current shell-p X Y: bind the container's port Y to the host's port X-name: name your container. If not specified, it will be given a randomly generated name-e: pass environment edits and their values when the container is started

As you can see from the output above, we run the container with httpd as the image name. Then, the local image was not found, and the Docker engine pulled it from Docker Hub. Notice that it downloads the mirror httpd:latest, where: followed by the version number. If you need to run a specific version of the container, you can indicate the version name after the image name. If you do not provide a version name, the Docker engine automatically pulls the latest version. The last line of the output shows the unique ID of your newly run httpd container.

How do I list all running Docker containers?

Now that your container is running, you may want to confirm this, or you may want to list all the containers running on your machine. You can use the docker container ls command. In the old version of Docker, the corresponding command was docker ps.

Root@kerneltalks # docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c46f2e9e4690 httpd "httpd-foreground" 11 minutes ago Up 11 minutes 0.0.0.0 httpd-foreground 80-> 80/tcp cranky_cori

The listed results are displayed by column. The values for each column are:

Container ID: the first few characters correspond to the unique IDImage of your container: you run the container's image name Command: the command run after the container starts Created: creation time Status: container current status Ports: port information connected to the host port Names: container name (if you do not name your container, it will be created randomly) how to view the history of the Docker container?

In the first step, we used the-d parameter to detach the container from the current shell as soon as it started running. In this case, we don't know what's going on inside the container. So to see the history of the container, Docker provides the logs command. It takes the container name or ID as a parameter.

Root@kerneltalks # docker container logs cranky_cori AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message [Thu May 31 18 pid 1:tid 35 Thu May 07.301158 2018] [mpm_event:notice] [pid 1:tid 139734285989760] AH00489: Apache/2.4.33 (Unix) configured-- resuming normal operations [Thu May 31 1815 35V 07.305153 2018] [core:notice] [pid 1:tid 139734285989760] AH00094: Command line: 'httpd-D FOREGROUND'

Here I use the container name as the parameter. You can see the history related to Apache in our httpd container.

How do I determine the process of the Docker container?

A container is a process that runs using host resources. In this way, you can locate the container's process in the process table of the host system. Let's determine the container process on the host system.

Docker uses the famous top command as the name of the subcommand to view the processes generated by the container. It takes the name of the container or ID as a parameter. In older versions of Docker, only the docker top command could be run. In the new version, both the docker top and docker container top commands take effect.

Root@kerneltalks # docker container top cranky_cori UID PID PPID C STIME TTY TIME CMD root 15702 15690 0 18:35? 00:00:00 httpd-DFOREGROUND bin 15729 15702 0 18:35? 00:00:00 httpd-DFOREGROUND bin 15730 15702 0 18:35? 00:00:00 httpd-DFOREGROUND bin 15731 15702 0 18:35? 00:00:00 httpd-DFOREGROUND root@kerneltalks # ps-ef | grep-I 15702 root 15702 15690 0 18:35? 00:00:00 httpd-DFOREGROUND bin 15729 15702 0 18:35? 00:00:00 httpd-DFOREGROUND bin 15730 15702 0 18:35? 00:00:00 httpd-DFOREGROUND bin 15731 157020 18:35? 00:00:00 httpd-DFOREGROUND root 15993 15957 0 18:59 pts/0 00:00:00 grep-- color=auto-I 15702

In the first output, a list of processes generated by the container is listed. It contains all the details, including user number, process number, parent process number, start time, command, and so on. All the process numbers here can be searched in the host's schedule. That's what we did in the second order. This proves that the container is indeed a process in the host system.

How do I stop the Docker container?

Only the stop command is needed! Again, it takes the container name or ID as a parameter.

How does root@kerneltalks # docker container stop cranky_cori cranky_cori list stopped or inactive Docker containers?

Now that we have stopped our container, if we use the ls command, it will not appear in the list.

Root@kerneltalks # docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

So, in this case, if you want to see stopped or inactive containers, you need to use the-a parameter in the ls command as well.

Root@kerneltalks # docker container ls-a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c46f2e9e4690 httpd "httpd-foreground" 33 minutes ago Exited (0) 2 minutes ago cranky_cori

With the-a parameter, we can now look at the stopped containers. Notice that the status of these containers is marked as exited. Since the container is just a process, "exit" is more appropriate than "stop"!

How do I (restart) the Docker container?

Now, let's start the stopped container. This is different from running a container. When you run a container, you will start a brand new container. When you start a container, you will start a container that has been stopped and saved in its current running state. It will start running again in the state it was when it was stopped.

Root@kerneltalks # docker container start c46f2e9e4690 c46f2e9e4690 root@kerneltalks # docker container ls-a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c46f2e9e4690 httpd "httpd-foreground" 35 minutes ago Up 8 seconds 0.0.0.0 docker container ls 80-> how does 80/tcp cranky_cori remove the Docker container?

We use the rm command to remove the container. You cannot remove a running container. You need to stop the container before removing it. You can use the-f parameter with the rm command to force the removal of containers, but this is not recommended.

Root@kerneltalks # docker container rm cranky_cori cranky_cori root@kerneltalks # docker container ls-a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES "what are the basic commands for Docker container management"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report