Harry S
Harry S

Reputation: 73

Docker Container run -d flag

I am running docker run container command in 2 ways

  1. docker container run 4e5021d210f6
    • It shows container exited N seconds ago on running docker container ls -a
  2. docker container run -d 4e5021d210f6
    • It also shows container exited N seconds ago on running docker container ls -a but do get some long string "954f102a3c83f6fb7c83189415448c4bb101ff1afe775f2d9b7b4cd70f4e137e" (I do not know what is this number)

what is the difference between the execution of command 1 and command 2?

Upvotes: 2

Views: 6342

Answers (3)

TassosK
TassosK

Reputation: 293

You must use docker ps to see containers running id, status etc, you can also use docker run -it image_name bash if you want to run commands through your terminal inside the container. -d option is mostly used when you have defined some operations with a Dockerfile and you don't want to interact with the container. So, you run your image's container in --detach (-d) mode, to run in the background.

Notice, that you have to perform docker stop container_id in order to stop it.

Upvotes: 2

Josh E
Josh E

Reputation: 7432

The -d parameter indicates to Docker that you do not want to attach to the container through stdin/out. In other words, you are asking to run the container in a background, non-interactive mode. The string printed is the unique ID of the newly created container which you can use with commands like docker inspect

In either command you're executing, your containers are only running for a very short amount of time before stopping. This can be because the container requires arguments to docker run, or because the container performs a one-time task or job before exiting. Therefore, the end-result you observe is the same. If the -d were omitted and the container ran for a longer amount of time you would see this more clearly

Upvotes: 1

Joshua Schlichting
Joshua Schlichting

Reputation: 3450

-d will run the container in the background. It's the same as adding --detach. What is being printed when you use -d is the container's ID.

https://docs.docker.com/engine/reference/commandline/run/

Upvotes: 4

Related Questions