skip to Main Content

I’m working on Debian-based Virtual Box

I have docker-compose yaml file:

version: '3' 
services:
  mydb:
    build: ./db/
    networks:
      - dockercompose-frontend 
    volumes:
      - server_db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=debian
    healthcheck:
      test: ["CMD", "mariadb-admin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 15s
      retries: 5

  frontend:
    build: .
      ports:
        - "8080:80"
      networks:
        - dockercompose-frontend
      environment:
        PMA_HOST=mydb
        PMA_PORT=3306

volumes:
  server_db:
    driver: local
networks:
  dockercompose-frontend:

And two Dockerfiles for each of the services, where I just install necessary images.

FROM mariabd:latest
RUN apt-get update && apt-get install -y iputils-ping

and

FROM phpmyadmin:5.2.0-apache
RUN apt-get update && apt-get install -y iputils-ping

When I’m using docker compose up (without -d mode):

frontend-1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain nam e, using 172.25.0.2. Set the ServerName directive globally to suppress this message

When I’m using docker compose up -d and checks Health State for mydb it throws:

{
  "Status":"starting", 
  "FailingStreak": 2, 
  "Log": [{"Start": "2024-02-19T10:05:48.178379963-05:00",
    "End": 2024-02-19T10:05:48.240662846-05:00", 
    "ExitCode": 1, 
    "Output":"u0007mariadb-admin: connect to server a t 'localhost failednerror: 'Can't connect to local server through socket '/run/mysqld/mysqld.sock (2) 
    'Check that mariadbd is running and that the socket: '/run/mysqld/mysqld.sock' exists!"
}

Also,

docker exec dockercompose-mydb-1 service mariadb status

throws "Mariadb service is stopped"

After

docker exec dockercompose-mydb-1 service mariadb start

it throws exit code 1 (failure).

2

Answers


  1. first, update the syntax in health check part as there’s error with quotes to

    healthcheck:
      test: ["CMD", "mariadb-admin", "ping", "-h", "localhost"]
      interval: 10s
      timeout: 15s
      retries: 5
    

    second specify the port in mariadb service

    version: '3' 
    services:
      mydb:
        build: ./db/
        networks:
          - dockercompose-frontend 
        volumes:
          - server_db:/var/lib/mysql
        environment:
          - MYSQL_ROOT_PASSWORD=debian
        healthcheck:
          test: ["CMD", "mariadb-admin", "ping", "-h", "localhost"]
          interval: 10s 
          timeout: 15s 
          retries: 5
        ports:
          - "3306:3306"  # Map container's port 3306 to host's port 3306
    
      frontend:
        build: .
        ports:
          - "8080:80"
        networks:
          - dockercompose-frontend
        environment:
          PMA_HOST: mydb
          PMA_PORT: 3306
    
    volumes:
      server_db:
        driver: local
    #rest of your docker comopse file
    

    also make sure that the network driver is not equal to none, it’s type is equal to bridge for example.

    you can add "depends_on" in frontend service so the container is not created unless mariaDB is up successfully.

      frontend:
        build: .
        ports:
          - "8080:80"
        networks:
          - dockercompose-frontend
        environment:
          PMA_HOST: mydb
          PMA_PORT: 3306
        depends_on:
          - mydb  # to prevent docker compose from starting the container until the container of mariaDB is running.
    
    Login or Signup to reply.
  2. There’s a healthcheck.sh script for the very purpose.

    test:[ "CMD", "healthcheck.sh", "--connect", "--innodb_initialized" ]
    

    So there’s no need to additional grants, users to make this happen.

    The other problem with mariadb-admin ping is without --protocol tcp it can return healthly during the initialization phases of the startup.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search