Here is my docker-compose.yml
:
version: "3"
services:
mongodb:
image: mongo:4.1
volumes:
- ./mongodb_data:/data/db
container_name: mongodb
ports:
- 27017:27017
mysql:
image: mysql:5.7
#volumes:
#- ./mysql_data:/var/lib/mysql
container_name: mysqldb
ports:
- 3306:3306
- 33060:33060
expose:
- 3306
- 33060
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=somedb
- MYSQL_USER=rstudio
- MYSQL_PASSWORD=password
healthcheck:
test: ["CMD", "mysql", "--user=$MYSQL_USER", "--password=$MYSQL_PASSWORD", "-e", "'SHOW DATABASES;'"]
timeout: 5s
retries: 5
How can I use environment variables MYSQL_USER
and MYSQL_PASSWORD
? In current setup I got following warning when running docker compose:
WARNING: The MYSQL_USER variable is not set. Defaulting to a blank string.
WARNING: The MYSQL_PASSWORD variable is not set. Defaulting to a blank string.
You need to use double-dollar sign ( $$
) in order to tell docker-compose
not to parse these environment variables. Also change the way of defining a test as I tried to make it work but I couldn't without changing it. The following should work as expected:
test: mysql --user=$$MYSQL_USER --password=$$MYSQL_PASSWORD -e 'SHOW DATABASES;'
docker ps
result: Up 35 seconds (healthy)
For me, Mostafa's (user: 2336650) answer does not work, but it set me in the right direction. The answer is correct in the aspect regarding $
needs escaping in docker-compose.yml
.
The missing piece is that the test command has more than one mode of execution; what we are interested in, in particular, is CMD-SHELL
.
CMD-SHELL
is different from CMD
because it wrapped command execution inside /bin/sh
, so shell syntax works. Since I used both environment variables and &&
for multiple condition checking, I don't really sure what makes the health checking breaks in my case. (could be both)
For those who wandering for their answer, probably a different one from mine, the reference doc is worth a look. https://docs.docker.com/compose/compose-file/compose-file-v2/#healthcheck
Your YAML looks OK, how are you invoking your env variables? Make sure you're referencing the correct object. Generally from your application you'd invoke your variables using something like
process.env.MYSQL_USER
You also don't need the dashes, my preference (for readibility) would be this syntax:
mysql:
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: somedb
MYSQL_USER: rstudio
MYSQL_PASSWORD: password
If you're trying to reference your env variable in the yaml config directly (ie in your tests command) you should use ARGS instead as your env variables probably won't be compiled yet.
EDIT - if you want to reference an object within the yaml, you could use self
eg:
test: ["CMD", "mysql", "--user=${self:services.mysql.environment.MYSQL_USER}", "--password=${self:services.mysql.environment.MYSQL_PASSWORD}", "-e", "'SHOW DATABASES;'"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.