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 "https://github.com/docker/compose/releases/download/$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 https://github.com/docker/compose/releases/download/1.23.1/docker-compose-`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'

services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: my_secret_pw_shh
      MYSQL_DATABASE: test_db
      MYSQL_USER: devuser
      MYSQL_PASSWORD: devpass
    ports:
      - "9906:3306"
  web:
    image: php:7.2.2-apache
    container_name: php_web
    depends_on:
      - db
    volumes:
      - ./php/:/var/www/html/
    ports:
      - "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
The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video Conferencing and social media domain. The motive of creating cutehits was just to share the knowledge/solutions I get to know during my day to day life so that if possible I can help someone for same problems/solutions. CuteHits.com is a really a very effort for sharing knowledge to rest of the world. For any query/suggestion about same you can contact me on below details:- Email: shekharmca2005 at gmail.com Phone: +91-9560201363

Latest posts by Chandra Shekhar (see all)

You may also like...