Dear readers
Another day, another research is done. This time, it is related to docker, specificly docker compose
For those who don't know, docker compose is a way to start one or more container with certain attributes or parameters, so that you don't need to execute:
docker run .....
everytime. Instead, you simply run:
docker-compose up
or similar command after you complete writing docker-compose.yml
The problem arise when you need to run something like this:
version:
"2"
services:
database
:
image: mysql
app:
image: custom_app
Let's put aside what the app is doing and how app connect to mysql. The problem is how make sure mysql run first and fully ready before app tries to connect?
The first solution that comes up to mind is to use depends_on like below:
version:
"2"
services:
database
:
image: mysql
app:
image: custom_app
depends_on:
- database
Sounds solid? Yes, on the surface. Try to run that and you will realize that what depends_on really means is that "wait until container A run then run B". The meaning of "run" doesn't necessarily means "ready", especially if you are dealing with database engine that does whole lot stuffs beside just "start".
I face similar situation and I come up with really simple solution (inspired by discussion in Stackoverflow here). So, here is my solution
version:
"2"
services:
database
:
image: mysql
app:
image: custom_app
depends_on:
- database
entrypoint:
-
"/bin/sh"
- -c
- |
sleep
30
&& \
/usr/local/bin/the_app
No comments:
Post a Comment