简体   繁体   中英

Docker-compose and race condition on container depending on other container

I have docker-compose which will start multiple containers of Java SpringBoot microservices and one of them, which depends on two others, will fail to start with something like race condition:

Exception thrown from ApplicationListener handling ContextClosedEvent

org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'rabbitConnectionFactory': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:208)

When I will stop it and run it manually it will pick up. When docker-compose will have set restart: on-failure for that service it will pick up on 3rd restart.

Any tips how to handle such of problem?

Usually, depends_on is enough. Nevertheless, sometimes it may not work properly. Once I heard about this issue and the recommendation was to use the healthcheck docker option, available at docker v2.3. See guide here .

I am adding an example using a healthcheck to guarantee the database is up. So, only after the healthcheck is ok, they will startup the other apps.

[...]
healthcheck:
  test: ["CMD-SHELL", 'mysql -uroot --database=<db-name> --password=<password> --execute="SELECT 
  count(table_name) > 0 FROM information_schema.tables;" --skip-column-names -B']
  interval: 10s
  timeout: 10s
  retries: 4

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM