Docker Container run

Docker run is a combination of create and start a container. It first creates the container and then starts it. The docker run command first creates a writeable container layer over the specified image, and then starts it using the specified command.

Detach and Run Container in Background

To run a container in the background, we can use docker container run command with -d (detach) mode and specify the container name (or ID) as well as the command to be executed on this container.

Syntax

docker container run <command>

Now, to execute the "sleep 30" command on the ubuntu container and send it to the background, simply provide -d option to run it in detached mode-

learning-ocean:~ gaurav$ docker container run -d ubuntu:14.04 sleep 30
0fbc31ed81ef0f25d245b32d040389f640ce95c58f2d541e9f81bf733e84dcde
learning-ocean:~ gaurav$

Docker Interactive Option (IT)

Docker allows you to run a container in interactive mode as well.

learning-ocean:~ gaurav$ docker container run -it ubuntu:14.04 /bin/bash
root@ad9f71ce3a04:/# #now i am in Container. please notice the hostname in this line
root@ad9f71ce3a04:/# hostname
ad9f71ce3a04
root@ad9f71ce3a04:/# cat /etc/os-release
NAME="Ubuntu"
VERSION="14.04.6 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.6 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
root@ad9f71ce3a04:/#

So by using the above command, users get an interactive bash shell of thecontaine. And if the user wants to come out from the container and stil wants o run it in the background then simply hit [ctrl+pq] from the