Search code examples
dockerdockerfiledocker-machinedocker-cli

why docker start command starts containers in detached mode by default


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.


Solution

  • 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!