Mind~G
Docker

Compose

Docker Compose is allowing you to define and run multi-container Docker applications using a simple YAML file. It is used to manage applications that consist of multiple containers, making it easier to define, run, and scale them.

What is Docker Compose?

Docker Compose is a tool that lets you define and run multi-container applications using a simple YAML file called docker-compose.yml. This file describes the services, networks, and volumes needed for your application. With Docker Compose, you can start all your containers with a single command, making it easier to manage complex applications.

Services

In a docker-compose.yml file, you define each container as a service. Each service can have its own configuration, such as the image to use, environment variables, ports to expose, and volumes to mount. Here’s a simple example of a docker-compose.yml file

Networks

Networks allow containers to communicate with each other. You can define custom networks in your Compose file to control how services interact.

Volumes

Volumes are used to persist data across container restarts. You can define volumes in your Compose file to ensure that data is not lost when containers are recreated.

Example of a docker-compose.yml file:

name: my_app
 
services:
  web:
    image: nginx:latest 
    ports:
      - "80:80"
    networks:
      - my_network
 
  app:
    image: my_app_image
    environment:
      - DATABASE_URL=mysql://db:3306/mydb
    networks:
      - my_network
 
  db:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: example
    volumes:
      - db_data:/var/lib/mysql
    networks:
      - my_network
 
networks:
  my_network:
    driver: bridge
 
volumes:
  db_data:
    driver: local
 

Breakdown of the Example:

  • version: Specifies the version of the Compose file format.
  • services: Defines the containers that make up your application.
    • web: Runs an Nginx server, exposing port 80.
    • app: Runs your application, connecting to the database service.
    • db: Runs a MySQL database with a specified root password and mounts a volume for data persistence.
  • networks: Defines a custom network called my_network using the bridge driver.
  • volumes: Defines a named volume db_data for the MySQL database.

Running Docker Compose

To run your application defined in the docker-compose.yml file, you can use the following command:

Start

docker-compose up

Stop

docker-compose down

Note:

In the service means we can simply run the image like for Nginx we can simply use docker run nginx:latest and for MySQL we can use docker run mysql:latest but the main benifit of using compose is that we can run multiple containers at once and also we can define the network and volume in a single file.

Custom Compose File

Like we have NodeJs application and it is developing phase so we want to create a docker file. but there is one catch how we can define that NodeJs application in docker-compose.yml file.

  • Push the docker image to the docker hub then define the service in the docker-compose.yml file. but there is one issue here as we know that the NodeJs application is in developing phase so we need to rebuild the image every time we make changes in the code.
  • So we can use the build option in the docker-compose.yml file to build the image from the Dockerfile in the current directory.
  • We can use the docker hub image in docker compose file if your project is ready to deploy and you want to use the image from the docker hub.

Code

name: my_node_app
 
services:
  node_app:
    build: 
      context: .
      dockerfile: Dockerfile
    container_name: my_node_app
    ports:
      - "3000:3000"
    networks:
      - my_network
 
  db:
    image: postgres:latest
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
    volumes:
      - db_data:/var/lib/postgresql/data
    networks:
      - my_network
 
networks:
  my_network:
    driver: bridge
 
volumes:
  db_data:
    driver: local

In this example, the node_app service is built from the Dockerfile in the current directory. The db service uses a PostgreSQL image from Docker Hub. Both services are connected to a custom network and use a named volume for data persistence.

On this page