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

How to run the Docker container

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

How to run the Docker container, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

The editor will discuss containers in depth: learning the various operations of containers, how to transition between the various states of containers, and implementing the underlying technology of containers.

Run the container

Docker run is the way to start the container. We have learned from the discussion of Dockerfile that there are three ways to specify the commands to be executed when the container starts:

CMD instruction.

ENDPOINT instruction.

Specified on the docker run command line.

For example, the following example:

Pwd is executed when the container starts, and the / returned is the current directory in the container. Execute docker ps or docker container ls to view the containers currently running in Docker host:

Hey, how come there is no container? Use docker ps-an or docker container ls-a to see.

-a displays containers with all states, and you can see that the previous container has been exited with a status of Exited.

This "flash" container is not usually the result we want, and we want the container to remain runing so that it can be used by us.

Let the container run for a long time

How to keep the container running?

Because the life cycle of the container depends on the command executed at startup, the container will not exit as long as the command does not end.

With this understanding, we can keep the container running by executing a long-running command. For example, execute the following command:

The while statement prevents bash from exiting. We can open another terminal to see the status of the container.

It is visible that the container is still running. However, this approach has a drawback: it takes up a terminal.

We can start the container in the background with the parameter-d.

The container starts up and goes back to the terminal of docker host. Here you see that docker returns a string of characters, which is the ID of the container. View the container through docker ps.

Now we have two containers that are running. Notice the CONTAINER ID and NAMES fields of the container here.

CONTAINER ID is the "short ID" of the container, and the "long ID" is returned when the container is started earlier. A short ID is the first 12 characters of a long ID.

The NAMES field displays the name of the container. When you start the container, you can use the-- name parameter to name the container. If it is not specified, docker will automatically assign the name to the container.

For subsequent operations of the container, we need to specify the container to be operated on by "long ID", "short ID", or "name". For example, stop a container as follows:

Here we specify the container to stop through the "short ID".

Containers launched through while can stay running, but they don't actually do anything meaningful. A common use of containers is to run backend services, such as the http server we saw earlier:

This time we specify the name of the container with-- name. We also see that the command run by the container is httpd-foreground, which we can tell from docker history that this command is specified through CMD.

We often need to go into the container to do some work, such as checking logs, debugging, starting other processes, and so on.

After reading the above, have you mastered how to run the Docker container? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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

Servers

Wechat

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

12
Report