Using docker in real world


In this article we will read about running multiple containers together  such that behind the scene all containers are running individually but serving for single application. So this article would more towards how we use Docker in real life.

Docker Compose

In order to support multiple container in one, docker has come with unique tool “Docker Compose”
Docker Compose: It is a tool for defining and running multiple containers on docker. We can create and run multiple different containers. With the docker compose we can run these multiple containers as one. For example we might want that nginx and mysql both should start same time with same service.

Installing Docker Compose

For Windows:
You must have “Docker for Windows” installed.
Start your powershell:
Step 1: In Powershell, since Github now requires TLS1.2, run the following:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Step 2:

Invoke-WebRequest "$dockerComposeVersion/docker-compose-Windows-x86_64.exe" -UseBasicParsing -OutFile $Env:ProgramFiles\docker\docker-compose.exe

Where $dockerComposeVersion will be your docker version like 1.23.1
For Linux

sudo curl -L`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

Next command to install docker compose

sudo chmod +x /usr/local/bin/docker-compose

Just to verify docker compose

docker-compose --version

Docker compose in action

We are going to create a docker-compose.yml file which will setup LAMP in one go. We only have to do two things:-
i) create docker-compose.yml file (see more on it below)
ii) Execute the command docker-compose up -d
You are all done. Just open http://localhost:8100/ in your browser.
Populate docker-compose.yml file, content are as below

# ./docker-compose.yml

version: '3'

    image: mysql:5.7
      MYSQL_ROOT_PASSWORD: my_secret_pw_shh
      MYSQL_DATABASE: test_db
      MYSQL_USER: devuser
      MYSQL_PASSWORD: devpass
      - "9906:3306"
    image: php:7.2.2-apache
    container_name: php_web
      - db
      - ./php/:/var/www/html/
      - "8100:80"

Code Analysis:
version: It specifies which docker-compose syntax version that we are going to use, in this case the version 3, this is the latest main version available
services: This section contain one or more services and under each service name it contain service description. For example we have two services i) db ii) web
image: It lets docker know what image we want to use to build our container
environment: It creates the list of environment variables.
ports:we are telling docker to map the port 9906 on our host, to the port 3306 on the container
container_name: Specify a custom container name, rather than a generated default name.
depends_on: Express dependency between services. It will start the service db (if not started first by default)
volumes: Mount host paths or named volumes, specified as sub-options to a service.It specify bind mount.

Data Layer in Docker

In order to handle data from this data layer (so that we can either take it or share it with any containers),we have three mechanism:-

  • Volumes :Volumes are stored in a host file system that is managed by Docker (For example /var/lib/docker/volumes/on Linux ). The idea is although, this is external file system but non-docker system shouldn’t modify this file system. Volumes are the best way to persist data in the docker. This is really important when you are storing the data in the cloud.
  • bind mounts : Bind mounts are the mounting such that you can store it anywhere in the host machine. Non docker processes on the Docker host or a docker container can modify them at any time.
  • tmps mount :these are stored in the host system’s memory only. And are never written to host systems file system.

Command to get network ip of running container

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id

For example

sudo docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' c5dd94808522

Know which linux you are using

uname -a

Related posts:

The following two tabs change content below.

Chandra Shekhar

Consultant at Infogain
Chandra Shekhar is a LAMP Developer,Thinker,Blogger and currently working as Magento(Ecommerce) specialist.. I am always open to learn new things in LAMP as well as other newer technology. In the journey of my last 9 years of web and product development, I have decided to share my knowledge and experience to Open Source Community in 2011. In this regard I have started Since then I am actively sharing my knowledge on various LAMP technology through’s technical ... Read More about Chandra Shekhar

Latest posts by Chandra Shekhar (see all)