1

I have 2 services: service-background & service-webapi. Those are registered in docker-compose. Service-background needs to be started up first. Only then service-webapi may be booted. This is a fixed order. I have following configuration in docker-compose.yml:

networks:
   my-fantastic-network

services:
   background-service:
      build: backgroundproject
      networks:
         my-fantastic-network:
            aliases:
               -background-service

webapi-service:
   build: webapi
   depends_on: 
     -background-service
   networks:
      my-fantastic-network:
         aliases:
            -webapi-service

I thought this would be enough to let the webapi-service wait for the background-service but this one starts up too soon and messes up totally.

My background-service has a log file in following file structure: server/database/logs/startup.log If the log file contains the text "Database initialized successfully", the background-service is ready and the webapi-service can startup safely. I could use the Linux grep function to check if my logfile contains this specific text. But I don't know how to do this with docker-compose.

I did some research and there is a healthcheck attribute available to use in docker-compose, but I'm not too familiar with it.

Can someone help me?

Lumpi
  • 69
  • 1
  • 10

1 Answers1

0

just have a look into the very good docker documentation: https://docs.docker.com/compose/compose-file/#healthcheck

But maybe it is not the right solution for you. I would recommend to look into this:

https://docs.docker.com/compose/startup-order/

It is a easy practical solution with an additional script in the image which will at first wait for the connection to the db and then it will start the actual start script.

Hope this helps.

Hannes
  • 491
  • 5
  • 21
  • I don't know how I can access a log file from container A while container B is starting up. This is also not described in that documentation. – Lumpi Jan 21 '20 at 16:15
  • Yes in this case you will not check the logfile. You will check if the actual service is up and will accept tcp connection. This step is described in my second hyperlink. – Hannes Jan 21 '20 at 16:17