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
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:Creates an array containing the elements
db
andmem
. While:Creates two empty map entries with the keys
db
andmem
.You can use the
condition: service_started
for themem
service as follows:You can also check the documentation.