Docker – run

Docker run command

Use Docker run command to launch containers from an image. It provides a large number of options.


docker run [OPTIONS] IMAGE [COMMAND] [ARG...]


For example, run the hello-world example officially provided by Docker community to test your installation. Run command will also download the image from docker hub it not found locally.

$ sudo docker run hello-world

As a better use, you can use -i to keep STDIN open and -t to allocate a TTY to the container. You can also use -d to run container in detached (background) mode. I have also run bash shell as a startup script to the container.

$ sudo docker run -it -d ubuntu /bin/bash

Docker – ps

Docker ps command

Use ps command to list the docker containers on your local system.


docker ps [OPTIONS]


To list all running container use the following command.

$ docker ps

docker ps command

Docker ps Options

#1. List All (-a, –all)

Use this switch to show all containers created on local system, either they are in any state.

$ docker ps -a
#2. Show ID Only (-q, –quiet)

This option will only display the numeric id only of the container.

$ docker ps -q
#3. Show Size (-s, –size)

This option will also display the size on disk of the listed containers.

$ docker ps -s

Docker – build

Docker build command

Docker build command is used to build an image from a Dockerfile. For more details visit our Dockerfile and Dockerfile directives tutorial.


docker build [OPTIONS] [Dockerfile PATH|URL]


Let’s create an example Dockerfile in your current directory.

vim Dockerfile

and add the following content.

Now build the image using single dot “.”, as Dockerfile is available in the current directory. Also, use -t image_name to specify tag name to the image.

docker build -t apache_ubuntu .

The build command will pull the image “tecadmin/ubuntu-ssh:16.04” from dokcerhub if not available locally. then it will install Apache2 web server on it. Then set the apachectl command on startup. Also, expose the port 80 to bind it with docker host machine port.

Build Dockerfile from URL

You directly use Dockerfile from remote URLs. For example, I have Dockerfile in your Github repository. Use this

docker build -t apache_ubuntu:latest ""

You can also use a remote tarball to build the image. The archive must have files only, not with the parent directory.

docker build -t apache_ubuntu:latest ""

Docker Build Options

Docker build command provides multiple options to build your images as per your requirements. Below is the list of few options, which is more useful and frequently used by users.

#1. Tag (-t, –tag list)

Use this option to specify tag name for the image. A tag name must be valid ASCII and may contain lowercase and uppercase letters, digits, underscores, periods and dashes. A tag name may not start with a period or a dash. It may contain a maximum of 128 characters.

docker build . -t apache_ubuntu:16.04
docker build . -t apache_ubuntu:latest
docker build . -t ubuntu:apache2.4

To publish your image on dockerhub, you must specify dockerhub username before the image name. For example my dockerhub user is tecadmin. So I build image as

docker build . -t tecrahul/apache_ubuntu:16.04
docker build . -t tecrahul/ubuntu:apache2.4
#2. Filename (-f, –file string)

Use this option to specify the name of your Dockerfile. For example, you have multiple Dockerfile in the current directory with different-2 names.

docker build -f Dockerfile.nginx .
#3. Suppress Output (-q, –quiet)

This option is used to suppress output during the build process. This will show image id after build completed

docker build . -q
#4. Memory Limit (-m, –memory bytes)

You can specify the maximum memory limit the docker build process can be used. To limit 100MB use below option.

docker build . -m 100000000

Docker – Compose Example

Docker Compose Example

This is the step by step tutorial to understand uses of Docker compose. In this tutorial, I will create two Docker containers using Docker compose. One docker container will have MySQL database instance and another Docker container have Apache web server with our dummy application file.

Let’s follow step by step tutorial and watch the things happening there.

Step 1 – Create Directory Structure

First of all, create a directory structure. Here webapp is our web application directory. Also, create a index.html in webapp directory for testing.

Step 2 – Create Dockerfile for Webapp

Now create a Dockerfile in webapp directory to create a customized image for your application including Apache web server.

$ vim  webapp/Dockerfile

add following content

FROM tecadmin/ubuntu-ssh:16.04

RUN apt-get update \
   && apt-get install -y apache2

COPY index.html /var/www/html/
WORKDIR /var/www/html
CMD ["apachectl", "-D", "FOREGROUND"]

Step 3 – Create Docker Compose File

Finally create a docker compose configuration file (docker-compose.yml) file in current directory. This will define all the containers will be used in your current setup.

$ vim  docker-compose.yml

add following content.

Above docker compose file has settings for two containers. The first container is for mysql database server and the second is for web server. The web container will run our application on Apache server. As this is customized we have defined build directory to webapp.

Step 4 – Build Webapp Image

Now, build an image using the following command. This will create an image named apache using Dockerfile and contents from webapp directory.

$ docker-compose build

read the below output of above command. I have skipped some part of output which is not required. The first line of below output shows that it skipped building for db container due to no build defined. For web container it uses webapp/Dockerfile to build an image.

db uses an image, skipping
Building web
Step 1/6 : FROM tecadmin/ubuntu-ssh:16.04
16.04: Pulling from tecadmin/ubuntu-ssh
b3e1c725a85f: Pull complete
4daad8bdde31: Pull complete
63fe8c0068a8: Pull complete
4a70713c436f: Pull complete
bd842a2105a8: Pull complete
c41407f48fa7: Pull complete
1fcfeb9b5ef4: Pull complete
13195a7d2240: Pull complete
b86be64bbda8: Pull complete
8c951fe917dc: Pull complete
f74bc80103b6: Pull complete
Digest: sha256:523d6fbc97954e9f77231bf54bfcfbbdd4805349887477fbac4a63dc735d777d
Status: Downloaded newer image for tecadmin/ubuntu-ssh:16.04
 ---> bb63b492da01
Step 2/6 : RUN apt-get update    && apt-get install -y apache2
 ---> Running in 00be0dd717ce
[[[Removed long output from here]]]
 ---> 41c731590234
Removing intermediate container 00be0dd717ce
Step 3/6 : COPY index.html /var/www/html/
 ---> 42f84d4c2243
Removing intermediate container 945aaee6cbde
Step 4/6 : WORKDIR /var/www/html
 ---> 40bebd21e352
Removing intermediate container e13f5f412906
Step 5/6 : CMD apachectl -D FOREGROUND
 ---> Running in ab0db1ef1c6e
 ---> 587bf2323289
Removing intermediate container ab0db1ef1c6e
Step 6/6 : EXPOSE 80
 ---> Running in 7bcbef52d585
 ---> 8f03d4135394
Removing intermediate container 7bcbef52d585
Successfully built 8f03d4135394
Successfully tagged apache:latest

Step 5 – Launch Docker Containers

Finally launch your containers using docker-compose up command. Use -d switch to run them in daemon mode.

$ docker-compose up -d

You can access your web application running on the apache_web container by accessing your docker host on port 8080. For example, http://dockerhost:8080/ where dockerhost is IP or hostname of your Docker host machine.

Step 6 – Update Content in Web Application

Let’s make a change in your web application. I have added some more content to webapp/index.html file as following.

$ echo "Welcome to Docker Compose Tutorial" >> webapp/index.html

Now use the following commands to rebuild webapp container and relaunch using docker-compose.

$ docker-compose build
$ docker-compose up -d

Check the output of the command.

You can see that mysql_db container is showing unchanged as nothing changed there. The only apache_web container has been recreated due to new build found for the image used for that.

Again access your web application on port 8080 of docker host machine. You will see the updated content here.


Docker – Introduction

Docker Introduction

Docker is a software container platform. It is an open platform for developing, shipping, and running applications. A Docker container wraps all the software components to run an application. Using Docker you can provide an isolated environment for your every individual application.

Docker Engine

“Docker Engine” is the core of Docker which creates, ship and runs Docker containers. Docker Engine provides client-server application architecture with the following major components.

  • A server daemon process for continuous running.
  • The REST API to talk to the daemon and send instruction to it.
  • A command line interface client.

Docker – Compose

Docker Compose

Docker Compose is another best tool for docker to setup multi-container environments. Using this create a single compose file with defining all the containers with there environments. You can easily use single command to build images and run all the containers.

There is the three-step process to work with Docker Compose.

1. Define application environment with Dockerfile for all services.
2. Create a docker-compose.yml file defining with all services under application.
3. Run docker-compose up to run all services under applications.

Step 1 – Prerequisites

You must have Docker Engine installed on your system. If you don’t have already installed, Visit our Docker installation section of this tutorial.

Step 2 – Install Docker Compose

Visit the Docker compose official Github page and download the latest version of Docker compose tool. You can also install Docker compose 1.16.1 using the following command. Before installing the specific version, You must check the compatibility on releases page with your docker version.

$ curl -L`uname -s`-`uname -m` > /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose

Step 3 – Docker Compose File Example

A docker-compose.yml file required to create to start working with the docker compose. Below is a sample configuration file of version 3. This file have only one service added in it named web.

Step 4 – Docker Compose CLI Reference

The docker-compose command provides a number of subcommands to manage Docker containers with docker-compose. Please find below details of the subcommands. Before reading below commands remember that you passed service name as an argument (not container name)

build –

The build option is used to build images for services for which build is defined.

$ docker-compose build             ## Build all services
$ docker-compose build web         ## Build single service

up –

Use to create docker containers with available services in docker-compose.yml file in current directory. Use -d switch to launch containers in daemon mode.

$ docker-compose up -d            ## Create all containers
$ docker-compose up -d web        ## Create single container

down –

This will stop and delete all containers, network and associated images for the services defined in a config file

$ docker-compose down           ## Restart all containers
$ docker-compose down web       ## Restart single container

ps –

This will list all containers created for the services defined in a config file with there status, port bindings and command.

$ docker-compose ps 

exec –

This will execute a command to the running container. For example list files in container associated with web service.

$ docker-compose exec web ls -l

start –

This will start stopped containers of the services defined in config file

$ docker-compose start            ## Start all containers
$ docker-compose start web        ## Start single container

stop –

This will stop running containers for the services defined in config file

$ docker-compose stop             ## Stop all containers
$ docker-compose stop web         ## Stop single container

restart –

This will restart containers of the services defined in config file

$ docker-compose restart           ## Restart all containers
$ docker-compose restart web       ## Restart single container

pause –

This will pause running containers for the services defined in config file.

$ docker-compose pause            ## Pause all containers
$ docker-compose pause web        ## Pause single container

unpause –

This will start paused containers for the services defined in config file.

$ docker-compose pause            ## Start all paused containers
$ docker-compose pause web        ## Start single paused container

rm –

This will remove stopped containers for the services defined in config file.

$ docker-compose rm               ## Start all paused containers
$ docker-compose pause web        ## Start single paused container

Docker – Networking Example

Docker Networking Example

As you already read our previous tutorial Docker Networking. This tutorial, we will create a small docker network with 2 docker containers as following.

> MySQL – A relational database server.
> PHPMyAdmin – A web based interface to manage MySQL server.

In this tutorial, you will learn how to access MySQL server using PHPMyAdmin running on the different container.

1. Create Network

First of all, create a new docker network. Use below command to create new bridge network named my-bridge-network.

$ docker network create -d bridge my-bridge-network

2. Run MySQL Container

Now, run a new MySQL docker container. Set the default root user password with MYSQL_ROOT_PASSWORD variable as showing in below command.

$ docker run --name mysql -e MYSQL_ROOT_PASSWORD=secret -d mysql/mysql-server

After creating container add this to our network.

$ docker network connect my-bridge-network mysql

Now view the new IP address of the MySQL container, which is required in next step.

$ docker inspect mysql | grep "IPAddress"

3. Run PHPMyAdmin Container

Now run a new Docker container of phpmyadmin using the following command. Change the PMA_HOST value with the IP address of MySQL container IP address get in last step.

$ docker run --name phpmyadmin -d -e PMA_HOST= -p 8080:80 phpmyadmin/phpmyadmin

Add this container to our network.

$ docker network inspect my-bridge-network

4. Inspect Our Network

As you have attached both containers to our new network. Let’s inspect the current network settings.

$ docker network inspect my-bridge-network

You will get the result some like below.

5. Allow MySQL to PHPMyAdmin Host

The default MySQL doesn’t allow remote hosts to connect. So allow phpmyadmin for MySQL connection. Get shell access to your MySQL container using below command.

$ docker exec -it mysql bash

Login to your MySQL server using the password provided during instance creation.

bash-4.2# mysql -u root -p

Create a new user with phpmyadmin host ip address. In my case phpmyadmin host ip address is ‘‘ as shown in above step.

6. Access MySQL with PHPMyAdmin

Finally, Connect your docker host system on port 8080 to access phpmyadmin web user interface.

Use MySQL credentials created in above step to login to PHPMyAdmin.

Docker – Networking

Docker Networking

Docker provides an option to create and manage there own network for the networking between docker containers. Use docker network subcommand to manage the Docker networking.


docker network [options]

Use below tutorial to create, list and manage Docker networking.

List Docker Networks

Use ls option with docker network command to list currently available network on docker host.

docker network ls

Create Docker Network

Docker provides multiple types of network. Below command will create bridge network on your system.


docker network create -d [network_type] [network_name}


docker network create -d bridge my-bridge-network

Connect Container to Network

You can connect any container to existing docker network by using containers name or ID. Once the container connected to the network, it can communicate with other containers in the same network.


docker network connect [network_name] [container_name]


docker network connect my-bridge-network centos

Disconnect Container from Network

You can disconnect container from specific network any time using the following command.


docker network disconnect [network_name] [container_name]


docker network disconnect my-bridge-network centos

Inspect a Docker Network

Use inspect option with docker network comment to view the details of docker network

docker network inspect my-bridge-network

You will get the results like below.

Remove a Docker Network

Use rm option to remove any existing unused Docker network. You can specify one or more networks with space separated to remove.


docker network rm my-bridge-network network2 network3

You can also remove all unused networks from docker host using prune option.

docker network prune

Docker – Installation

Docker Installation

We have prepared tutorials for advance docker installation. Use links to get access for docker installation.

> Install Docker on CentOS, RedHat & Fedora
> Install Docker on Ubuntu & Debian

To install docker from default repositories follow this tutorial.

#1. Requirements

Docker required 64-bit operating system with Kernel >= 3.10. Older versions of Kernel have some missing requirements to run all features of Docker.

$ uname -r


#2. Install Docker

Docker is available under default repositories of Latest Linux systems. You simply install them using package management tools.

First, you need to remove the current installed old version of docker or docker engine.

## For Debian & Ubuntu systems
$ sudo apt-get remove docker docker-engine

## For CentOS & Redhat systems
$ sudo yum remove docker docker-engine

Now install the latest version of docker

## For Debian & Ubuntu systems
$ sudo apt-get update 
$ sudo apt-get install docker-engine   

## For CentOS & Redhat systems
$ sudo yum install docker-engine

Docker – Manage Ports

Manage Ports in Docker

The Docker containers run services inside it on a specific port. To access that services of container running on a port, You need to bind container port with some Docker host port.

Example 1

Have a look at below image. You will see that docker host is running two containers, one is running Apache which has some website and other have MySQL.

Now, you need to access the website running on Apache container on port 80. Let’s bind docker host port 8080 to containers port 80. You can also use port 80 on docker host as well.

The second container has MySQL running on port 3306. There are other ways to access MySQL from the host machine. But for this tutorial, I have bind docker host port 6603 to containers port 3306. Now you can directly access MySQL from Docker container by connecting host machine on port 6603.

The following commands will bind host system port with containers port.

$ docker run -it -p 8080:80 apache_image
$ docker run -it -p 6603:3066 mysql_image

Example 2

In the second example use our sample project available on Github. This will show you a running example of port binding. Just clone the repository using the following command.

$ git clone
$ cd dockerfile

Now build the docker image with name apacheimage.

$ docker build -t apacheimage .

Now run the container using docker run command. The Apache service will start on the container on port 80. You need to specify -p 8080:80 to bind host system port 8080 with container port 80.

$ docker run -it -p 8080:80 apacheimage

Now access host machine IP with port 8080 in the web browser. You will get a page running on container’s Apache service like below. My host machine IP is

More Examples

You can bind multiple ports as well with a single container, but make sure to EXPOSE all ports in Dockerfile before building the image.

$ docker run -it -p 8080:80,8081:443 image_name

If you need to bind port with the specific interface of host machine define its IP address as below. The below example, port 8080, 8081 will accessible with IP only.

$ docker run -it -p, image_name
$ docker run -it -p, image_name