Eight basic Docker container management commands

Eight basic Docker container management commands

Past memory big data Past memory big data

English original of this article: https://kerneltalks.com/virtualization/8-basic-docker-container-management-commands/
Chinese translation: https://www.iteblog.com/archives/2382.html (click below to read the original Accessible)
In this article, I will introduce eight basic Docker container commands. These commands are useful for performing basic operations on Docker containers, such as run, list, stop, view logs, delete, and so on. If you are not familiar with the concept of Docker, I recommend you to check the relevant introduction on the Internet, this article will not introduce it in detail. Now we quickly enter the command to understand:

How to run Docker containers?

As we all know, a Docker container is just an application running on the host operating system, so for a Docker, you need an image to run it. When a Docker image runs in a process, we call it a Docker container. You can get the Docker image locally or download it from Docker Hub. Docker Hub is a centralized repository for storing public and private images. The official Docker Hub is located at hub.docker.com. When you use the Docker engine to run a container, it will first search for the local image, and if it is not found locally, it will pull the relevant image from the Docker Hub.

Let's run a Docker image of the Apache web server (such as the httpd process). You need to use the docker container run command. The old command is docker run, but because Docker later added a subcommand part, 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 image name in the Docker run command must be entered, and colleagues also support many optional parameters. Commonly used parameters are:

  • -d: detach the container from the current shell
  • -p X:Y: Bind port Y of the container with port X of the host
  • --name: Give your container a name. If not set, it will be set to a randomly generated name
  • -e: Pass environment variables and their values ​​when starting the container.
    As you can see from the above output, we use httpd as the image name to run the container. Because this image was not found locally, the Docker engine pulled it from the Docker Hub. Please note that the Docker engine downloaded the image httpd:latest, where: is followed by the version number, which is the naming convention for Docker container images. If you want to run a specific version of the container, you can indicate the relevant version number after the image name. If the version number is not provided, the Docker engine will always pull the latest version.

The last line of output shows the unique container ID of the newly running httpd container.

How to list all running Docker containers?

Now that your container is running, you may need to check it, or you want to list all running containers on the machine. You can use the docker container ls command to achieve this goal. In the old Docker version, you need to use the docker ps command to achieve.

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 are displayed in columns, and the meaning of each column's value is as follows:

  • Container ID: the first 12 letters of the unique ID of the container
  • Image: The name of the image running the container
  • Command: The command to run after the container is started
  • Created: Created time
  • Status: the current status of the container
  • Ports: detailed information bound to the host port
  • Names: The name of the container (because we did not specify a name when we started the container, so here is a randomly generated name)

    How to view the running log of a Docker container?

Since we used the -d parameter in the first step to separate the container from the current shell, it will run in the background. In this case, we don't know what happened inside the container, so we need to check the container's running log. Docker provides us with the logs command, which 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 used the container name as a parameter in the command. You can view Apache-related logs in the httpd container.

How to identify the process of a Docker container?

A container is a process that uses host resources to run. If this is true, then you can find the process of the container in the process table on the host. Let's see how to check the container process on the host.

Docker uses the famous top command as the name of its subcommands to view the processes generated by the container. It uses the container name or ID as a parameter. In older versions of Docker, only the docker top command can be used. However, in the new version, we can use the docker top and docker container top commands.

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

The first output lists the process list generated by the container. It contains all the information, including uid, pid, ppid, start time, start command and so on. You can search for all the process numbers shown here in the host's process table. This is what we did in the second command. So, this proves that the container is actually just a process on the host host.

How to stop the Docker container?

Please use the stop command! Similarly, it takes the container name or ID as a parameter.


root@kerneltalks # docker container stop cranky_cori
cranky_cori

How to list stopped or not running Docker containers?

Now that we have stopped our container, if we try to list the container using the ls command, we will not be able to see it inside.


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

Therefore, in this case, if you want to view stopped or not running containers, you need to use the -a parameter in the ls command at the same time, as follows:


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

After using the -a parameter, we can view the stopped containers. Please note that the status of this container is marked as "Exited". Since the container is just a process, it is more appropriate to use exited than to stop.

How to start a Docker container?

Now let us start the stopped container, which is still different from running the container. When you run the container, you will run commands in a brand new container. When you start a container, you are starting an old stopped container that has saved the old state. It will resume operation in the state it was in 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 to remove the Docker container?

If we need to remove the container from the Docker engine, we can use the rm command. However, you cannot remove a running container. You need to stop the container before removing it. However, you can also use the rm command with the -f parameter to force the removal of the container, 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

After the container is removed, we can no longer use the ls -a command to view the container.

Guess you like

Origin blog.51cto.com/15127589/2679552