1

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?

3 Answers 3

2

-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/

8
  • if the container is running in background then why docker container ls -a show exited....it should status as UP and Running
    – Harry S
    Apr 16, 2020 at 16:30
  • 1
    Are you certain that whatever is running in your container isn't crashing or shutting down almost immediately after starting? Apr 16, 2020 at 16:34
  • may be i can try running something under the container and see it crashes or not...was just trying to understand the purpose of -d flag
    – Harry S
    Apr 16, 2020 at 16:37
  • 2
    It's simply stopping your container from using your current command line for input and output (stdin/stdout). It's "detached," now running in the background. Apr 16, 2020 at 16:39
  • Did you find that this answer addressed your question adequately? May 21, 2020 at 20:17
2

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.

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.