Does docker launch execute the CMD command?

Let's say the docker container was started using "docker launch" and then stopped by "docker stop". Will the CMD command be executed after launching the docker?

+6
source share
4 answers

I believe that @jripoll is wrong, it seems to run a command that was first run using docker run on docker start .

Here is a simple example to check:

First, create a shell script to run called tmp.sh :

 echo "hello yo!" 

Then run:

 docker run --name yo -v "$(pwd)":/usr/src/myapp -w /usr/src/myapp ubuntu sh tmp.sh 

It will print hello yo! .

Now run it again:

 docker start -ia yo 

It will print it every time every time it starts.

+6
source

When you start docker, you call api/client/start.go , which calls:

  cli.client.ContainerStart(containerID) 

This calls engine-api/client/container_start.go :

 cli.post("/containers/"+containerID+"/start", nil, nil, nil) 

The daemon daemon process that the API calls in daemon/start.go :

 container.StartMonitor(daemon, container.HostConfig.RestartPolicy) 

The container monitor starts the container in container/monitor.go :

 m.supervisor.Run(m.container, pipes, m.callback) 

By default, the docker daemon is the supervisor here in daemon / daemon.go :

 daemon.execDriver.Run(c.Command, pipes, hooks) 

And execDriver creates a command line in daemon/execdriver/windows/exec.go :

 createProcessParms.CommandLine, err = createCommandLine(processConfig, false) 

This uses processConfig.Entrypoint and processConfig.Arguments in daemon/execdriver/windows/commandlinebuilder.go :

 // Build the command line of the process commandLine = processConfig.Entrypoint logrus.Debugf("Entrypoint: %s", processConfig.Entrypoint) for _, arg := range processConfig.Arguments { logrus.Debugf("appending %s", arg) if !alreadyEscaped { arg = syscall.EscapeArg(arg) } commandLine += " " + arg } 

Those processConfig.Arguments are populated in daemon/container_operations_windows.go :

 processConfig := execdriver.ProcessConfig{ CommonProcessConfig: execdriver.CommonProcessConfig{ Entrypoint: c.Path, Arguments: c.Args, Tty: c.Config.Tty, }, 

moreover, c.Args is the argument of the container (runtile or CMD parameters)

So yes, the CMD commands execute after docker start .

+3
source

No, the CMD command is only executed when executing a โ€œlaunch dockerโ€ to launch an image-based container.

In the documentation: When used in shell or exec formats, the CMD command sets the command that should be executed when the image is running .

https://docs.docker.com/reference/builder/#cmd

0
source

If you want your container to run the same executable every time, you should use ENTRYPOINT in combination with CMD .

Note: do not confuse RUN with CMD . RUN actually executes the command and captures the result; CMD does nothing during assembly, but indicates the intended command for the image.

https://docs.docker.com/engine/reference/builder/

0
source

Source: https://habr.com/ru/post/975817/


All Articles