Discussion on Docker run container in created state

  • 2021-10-16 05:38:03
  • OfStack

In a recent problem, there is such a phenomenon:

There is a test script in the system that continuously executes docker run command to run the container. During the test, it is found that sometimes the container is not completely run to the "Up" state, but in the "created" state, which is 10 points strange.

The environment above first looks at the container for the "created" status, as well as the dockerd log:

(1) There is only "post create" request in the log of dockerd, but there is no "post start" request from this container;

(2) Manual execution of docker start can pull the container to the "Up" state, indicating that there is no problem with the container and mirror itself.

Based on the above phenomenon, it is suspected that the process of "docker run" was not completed, and docker run exited. Read immediately through "docker run." In cli/command/container/run. go, the implementation of the processing function func runRun () for the "docker run" command is as follows:


func runRun(dockerCli *command.DockerCli, flags *pflag.FlagSet, opts *runOptions, copts *runconfigopts.ContainerOptions) error {
  . . . . . . 
 createResponse, err := createContainer(ctx, dockerCli, config, hostConfig, networkingConfig, hostConfig.ContainerIDFile, opts.name)
  . . . . . . 
 if err := client.ContainerStart(ctx, createResponse.ID, types.ContainerStartOptions{}); err != nil {)
  . . . . . 
}

If the "docker run" command exits abnormally after executing the createContainer () function (such as the kill signal is encountered), the ContainerStart () function cannot continue running at this time. This results in the container being successfully created in the "created" state, but not really giving dockerd "ES50start", which ultimately leads to the above phenomenon.

Therefore, it is necessary to monitor the "docker run" command in the daily production process, such as judging whether it is successfully executed, whether it exits abnormally, whether the return value is 0 when exiting, and so on.

Supplement: 3 methods for docker to be in running state after running container

Solution 1

When we run the docker container, we are often in Exited state.

For example, the following command docker run-d-name nginx-P nginx/bin/bash will exit after running, and it is useless to re-use docker start;

If you want the container to be running after running, just remove the/bin/bash.

The second solution


docker run -it --name nginxit -P nginx

The container nginxit is now in the exit state;

It can be used as long as we start it with docker


docker start nginxit

Type 3 is similar to Type 2:


docker run -it --name nginxit2 -P nginx /bin/bash

Background and background interaction needs to exit:


root@de4dbb27f905:/# exit

Then restart:


docker start nginxit2

Related articles: