skip to Main Content

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?

2

Answers


  1. Yaml can be mapped directly into a data structure. When you start a line with a - you are defining an entry in an array. When it’s a string followed by a colon, it’s the key of a key/value mapping. So:

    depends_on:
          - db
          - mem 
    

    Creates an array containing the elements db and mem. While:

    depends_on:
          db: {}
          mem: {}
    

    Creates two empty map entries with the keys db and mem.

    Login or Signup to reply.
  2. 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.

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