Kaio
Kaio

Reputation: 230

Differences between detached mode and background in docker

Running docker run with a -d option is described as running the container in the background. This is what most tutorials do when they don't want to interact with the container. On another tutorial I saw the use of bash style & to send the process to the background instead of adding the -d option.

Running docker run -d hello_world only outputs the container ID. On the other hand docker run hello_world & still gives me the same output as if i had run docker run hello_world.

If I do the both experiments with docker run nginx I get the same behavior on both (at least as far as I can see), and both show up if I run docker ps.

Is the process the same in both cases(apart from the printing of the ID and output not being redirected with &)? If not, what is going on behind the scenes in each?

Upvotes: 1

Views: 875

Answers (1)

atline
atline

Reputation: 31574

docker is designed as C-S architecture: docker client, docker daemon(In fact still could fined to container-d, shim, runc etc).

When you execute docker run, it will just use docker client to send things to docker daemon, and let daemon call runc etc to start a container.

So:

docker run -d: It will let runc run container in background, you can use docker logs $container_name to see all logs later, the background happend on server side.

docker run &: It will make the linux command run at background, which means the docker run will be in background, this background run on client side. So you still can see stdout etc in terminal. More, if you leave the terminal(even you bash was set as nohup), you will not see it in terminal, you still need docker logs to see them.

Upvotes: 2

Related Questions