Fig up: docker launch synchronization

For one of my home projects, I decided to use docker and figure containers for orchestration (using these tools for the first time).

Here is my fig.yaml:

rabbitmq: image: dockerfile/rabbitmq:latest mongodb: image: mongo app: build: . command: python /code/app/main.py links: - rabbitmq - mongodb volumes: - .:/code 

Rabbitmq's startup time is much slower than the load time of my application. Despite the fact that the rabbitmq container starts to load first (since it is in the links to the application), when my application tries to connect to the rabbitmq server, it is not yet available (it definitely loads the synchronization problem, because if I just put the sleep mode into for 5 seconds before connecting to rabbitmq - everything works fine). Is there a standard way to solve boot time synchronization problems?

Thanks.

+5
source share
3 answers

I do not think that there is a standard way to solve this problem, but this is a known problem, and some people have acceptable workarounds.

There is a suggestion in the Docker issue tracker that you do not consider the container as running until it listens on open ports. However, this is most likely not to be accepted due to other problems that it would create elsewhere. There is a drawing on the same topic.

A simple solution is to make a dream, as @jcortejoso says. Example from http://blog.chmouel.com/2014/11/04/avoiding-race-conditions-between-containers-with-docker-and-fig/ :

 function check_up() { service=$1 host=$2 port=$3 max=13 # 1 minute counter=1 while true;do python -c "import socket;s = socket.socket(socket.AF_INET, socket.SOCK_STREAM);s.connect(('$host', $port))" \ >/dev/null 2>/dev/null && break || \ echo "Waiting that $service on ${host}:${port} is started (sleeping for 5)" if [[ ${counter} == ${max} ]];then echo "Could not connect to ${service} after some time" echo "Investigate locally the logs with fig logs" exit 1 fi sleep 5 (( counter++ )) done } 

And then use check_up "DB Server" ${RABBITMQ_PORT_5672_TCP_ADDR} 5672 before starting the application server, as described in the link above.

Another option is to use docker-wait . In fig.yml .

 rabbitmq: image: dockerfile/rabbitmq:latest mongodb: image: mongo rabbitmqready: image: aanand/wait links: - rabbitmq app: build: . command: python /code/app/main.py links: - rabbitmqready - mongodb volumes: - .:/code 
+6
source

Similar problems that I encountered, I decided to use a custom script as CMD in my Dockerfiles . Then you can run any validation command that you want ( sleep for a while, or wait for a service to listen, for example). I think there is no standard way to do this, anyway, I think the best way is to run the application so that it can request an external service and connect to them, but this is not possible in most cases.

+1
source

Source: https://habr.com/ru/post/1209966/


All Articles