Global Statistics

All countries
229,797,847
Confirmed
Updated on 21/09/2021 1:56 am
All countries
204,713,503
Recovered
Updated on 21/09/2021 1:56 am
All countries
4,712,944
Deaths
Updated on 21/09/2021 1:56 am

Global Statistics

All countries
229,797,847
Confirmed
Updated on 21/09/2021 1:56 am
All countries
204,713,503
Recovered
Updated on 21/09/2021 1:56 am
All countries
4,712,944
Deaths
Updated on 21/09/2021 1:56 am

What is Docker Compose and how is it used? – CloudSavvy IT

Docker Compose is a tool that you can use to centrally manage the deployments of many different Docker containers. It is an important tool for any application that needs multiple microservices, as it allows each service to be easily in a separately managed container.

What does Docker Compose do?

Docker containers are used to run applications in an isolated environment. Nowadays it is quite common to see application deployments made in Docker due to the many benefits it provides. However, it is often not as simple as running a single container. Typically you may have many containers that come together to act as a cohesive service made up of many moving parts.

Managing all of this at deployment time is tricky, so to clean it up, Docker provides Docker Compose, a configuration tool used to run multiple containers at the same time. You can define all settings in a YAML file and then start all containers with one command.

Rather than having all your services in one large container, Docker Compose allows you to divide them into individually manageable containers. This is better for build and deployment as you can manage them all in separate code bases and there is no need to manually start each individual container.

Using Docker Compose is a three-step process:

  • Create the component images using your Docker files or extract them from a registry.
  • Define all component services in one docker-compose.yml proceedings.
  • Run them all together using the docker-compose CLI.

Docker Compose is not another type of Dockerfile. You will still need to create and publish your Docker containers using a Dockerfile. But instead of running them directly, you can use Docker Compose to manage the configuration of a multi-container deployment.

How do you use Docker Compose?

The configuration of a Docker composition file is done in docker-compose.yml. You don’t need to put this in the root of your project like a Dockerfile. In fact, it can go anywhere as it doesn’t depend on any other code. However, if you are compiling the images locally, you will need to go into a project folder with the code that is being compiled.

A Compose configuration file will look similar to the following. This setup runs a WordPress instance using the wordpress Docker Hub container. However, this depends on a MySQL database, which is also created by Compose.

version: '3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: rootpasswordchangeme
       MYSQL_DATABASE: wordpress
       MYSQL_USER: usernamechangeme
       MYSQL_PASSWORD: passwordchangeme

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: usernamechangeme
       WORDPRESS_DB_PASSWORD: passwordchangeme
volumes:
    db_data:

Let’s take a look at the format of this file. First, a version number, as the syntax can change depending on the version you are using.

Here is a list of services. The first is called “db” and uses the mysql:5.7 container, configured to always restart, and with environment variables to configure the database with a username and password. To keep data on reboots, this image is configured with a Docker volume mounted in the MySQL data directory.

The other service is “wordpress”, which is dependent on the database service, ensuring that Docker will make sure the database is started before running. It exposes port 80 as port 8000 and sets some environment variables so that it can connect to MySQL. Note that the database host is set to db:3306, which tells the WordPress container to connect to the “db” service.

Lastly, the volumes are defined for persistent storage. Optionally, you can so define custom networks for containers. There are many extended options that you can configure, so if you are looking to do something specific you should refer to the documentation for Docker Compose.

Once configured, starting this service is easy. Just run docker-compose up, which will extract all the necessary containers and start its services.

docker-compose up -d

And you should see the services running on the system with docker ps. In this case, you will see WordPress working properly.

RELATED: How to install Docker and Docker Compose on Linux

Build with Docker Compose

Docker Compose can also be used within a Dockerfile project and can be configured to build and run an image locally instead of pulling it from the Docker Hub.

To do so, you simply need to add a build section on service. You can configure different context directories as well as different Docker files for different images.

version: "3.8"
services:
  webapp:
    build:
      context: ./dir
      dockerfile: Dockerfile-alternate
    image: imagename:tag

In this case, the image: The variable is also set, but here it is used to label the image created by Docker Compose.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Hot Topics

Related Articles