DEV Community

Harry@StartQuick Tech
Harry@StartQuick Tech

Posted on • Originally published at startquicktech.Medium on

How Docker Compose helps you setup multi-container application

Lots of companies are using container technology to deploy their applications but their developers are still using the traditional way for the local dev environment set up such as installing framework, dependancies on their local machine.

Docker Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

If you like to learn it with hands-on practice, you can checkout my video which shows you how to use docker compose step by step. Hope you can also leave you comments with your suggestions or anything you wanna learn.

Regarding learning Docker Compose, there are some key points you should understand.

  1. YAML File for Docker Compose:

Below is the example we are using in the video for a local wordpress development environment setup. This is really helpful if you production environment is using container technology such as AWS Fargate, Kubernetes.

version: '3.1'

services:
  wordpress:
    image: wordpress
    build:
      context: .
      dockerfile: ./Dockerfile
    restart: always
    depends_on:
      db:
        condition: service_healthy
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: exampleuser
      WORDPRESS_DB_PASSWORD: examplepass
      WORDPRESS_DB_NAME: exampledb
    volumes:
      - ./varwww:/var/www/html
    networks:
      - wp-network-default  

  db:
    image: mysql:8.0
    restart: always
    environment:
      MYSQL_DATABASE: exampledb
      MYSQL_USER: exampleuser
      MYSQL_PASSWORD: examplepass
      MYSQL_RANDOM_ROOT_PASSWORD: '1'
    healthcheck:
      test: ['CMD', 'mysqladmin', 'ping', '-h', 'localhost']
      interval: 5s
      timeout: 5s
      retries: 5
    volumes:
      - db:/var/lib/mysql
    networks:
      - wp-network-default  

volumes:
  db:

networks:
  wp-network-default:
Enter fullscreen mode Exit fullscreen mode
  • Version: This specifies the version of Docker Compose to use.
  • Services: This section defines the different containers that make up the application. Each container is defined as a service, and multiple services can be defined in a single YAML file.
  • Image: This specifies the Docker image to use for each service. If the image is not found locally, Docker Compose will pull it from a registry.
  • Ports: This maps the ports used by the container to the host machine, allowing the container to be accessed from outside the container.
  • Environment: This section defines environment variables that will be passed to the container. These variables can be used to configure the container at runtime.
  • Volumes: This maps files or directories on the host machine to files or directories inside the container, allowing data to persist even if the container is deleted.
  • Restart: This defines the restart policy for the container. It specifies whether the container should be automatically restarted if it crashes or is stopped.
  1. Commands for start, stop, remove the environment. Below are the most basic commands:
  • Start containers
docker compose up
docker compose up -d # quite mode
docker compose up -d --build # rebuilt the image
Enter fullscreen mode Exit fullscreen mode
  • Stop containers
docker compose stop
Enter fullscreen mode Exit fullscreen mode
  • Stop and Remove containers
docker compose down
Enter fullscreen mode Exit fullscreen mode
  1. Volumes and Networking (Very important for advanced usage)

Volumes are a way to persist data in Docker containers. They allow you to store and share data between containers or between a container and the host machine. When you create a volume in Docker, it creates a separate storage location outside of the container’s file system that is managed by Docker.

There are two types of volumes in Docker: named volumes and host volumes.

  • Named volumes are created and managed by Docker, and are ideal for sharing data between containers.
  • Host volumes, on the other hand, allow you to mount a directory from the host machine into the container, and are useful for persisting data on the host machine.

Volumes are useful for a number of reasons, such as ensuring that data is not lost if a container is deleted, and allowing multiple containers to access the same data. They can be used for a variety of applications, such as storing database data, configuration files, or user data.

Docker networks are used to connect multiple containers together, allowing them to communicate with each other. When you create a network in Docker, it creates a virtual network that allows containers to communicate with each other as if they were on the same physical network.

There are several types of networks in Docker, including bridge networks, host networks, and overlay networks.

  • Bridge networks are the default network type in Docker, and are used to isolate containers from each other.
  • Host networks allow containers to use the host machine’s network stack, and are useful for applications that require low-latency communication.
  • Overlay networks are used to connect containers across multiple Docker hosts.

Networks are useful for a number of reasons, such as enabling containers to communicate with each other, allowing multiple instances of the same container to communicate with each other, and isolating containers from each other for security purposes.

I do use Docker Compose quite a lot in my work. Please feel free to leave your questions and I am happy to chat with you and learn from each other.

Thank you.

Harry@NZ

Top comments (0)