'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?
Solution 1:[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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BMitch |
