In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Foreword:
In this article, we will take you through eight basic Docker container commands that control the basic activities of the Docker container, such as running run, enumerating list, stopping stop, viewing history logs, deleting delete, and so on. Welfare at the end of the article!
With these 8 commands, you can learn the basic management of Docker containers. This is a guide for Docker beginners with demonstration command output.
In this article, we will take you through eight basic Docker container commands that control the basic activities of the Docker container, such as running run, enumerating list, stopping stop, viewing history logs, deleting delete, and so on. If you are new to the concept of Docker, it is recommended that you take a look at our introduction guide to learn the basics of Docker and how to install Docker on Linux. Now let's get to the commands we need to know:
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 host OS, 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 public and private images for pulling 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 httpdUnable to find image 'httpd:latest' locallylatest: Pulling from library/httpd3d77ce4481b1: Pull complete73674f4d9403: Pull completed266646f40bd: Pull completece7b0dda0c9f: Pull complete01729050d692: Pull complete014246127c67: Pull complete7cd2e04cf570: Pull completeDigest: sha256:f4610c3a1a7da35072870625733fd0384515f7e912c6223d4a48c6eb749a8617Status: Downloaded newer image for httpd:latestc46f2e9e4690f5c28ee7ad508559ceee0160ac3e2b1688a61561ce9f7d99d682
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 Xbank 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 lsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc46f2e9e4690 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 ID of your container
Image: the image name of the container under which you are running
Command: the command that runs after the container starts
Created: creation time
Status: current status of the container
Ports: Port information connected to the host port
Names: container name (if you do not name your container, it will be created at random)
How do I view the history of Docker containers?
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_coriAH00558: 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 messageAH00558: 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_coriUID PID PPID C STIME TTY TIME CMDroot 15702 15690 0 18:35? 00:00:00 httpd-DFOREGROUNDbin 15729 15702 0 18:35? 00:00:00 httpd-DFOREGROUNDbin 15730 15702 18:35? 00:00:00 httpd-DFOREGROUNDbin 15731 157020 18:35? 00:00:00 httpd-DFOREGROUNDroot@kerneltalks # ps-ef | grep-I 15702root 15702 156900 18:35? 00:00:00 httpd-DFOREGROUNDbin 15729 157020 18:35? 00:00:00 httpd-DFOREGROUNDbin 15730 157020 0 18:35? 00:00:00 httpd-DFOREGROUNDbin 15731 15702 18:35? 00:00:00 httpd-DFOREGROUNDroot 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 uid, process number pid, parent process number ppid, 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.
Root@kerneltalks # docker container stop cranky_coricranky_cori
How do I 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 lsCONTAINER 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-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc46f2e9e4690 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 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 c46f2e9e4690c46f2e9e4690root@kerneltalks # docker container ls-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESc46f2e9e4690 httpd "httpd-foreground" 35 minutes ago Up 8 seconds 0.0.0.0 docker container ls 80-> 80/tcp cranky_cori
How do I remove a 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_coricranky_coriroot@kerneltalks # docker container ls-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
You see, once the container is removed, you can't see the container even if you use the ls-a command again.
Summary
The above are the eight basic Docker container management commands introduced by the editor. I hope they will be helpful to you. If you have any questions, please leave me a message and the editor will reply you in time. Thank you very much for your support to the website!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.