To Run Services After Deploying The MariaDB Service Using Docker Compose
Tags: docker, mariadb, dockercompose, mysql
Published: 2022-05-28 05:55:29

The `depends_on` key in `docker-compose.yml` can set the order to start running each service such as starting the Node.js service after starting the MariaDB service. However, it cannot control the order to run services after deploying the dependent service as shown the NG case in the figure below. For example, in the case of using MariaDB(a similar database to MySQL), it means that Docker runs any services without waiting to finish initializing MariaDB such as making tables if you just set the `depends_on` keys only.
This article shows the way to run the Node.js service after deploying the MariaDB service as examples to run services after deploying the dependent service using Docker Compose.
The following figure indicates the comparison of running services order between only using the `depends_on` key and this article method.

![The running order comparison](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g5v36vulrpyhdyy7zht1.png)

# Source Code
This sample source code was made based on the source code that [snowcait](https://zenn.dev/snowcait) told me in [the Discussion of this original article (written in Japanese)](https://zenn.dev/link/comments/805422bab480f5). I thank snowcait to give me this idea.
**As important notes**, you should type the user name and password that are initialized in `/docker-entrypoint-initdb.d` to the test command in the `healthcheck` key if you need Docker to wait for the finish to run the script in `/docker-entrypoint-initdb.d` before running Node.js. It is better to make the code to access a new table that was made in `/docker-entrypoint-initdb.d` script if you only make some tables without making other users.

## .github/workflows/test.yml

```yml
name: Actions Test

on:
  push:
    branches:
      - dev-*

jobs:
  sandbag_test:
    runs-on: ubuntu-20.04
    timeout-minutes: 5
    env:
      MARIADB_ROOT_PASSWORD: ${{secrets.MARIADB_ROOT_PASSWORD}}
      MARIADB_DATABASE: ${{secrets.MARIADB_DATABASE}}
      MARIADB_WEB_USER: ${{secrets.MARIADB_WEB_USER}}
      MARIADB_WEB_PASSWORD: ${{secrets.MARIADB_WEB_PASSWORD}}
      MARIADB_API_USER: ${{secrets.MARIADB_API_USER}}
      MARIADB_API_PASSWORD: ${{secrets.MARIADB_API_PASSWORD}}
      MARIADB_PORT: ${{secrets.MARIADB_PORT}}
      DB_HOST: ${{secrets.DB_HOST}}
      WEB_PORT: ${{secrets.WEB_PORT}}

    steps:
      - uses: actions/checkout@v2

      - name: Shutdown MariaDB
        run: sudo service mysql stop

      - name: Run docker-compose
        shell: bash
        run: |
          touch .env
          docker-compose -v
          docker-compose up --build --abort-on-container-exit
        working-directory: ./
```

## docker-compose.yml

```yml
version: '3'
services:
  mariadb:
    image: mariadb:10.7
    container_name: sandbag_mariadb
    environment:
      - MARIADB_ROOT_PASSWORD=${MARIADB_ROOT_PASSWORD}
      - MARIADB_DATABASE=${MARIADB_DATABASE}
      - MARIADB_USER=${MARIADB_WEB_USER}
      - MARIADB_PASSWORD=${MARIADB_WEB_PASSWORD}
      - MARIADB_API_USER=${MARIADB_API_USER}
      - MARIADB_API_PASSWORD=${MARIADB_API_PASSWORD}
      - MARIADB_HOST=%
    tty: true
    volumes:
      - ./sql:/docker-entrypoint-initdb.d
    networks:
      - datastream
    healthcheck:
      test: ["CMD", "mariadb", "-u${MARIADB_API_USER}", "-p${MARIADB_API_PASSWORD}", "-e", "use ${MARIADB_DATABASE}"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 5s
  web:
    env_file:
      - .env
    build:
      context: ./web/.
      dockerfile: "Dockerfile"
    ports:
      - ${WEB_PORT}:${WEB_PORT}
    depends_on:
      mariadb:
        condition: service_healthy
    command: ["npm", "test"]
    volumes:
      - ./volume/log/web:/usr/src/logger/web
    networks:
      - datastream
networks:
  datastream:
```

# The Execution Result of The Source Code 
You can check the execution result of this source code in the following URL.

[https://github.com/KASHIHARAAkira/actions-playground/runs/6634973345?check_suite_focus=true](https://github.com/KASHIHARAAkira/actions-playground/runs/6634973345?check_suite_focus=true)

# Acknowledgement
As I indicated before, [snowcait](https://zenn.dev/snowcait) provided me the most of this source code in [the Discussion of this original article (written in Japanese)](https://zenn.dev/link/comments/805422bab480f5). I thank snowcait to give me this idea.

-----
This original article is the following that is written by me. This is a translation of a portion of this original article from Japanese to English.
{% embed https://zenn.dev/akira_kashihara/articles/e7bbbcf55a936a %}