Share eight basic Docker container managed commands

  • 2020-11-30 08:39:52
  • OfStack

Preface:

In this article, we'll walk 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. Article end welfare!

With these eight commands, you can learn the basics of managing Docker containers. This is a guide for Docker beginners with a demo command output.

In this article, we'll walk 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, we recommend you take a look at our introductory guide to learn the basics of Docker and how to install Docker on Linux. Now let's move quickly to the command to understand:

How do I run the Docker container?

As we all know, the Docker container is just one application process running on the host operating system host OS, so you need a mirror to run it. The Docker image is called the Docker container when it is run as a process. You can load local Docker images or download them from Docker Hub. Docker Hub is a centralized repository that provides both public and private images for pull pull operations. The official Docker Hub is located at hub.docker.com. When you instruct the Docker engine to run the container, it first searches for the local image, and if it doesn't find it, it pulls the corresponding image from Docker Hub.

Let's run an Docker image of an Apache web server, such as the httpd process. You need to run 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

The run command for Docker takes the mirror name as a mandatory parameter, and there are many other optional parameters. Common parameters are:

The & # 8226; -ES67en: Detaches from the container from the current shell
The & # 8226; -ES70en X:Y: Bind the container's port Y to the host's port X
The & # 8226; --name: Name your container. If not specified, it is given a randomly generated name
The & # 8226; -ES78en: Pass the environment edit and its value when the container is started

As you can see from the above output, we run the container with httpd as the mirror name. Then, the local mirror was not found and the Docker engine pulled it from Docker Hub. Notice that it downloads the mirror httpd:latest, where: is followed by the version number. If you need to run a particular version of the container, you can specify the version name after the mirror name. If the version name is not provided, the Docker engine pulls the latest version automatically.

The last line of the output shows the only ID for your newly run httpd container.

How do I list all the Docker containers in action?

Now that your container is up and running, you may want to confirm this point, 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:80->80/tcp cranky_cori

The results listed are shown in columns. The values of each column are:

Container ID: 1 the first few characters correspond to the only 1 ID in your container
Image: You run the mirror name of the container
Command: The command to run after the container is started
Created: Creation time
Status: The current state of the container
Ports: Port information that is connected to the host port
Names: Container name (if you don't name your container, it will be created randomly)

How can I view the history of Docker containers?

In step 1, we used the -ES128en parameter to detach the container from the current shell as soon as it starts running 1. In this case, we don't know what's going on inside the container. So to view 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:35:07.301158 2018] [mpm_event:notice] [pid 1:tid 139734285989760] AH00489: Apache/2.4.33 (Unix) configured -- resuming normal operations
[Thu May 31 18:35:07.305153 2018] [core:notice] [pid 1:tid 139734285989760] AH00094: Command line: 'httpd -D FOREGROUND'

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

How do I determine the progress of an Docker container?

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

Docker USES the famous top command as the name of the subcommand to see the process 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 docker top and docker container top commands are available.


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 15702 0 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, the list of processes produced 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 of these process Numbers you can search in the host process table. That's what we did in the second command. This proves that the container is indeed a process in the host system.

How do I stop the Docker container?

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


root@kerneltalks # docker container stop cranky_cori
cranky_cori

How do I list stopped or inactive Docker containers?

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


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

So, in this case, if you want to see a stopped or inactive container, you need to use both the -ES181en parameter in the ls command.


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 container. Note that the status of these containers is marked out of exited. Since the container is just one 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 stopped and saved the running state at that time. It will restart as it was when it 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:80->80/tcp cranky_cori

How do I remove the Docker container?

We used the rm command to remove the container. You may not remove a running container. You need to stop the container before you remove it. You can use the -f parameter with the rm command to force the container to be removed, but it 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

You see, once the container is removed, it is reused ls -a The command can no longer see the container.

conclusion


Related articles: