Use these 8 commands to learn the basic management of Docker containers. This is a guide for Docker beginners with sample command output.

In this article, we will take you through 8 basic Docker container commands, which 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 unfamiliar with the concept of Docker, it is recommended that you take a look at our introduction guide to understand the basic content of Docker and how to install Docker on Linux. Now let's quickly enter the command to understand:

How to run Docker containers?

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

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

Share 8 commands to learn Docker containers

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

-d: leave the container from the current shell

-p X: Y: bind port Y of the container to port X of the host

--name: Name your container. If not specified, it will be given a randomly generated name

-e: pass the environment edit and its value when starting the container

You can see from the above output that we use httpd as the image name to run the container. Then, the local image was not found, and the Docker engine pulled it from Docker Hub. Note 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 specify the version name after the image name. If no version name is provided, the Docker engine will automatically pull the latest version.

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

How to list all running Docker containers?

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

Share 8 commands to learn Docker containers

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

Container ID: The first few characters correspond to the unique ID of your container

Image: the image name of the container you are running

Command: The command to run after the container starts

Created: creation time

Status: The current status of the container

Ports: Port information connected to the host port

Names: container names (if you do n’t name your container, it will be created randomly)

How to view the history of Docker containers?

In the first step we used the -d parameter to detach the container from the current shell when it first started. In this case, we do not know what is happening inside the container. So in order to view the history of the container, Docker provides the logs command. It takes the container name or ID as a parameter.

Share 8 commands to learn Docker containers

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

How to determine the progress of Docker containers?

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

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

In the first output, a list of processes spawned by the container is listed. It contains all the details, including user ID uid, process ID pid, parent process ID ppid, start time, commands, etc. You can find all process numbers here

Share 8 commands to learn Docker containers

Searched in the host's process table. This is what we did in the second command. This proves that the container is indeed a process in the host system.

How to stop Docker containers?

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

Share 8 commands to learn Docker containers

How to 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.

Share 8 commands to learn Docker containers

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

Share 8 commands to learn Docker containers


With the -a parameter, we can now view the stopped container. Note that the status of these containers is marked as exited. Since the container is just a process, it is more appropriate to use "exit" rather than "stop"!

How to (re) start 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 running in the state it was stopped in.

Share 8 commands to learn Docker containers

How to remove Docker containers?

We use the rm command to remove the container. You cannot remove the running container. The container needs to be stopped before removal. You can use the -f parameter with the rm command to force container removal, but this is not recommended.

Share 8 commands to learn Docker containers

You see, once the container is removed, you can't see the container even if you use the ls -a command again.

Blender

SHENZHEN CHONDEKUAI TECHNOLOGY CO.LTD , https://www.szsiheyi.com

Posted on