Wait-for-it helps overcome limitations with depends_on in docker-compose by staggering various docker container runs.
Docker compose is a very useful tool that allows you to deploy multiple docker images together. Below is an example
- Database (eg Postgres, Mongo etc)
- Backend (eg API service)
- Client (eg Nginx )
In most cases you will require one service to be up before you can run another. Docker-compose provides you a helpful tag called
depends_on , which ensures that one container runs before the other. Lets see an example of this:
version: '3' services: postgres: build: context: . dockerfile: postgres/Dockerfile image: postgres:tag env_file: .env ports: - '5432:5432' backend: build: context: . dockerfile: backend/Dockerfile image: backend environment: database:postgresql://user:[email protected]:5432' depends_on: - postgres ports: - '8000:8000' frontend: build: context: . dockerfile: frontend/Dockerfile image: frontend ports:'5000:5000'
However, in our example, backend might take 1 second to run, and postgres that it depends on being available might take 10 seconds. Docker-compose ’s
depends_on will run the containers in sequence, but docker-compose has no way of knowing when the postgres service is actually ready to receive requests.
In these types of situations it’s helpful to use a wait-for-it. https://github.com/vishnubob/wait-for-it
Its a helpful tool that lets you wait for another service to be ready. Example usage is as follows:
# base image FROM node:12.1.0 # # All your other docker statements # # Add wait-for-it COPY wait-for-it.sh wait-for-it.sh RUN chmod +x wait-for-it.sh ENTRYPOINT [ "/bin/bash", "-c" ] CMD ["./wait-for-it.sh" , "[ENTER YOUR ENDPOINT HERE]" , "--strict" , "--timeout=300" , "--" , "YOUR REAL START COMMAND"]
I hope this was helpful. Please see references for detailed usage.