dockerdetach

why would we want to use both --detach switch with --interactive and --tty in docker?


I'm reading the docker documentations, and I've seen this command:

$ docker run -d \
     -it \
     --name devtest \
     --mount type=bind,source="$(pwd)"/target,target=/app,readonly \
     nginx:latest

As far as I know, using -d or --detach switch run the command outside of the current terminal emulator, and return the control of terminal back to the user. And also using --tty -t and --interactive -i is completely the opposite. Why would anyone want to use them in a command?


Solution

  • For that specific command, it doesn't make sense, since nginx does not have an interactive component. But in general, it allows you to later attach to the container with docker attach. E.g.

    $ docker run --name test-no-input -d busybox /bin/sh
    92c0447e0c19de090847b7a36657d3713e3795b72e413576e25ab2ce4074d64b
    
    $ docker attach test-no-input
    You cannot attach to a stopped container, start it first
    
    $ docker run --name test-input -dit busybox /bin/sh
    57e4adcc14878261f64d10eb7839b35d5fa65c841bbcb3cd81b6bf5b8fe9d184
    
    $ docker attach test-input
    / # echo hello from the container
    hello from the container
    / # exit
    

    The first container stopped since it was running a shell, and there was no input on stdin (no -i). A shell exits when it finishes reading input (e.g. the end of a shell script).