A photo of Harshad Yeola

Harshad Yeola

Linux Enthusiast & SysAdmin

Email Twitter Google+


Docker - Running Containers

Overview

NOTE! For running docker containers you must have docker engine installed. We assume that you already installed Docker.

Docker Containers

For running applications inside containers you will need a commad docker run. Following format will help you understand running docker containers.

$ docker run <image> <command>

To Illustrate above command lets launch first container.

$ docker run ubuntu /bin/echo Hello world
Hello world

In above example

  • ubuntu - Is an image you run containers with. you are running command within Ubuntu OS. This image if not present locally is pulled from public image registry Docker Hub

  • /bin/echo - Is the command you run inside container running with specified image.

Docker Interactive Containers

In previous section containers remained active for the time of execution of command specified. So Let’s run the containers interactive manner.

$ docker run -t -i <image> <command>

To Illustrate above command lets run another container.

$ docker run -t -i ubuntu /bin/bash
root@af8bae53bdd3:/#

In above example

  • -t - Is the flag which assigns psuedo-tty or terminal inside container.

  • -i - Is the flag which allows you to run container interactively.

  • /bin/bash - Launches a bash shell inside container.

Lets run some more commands inside the same container

root@af8bae53bdd3:/# pwd
/
root@af8bae53bdd3:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

When completed, run the exit command or enter Ctrl-D to exit the interactive shell. This will also stop the running container.

Docker Daemonized Containers

If you want to run your application in daemonized way, you can do this in following manner

$ docker run -d <image> <command>

To Illustrate above command lets run a container

docker run -d ubuntu /bin/sh -c "while true; do echo hello world; sleep 1; done"
1e5535038e285177d5214659a068137486f96ee5c2e85a4ac52dc83f2ebe4147

In above example

  • -d - flag runs the container in the background (to daemonize it).

In this post we have studied three different ways to run containers.





Post Navigation