How do i launch a temporary docker container?



  • Man page docker run: http://bit.ly/2h77MqO
    Docker Documentation - docker run: https://docs.docker.com/engine/reference/commandline/run/

    Man page for docker ps: http://bit.ly/2Ac6OAX
    Docker documentation - docker ps: https://docs.docker.com/engine/reference/commandline/ps/

    Man page for docker exec: http://bit.ly/2j3fIxJ
    Docker documentation - docker exec: https://docs.docker.com/engine/reference/commandline/exec/

    Docker can be made to run in a persistent state as well as temporary (ephemeral) state.

    Run docker in ephemeral state.

    $ docker run alpine /bin/sh -c "while true; do ps; sleep 2; done"
    PID   USER     TIME   COMMAND
        1 root       0:00 /bin/sh -c while true; do ps; sleep 2; done
       81 root       0:00 /bin/sh
     1194 root       0:00 ps
    ...
    ...
    ...
    

    You now have docker container running, to see it open an new terminal window and type:

    $ docker ps
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
    caa2d17120dc        alpine              "/bin/sh -c 'while..."   23 seconds ago      Up 22 seconds                           gallant_easley
    

    Docker is not a full OS, but a container under the OS, but you can still get into it, like so:

    -i, --interactive[=false]       Keep STDIN open even if not attached
    -t, --tty[=false]               Allocate a pseudo-TTY
    

    The container ID is obtained from the docker ps command and specify /bin/sh, to get a shell running in the container.

    $ docker exec -it caa2d17120dc /bin/sh
    / # 
    / # ps
    PID   USER     TIME   COMMAND
        1 root       0:00 /bin/sh -c while true; do ps; sleep 2; done
       81 root       0:00 /bin/sh
       86 root       0:00 sleep 2
       87 root       0:00 ps
    

    You can see from the ps, your script is running, once you Ctrl-C out from the terminal running the script, then do another docker ps, the docker container is no longer running.

    $ docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
    

    If you want to see the ones that have exited.

    $ docker ps -a
    


© Lightnetics 2024