How to play with Docker containers on Linux
In our previous article, we have instructed about Docker installation and configuration on Major Linux distribution such as Debian, Ubuntu, LinuxMint, CentOS, RHEL, Fedora, openSUSE, Arch Linux and also we have played around with Docker Images. Now, we are going to play with Docker containers. Two types of containers are available Interactive & Daemonized. Interactive container runs in the foreground. Daemonized container runs in the background.
1) Run a Interactive Docker container
Docker Hub repository holds multiple variants of an image. say for example,
ubuntu image having multiple versions like 12.04, 14.04, 14.10, 15.04 & 15.10. Each variant is identified by a tag. so whenever we run docker better to use
tag so that we can run the specific one. If you don’t specify a variant, by default Docker will use latest version of image. To run container we need to specific the format like below.
[Docker Command Syntax Usage] [docker [command] [flags] [arguments].....] [Run a Docker Container] # docker run -ti ubuntu:12.04 /bin/bash ro[email protected]:/# cat /etc/*-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=12.04 DISTRIB_CODENAME=precise DISTRIB_DESCRIPTION="Ubuntu 12.04.5 LTS" NAME="Ubuntu" VERSION="12.04.5 LTS, Precise Pangolin" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu precise (12.04.5 LTS)" VERSION_ID="12.04"
Note : To exit from the interactive session, use Ctrl+p and Ctrl+q keys.
- docker : Applicatin binary
- run : Docker Command
- -ti : Flags: (-t) enable terminal inside our new container, (-i) making an interactive connection
- ubuntu:12.04 : Image with tag
- /bin/bash : This will launch a Bash shell inside our container
1a) Run a Daemonized Docker container
For Daemonized Docker container, no need to go inside container like Interactive and we can do everything from host system itself.
# docker run ubuntu:12.04 cat /etc/issue Ubuntu 12.04.5 LTS \n \l
2) Playing inside container
Once we launched the container which will display similar like below. Now, we are in inside container and going to execute few commands and when you’re done you can use the exit command or enter Ctrl-D to finish.
[email protected]:/# pwd [email protected]:/# free -m [email protected]:/# ls -lh [email protected]:/# exit
3) List Docker Containers
When you are launching container, docker will automatically generate Unique container ID for that. Make sure you should not the container id by issuing the below command. So that we can start,stop,attach the container again when it’s required.
[List Out Running containers] # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5064f89fa47b ubuntu:12.04 "/bin/bash" 54 seconds ago Up 53 seconds loving_bardeen [List Out All containers (Include Running and Non-Running)] # docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0d602401979b ubuntu:12.04 "cat /etc/issue" About a minute ago Exited (0) About a minute ago cranky_sinoussi c1e38de2dd95 ubuntu "cat /etc/issue" 2 minutes ago Exited (0) 2 minutes ago grave_wing be59a09e9af5 centos "cat /etc/issue" 2 minutes ago Exited (0) 2 minutes ago admiring_mcclintock 39f02c8657b3 centos "cat /etc/issue" 5 minutes ago Exited (0) 5 minutes ago amazing_murdock 0182f4f65333 ubuntu:12.04 "/bin/bash" 25 minutes ago Exited (127) 23 minutes ago berserk_einstein 38d0baa625e9 centos:latest "/bin/bash" 51 minutes ago Exited (0) 44 minutes ago tiny_franklin ab27e46390c2 centos "/bin/bash" 56 minutes ago Exited (0) 51 minutes ago sleepy_wing 7b0fbc7f3136 hello-world "/hello" About an hour ago Exited (0) About an hour ago naughty_mccarthy 11fc40821bee hello-world "/hello" 5 weeks ago Exited (0) 5 weeks ago tender_engelbart
4) Start Docker container
If you know the container ID, we can easily start,stop the container by firing the below command. Not only container id we can use auto generated name also.
# docker start [container ID or Name] # docker start c1e38de2dd95
5) Attach/Reconnect Docker container
If you want to reconnect running container, we can easily attach it by using below command.
# docker attach [container ID or Name] # docker attach c1e38de2dd95
6) Stop Docker container
If you want to stop the running container, use the below command to stop it.
# docker stop [container ID or Name] # docker stop c1e38de2dd95
7) Delete Docker container
You have played around the container and want to Remove/Delete, you can do by running below commands. Make sure the container should be stop before proceeding removal.
# docker rm [container ID or Name] # docker rm c1e38de2dd95
8) Stop Docker container from Host
To stop running container from Master Host.
# docker kill [container ID or Name] # docker kill 5064f89fa47b