You may run into a situation where your app attempts to access the database before it is ready to accept connections, resulting in a failure. It this case it is necessary to wait for the database to come online fully before continuing.
We suggest dockerize: https://github.com/jwilder/dockerize
In this example PostgreSQL is our database. Our first run step downloads the newest Dockerize binary and the next run step `Wait for db` waits on the port for our database until it receives a response, with a 1 minute timeout.
version: 2.0
jobs:
build:
docker:
- image: your/image_for_primary_container
- image: postgres:9.6.2-alpine
environment:
POSTGRES_USER: your_postgres_user
POSTGRES_DB: your_postgres_test
workDir: /your/workdir
steps:
- checkout
- run:
name: install dockerize
command: wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && sudo tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
environment:
DOCKERIZE_VERSION: v0.3.0
- run:
name: Wait for db
command: dockerize -wait tcp://localhost:5432 -timeout 1m
You can apply the same principle for MySQL:
dockerize -wait tcp://localhost:3306 -timeout 1m
Redis:
dockerize -wait tcp://localhost:6379 -timeout 1m
and other services such as web servers
dockerize -wait http://localhost:80 -timeout 1m
Comments
Article is closed for comments.