0

I have used run command in docker cli to so when i used the command

docker run --name ubuntu ubuntu

it simply exists because it has no ptty and input output connection and we can attach our terminal using -it flags but when we try to start docke container which is in stopped state why does the container by default start in dettached mode this behaviour has not even been mentioned in documentation.

when i use the command

docker start ubuntu

when container "ubuntu" is in stopped mode the container is up and is running in background but when i use "docker run" command the container simply stops can someone explain this behaviour.

and it would be great if you can provide some material to refer as i found none regarding this behaviour.

gilf0yle
  • 1,092
  • 3
  • 9
  • It is so because it was implemented that way. I do not think there is a deeper meaning behind this. There is - of course - the option to `docker start --attach ...` in order to start attached. – Turing85 Aug 19 '23 at 07:42
  • i saw a article saying run runs the cmd or entry point where as start starts a services instead of running cmd or entrypoint do you know about it? – gilf0yle Aug 19 '23 at 07:50
  • I rarely if ever use `docker start`; the container itself isn't especially valuable, and if you `docker rm` the old container and `docker run` a new one, it will always start from a known state. – David Maze Aug 19 '23 at 11:02
  • Got it thank you will do this @DavidMaze – gilf0yle Aug 21 '23 at 03:40

1 Answers1

1

by running docker container ls -a
you can see your stopped containers and the PID 1 or EntryPoint of the container in COMMAND column.
which is /bin/bash.

that's it , and because you did not attach to the shell , it is exited
it is immediately done and the container is exited , so the status column shows "Exited (0)".
the number inside the parentheses is the Exit code for the container.

if you start the container again i don't think there would be a difference.

you can give a command to overwrite the main EntryPoint for the image which is just /bin/bash or attach to it with -it while running the container for the first time.

suggestion:

docker run -d --name  ubuntu-test ubuntu:20.04  /bin/bash -c "sleep inf"

It will run the container indefinitely until you stop it

Hope that helps!

  • but why is the container not exiting when I do `docker container start` that's my question – gilf0yle Aug 19 '23 at 08:44
  • are you sure that when you start the container the container is running afterwards? i have tested and it's **not** , it performs the same and i can see the exited time changes to "exited x second ago". which means its the same process – AlirezaPourchali Aug 19 '23 at 10:03
  • try creating a container with -it flag and then exit now your container is stopped. Now try starting your container again it will automatically run in background. – gilf0yle Aug 19 '23 at 10:16
  • 1
    just found it by using -it flag it keeps the container's STDIN open even if not attached, with this container is waiting for some process of input so container doesn't have to exit as it has some task to wait other than just bin/bash – gilf0yle Aug 19 '23 at 10:31
  • that is correct , when you start it when with `-it` the pid 1 will be /bin/bash and its waiting on input but you are not attached to it, but when you ru n with -it and close the shell it is exited and so the container is stopped. also after starting container if you exec to the container and run `ps -aux` you can see all the pid including the /bin/bash as pid 1. – AlirezaPourchali Aug 19 '23 at 12:18
  • Great got your point can you give me more info or some resource to know more on this. – gilf0yle Aug 21 '23 at 03:40
  • Answer to this [question](https://stackoverflow.com/questions/75763907/timeout-command-in-docker-container-not-working/75856816#75856816) and the main [doc](https://docs.docker.com/engine/reference/run/) for run command might help more – AlirezaPourchali Aug 21 '23 at 04:56