Using Docker Compose

Wouldn't it be great if we could define all the containers we need for our service to work in a simple interface? Welcome to Docker Compose. This is an orchestration tool that enables us to do what we just did in a simpler way and, most importantly, in a distributed way.

We can define how our containers interact with each other and what they need in a simple file structure, and then we can transfer that to another host and just run Docker Compose, where it will deploy it all for us.

Docker Compose uses a YAML file, which is a human-readable data serialization language. It's easier to read and understand and at the same time, allows us to make complex configurations.

Let's migrate our two containers to this layout. The only step we need right now is to create a file called docker-compose.yml. Inside, we define our two containers, the network, the ports, and the volumes. Here's an example:

version: "3"
networks:
imagini:
services:
database:
image: mysql:5.7
networks:
- imagini
volumes:
- ${PWD}/mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: imagini
MYSQL_ROOT_PASSWORD: secret
service:
image: imagini:0.0.5
networks:
- imagini
volumes:
- ${PWD}/settings.json:/opt/app/settings.json
ports:
- "80:3000"
restart: on-failure

Let's do this step by step:

version: "3"

First, we indicate that we're defining our services in Docker Compose version 3 syntax and features:

services:
database:
image: mysql:5.7
networks:
- imagini
volumes:
- ${PWD}/mysql:/var/lib/mysql
environment:
MYSQL_DATABASE: imagini
MYSQL_ROOT_PASSWORD: secret

Then, we define our database using image mysql:5.7, attach it to the imagini network, and then use the host mysql folder to store the database and define the two environment variables. This is exactly what we did in the previous command:

    service:
image: imagini:0.0.5
networks:
- imagini
volumes:
- ${PWD}/settings.json:/opt/app/settings.json
ports:
- "80:3000"
restart: on-failure

Finally, we do the same for our service, indicating the name, the imagini:0.0.5 image, the network, the local settings file, and the port assignment. But there's something more, which is very important, and this is the restart policy. Because our database service does not load instantly, our service will most probably fail at first and stop. Docker will then lift it up again and then the database server will be ready, and everything will be up and running.

As we're composing a Docker Compose project, we can avoid using imagini on the service names, and so we need to once again change our settings file to something like this:

{
"db": "mysql://root:secret@database/imagini"
}

We can now try it out by running docker-compose up. This will deploy our containers under the imagini project, which is the folder name:

This will start the containers, but you'll notice that they didn't start detached. To do that, hit Ctrl + C to stop the containers:

Now, let's run this again properly by using the -d parameter:

Great! We can start our service with everything it needs in a single command. You could just share this folder with a co-worker and he or she could have the service up and running in a matter of seconds.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset