Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yaml "must be a mapping" error in docker-compose file

In an attempt to make my app container wait for my DB container to be up, I am trying to change my docker-compose file from this:

version: '2.1'

services:
  db:
    image: mysql:5.7
    restart: always
    environment:
       MYSQL_ROOT_PASSWORD: <credentials here>
       MYSQL_DATABASE: <credentials here>
       MYSQL_USER: <credentials here>
       MYSQL_PASSWORD: <credentials here>

  mem:
    image: memcached:alpine
  
  web:
    build: <path/to/project>
    depends_on:
      - db
      - mem 
    restart: always
    environment:
      ENV: devel
      MEMCACHE_SERVER: 'mem:11211'
      DB_ENV_MYSQL_USER: <credentials here>
      DB_ENV_MYSQL_DATABASE: <credentials here>
      DB_ENV_MYSQL_PORT: 3306
      DB_ENV_MYSQL_PASSWORD: <credentials here>
      DB_ENV_MYSQL_ADDR: db-1
    ports:
      - "4000:4000"

to something like this:

version: '2.1'

services:
  db:
    image: mysql:5.7
    restart: always
    environment:
       MYSQL_ROOT_PASSWORD: <credentials here>
       MYSQL_DATABASE: <credentials here>
       MYSQL_USER: <credentials here>
       MYSQL_PASSWORD: <credentials here>
    healthcheck:
      test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
      timeout: 20s
      retries: 10

  mem:
    image: memcached:alpine
  
  web:
    build: <path/to/project>
    depends_on:
      db:
        condition: service_healthy
      mem
    restart: always
    environment:
      ENV: devel
      MEMCACHE_SERVER: 'mem:11211'
      DB_ENV_MYSQL_USER: <credentials here>
      DB_ENV_MYSQL_DATABASE: <credentials here>
      DB_ENV_MYSQL_PORT: 3306
      DB_ENV_MYSQL_PASSWORD: <credentials here>
      DB_ENV_MYSQL_ADDR: db-1
    ports:
      - "4000:4000"

I made various attempts to format the depends_on section correctly, but no matter what, I can't seem to make it work. Online yaml validators do't help either in figuring out how to fix the error.

I feel like I've tried all possible combinations of dashes and colons but none of them seem to work:

    depends_on:
      - db:
        - condition: service_healthy
      - mem: 
    depends_on:
      db:
        condition: service_healthy
      - mem
    depends_on:
      db:
        - condition: service_healthy
      - mem

How can I have two entries in the depends_on section, with one of them having an additional sub-entry?

like image 277
mrodo Avatar asked Sep 01 '25 01:09

mrodo


1 Answers

You can use the condition: service_started for the mem service as follows:

    depends_on:
      db:
        condition: service_healthy
      mem:
        condition: service_started

You can also check the documentation.

like image 66
Dmitry Nichiporenko Avatar answered Sep 02 '25 14:09

Dmitry Nichiporenko