kelsett
(Kelsett)
February 16, 2023, 11:10pm
1
Hi,
I’m trying to pass some parameters to my healthcheck test :
version: '3.8'
services:
mongodb:
image: mongo
container_name: mongodb
environment:
- MONGO_INITDB_ROOT_USERNAME=${MONGODB_USER}
- MONGO_INITDB_ROOT_PASSWORD=${MONGODB_PASS}
volumes:
- ./db:/data/db
networks:
- proxy
restart: unless-stopped
healthcheck:
test: test $$(echo "rs.initiate({_id: 'rs0', members: [{_id: 1, 'host': 'mongodb:27017'}]}).ok || rs.status().ok" | mongosh -u $${MONGO_INITDB_ROOT_USERNAME} -p $${MONGO_INITDB_ROOT_PASSWORD} --quiet) -eq 1
interval: 10s
start_period: 30s
I’m getting this error :
yaml: mapping values are not allowed in this context
If I remove the colons ( : ) it works. How can I escape these colons in my test value ?
meyay
(Metin Y.)
February 16, 2023, 11:21pm
2
The first link in the google search result list for “escape colon in yaml” shows plenty of variations: escaping - How to escape indicator characters (colon and hyphen) in YAML - Stack Overflow .
Personalty, I would just wrap it in single quotes, as plain style (as in unquoted) does not allow escaping.
rimelek
(Ákos Takács)
February 17, 2023, 12:13am
3
@meyay gave you the actual answer to your question. I just want to add a relevant link in case someone finds the topic searching for “colon” and “docker compose value”. In some values colons have meaning. Like in volume mounts and you can’t really “escape” it, only change the source path.
First of all the devices section is a list of device mappings, not a list of devices. The way you defined it would mean that
/dev/disk/by-id/usb-ST2000DL_003-9VT166_152D00539000-0
is the source device on the host and you want to attach it to the container two times. Once as “0” and once as “1”.
This is the same as you mount files or fodlers from the host.
Second, a mapping contains colons as separators, so you can’t have it in the values. As you probably now many files under /dev/disk are a…
1 Like
kelsett
(Kelsett)
February 17, 2023, 12:46am
4
My bad, I kept including “docker compose” in my keywords when searching, I’m forgetting YAML is not exclusive to Docker… I was able to solve the issue. The working code is this :
healthcheck:
test: test $$(echo 'rs.initiate({_id':' "rs0", members':' [{_id':' 1, "host"':' "mongodb':'27017"}]}) || rs.status().ok' | mongosh -u $${MONGO_INITDB_ROOT_USERNAME} -p $${MONGO_INITDB_ROOT_PASSWORD} --quiet) -eq 1
Thanks a lot !