DOCKER LEARNING IV

Hasan Özdemir
6 min readDec 31, 2021

Hello everyone. I hope all you guys doing great during COVID-19 times.

In this blog I will be talking about below topics.

  • Introduction to YAML
  • Installation to Docker and Python
  • Docker Compose Fundamentals
  • Docker Compose with multiple services
  • Docker Compose Use Case: Voting App Example

Let’s get started…

One of the basic principles in container architectures, especially in Docker and similar structures, is that each container does a single job, running software for a single job.

Today, many services consist of more than one component and it is not the right approach to solve all these processes with a single container.

For example, to install WordPress, we need a Apache Server with PHP module activated, a MySQL or MariaDB database. We need to create two different container to manage this process in a right approach

When working with multiple containers, the difficulty arises when establishing / managing communications.

We will learn how to describe multiple containers, communications between containers, how to run and how to logging with Docker Compose.

INTRODUCTION TO YAML

YAML is a markup language designed to express data in programming languages more appropriately for humans.

We are going to use YAML files for Docker Compose & Docker Stack therefore it is good to know basics of YAML.

This block format uses hyphen+space to begin a new item in a specified list. Observe the example shown below

days_of_week:
- Monday
- Tuesday
- Wednesday
- Thursday
- Friday
- Saturday
- Sunday

YAML has couple of different use cases but this format is the common one. For further reading you can read their official docs.

INSTALLATION

Docker Compose does not come with Docker by default. Let’s install Docker Compose via pip package manager to our system.

sudo apt install python3-pip
sudo pip install docker-compose
$ sudo apt install python3-pip -> install package manager to Linux
$ sudo pip install docker-compose -> install package to Linux
$ docker-compose -> to confirm installation of Docker Compose

What we’ve done so far is working. We firstly installed pip package manager and after all we installed docker-compose package.

NOTE : PIP allows you to install and manage additional packages that are not part of the Python standard library. I have also uploaded package about Turkish ID & TAX number. You can check it out via this link

First Docker Compose File

In default system try to read docker-compose.yml file therefore let’s create our file with the same name.

$ cat > docker-compose.yml
version : '3'
services:
web:
image:nginx
ports:
-"80:80"

Save and click on CTRL + C on your terminal. Let’s get into our file and see what we have done so far.

docker-compose.yml
  • 1st line must include version information
  • 3rd line showing which services / applications we are going to use.
  • 5th line showing which image we are going to use.
  • 6th line showing our project will run in which port.

NOTE : Always put one space after ‘:’ (double quote) otherwise you can encountered with invalid YAML file error. Try to leave same indentiation for your YAML file. I prefer to leave two spaces for each indent.

Let’s raise the system

$ docker-compose up -> start all services
docker-compose up output
$ docker-compose down -> stop all services

NOTE : If we want to work during our system arise we can use this command.

$ docker-compose up -d -> start services with terminal free mood

NOTE : After run our project if we want to scale a project that does not open port we can use this command

$ docker-compose scale -> scale services

DOCKER COMPOSE with MULTIPLE SERVICES

$ cat > docker-compose.yml
version: '3'
services:
database:
image: mariadb
volumes:
- database:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: password
adminer:
image: adminer
ports:
- '80:8080'

What we done ? We try to create adminer service. We used with adminer service MariaDB database.

Also we’ve added volumes detail because when we stop our service with docker-compose down’ command all changes will be deleted. Volumes is solving this problem. volumes allow us to make changes permanent

NOTE : When you creating a stateful services volumes is necessary to add to keep changes.

After preparing your docker-compose.yml file feel free to arise your service.

Creating Images with Docker Compose

We can automate creating docker images process with docker compose service

We must pass the path of Dockerfile to automate process. In fact we can automate creating images, connecting services each other and run the images.

$ cat > docker-compose.yml
version: '3'
services:
random:
build: .
ports:
- '80:80'

NOTE : We want it to create an image using the Dockerfile and context in the directory you are in, by giving the value a dot(.) to the a parameter.

$ docker-compose build  # create image
$ docker-compose up # to run the service

CREATING NETWORK

Docker Compose in default creates network when we called docker-compose up command. If you want, we can include certain services in the same virtual networks to communicate with each other. We use for this networks section in docker-compose file. Let’s look at it our example.

$ cat > docker-compose.yml
version: '3'
services:
random:
build: .
networks:
ports:
- '80:80'
networks:
- network_1
- network_2
- network_3
networks:
network_1:
network_2:
network_3:

We created three different networks and then we connected service to those networks. For sure we also can connect one of the server to only one of the network.

DOCKER COMPOSE EXAMPLE PROJECT

In this section we will clone example-voting-app from dockersamples repository and launch the app.

Voting app is simply distributed under the Apache 2.0 license, this application includes a simple survey system and a result display screen. This application use microservices architecture and includes postgres, redis, nodejs, .NET and Python. Let’s see how it is works.

$ git clone https://github.com/dockersamples/example-voting-app
$ cd example-voting-app
$ docker-compose up

What we done so far ?

1st : We cloned repository from Github

2nd : We moved into project root folder

3rd : We launched voting app

4rd : Open browser and go to -> 127.0.0.1:5000 and test the application

NOTE : I extremely recommend you to pay attention of docker-compose.yml file to understand how it is works. This project includes 2 network creation, 1 volume creation and 5 service creation and some of them work outside of the network and some of through network.

Thank you for reading my blog. Feel free to leave me your questions.

I wish happy new year to all of you guys. Stay healthy and safe.

I hope will be better :)

--

--