Waiting for Docker Services to be Ready

Edward Cernera
4 min readMar 8, 2020
Shipping containers stacked
Photo by frank mckenna on Unsplash

Purpose

The purpose of this article is to illustrate how we can implement a simple “waiting” method in docker-compose. With certain docker orchestrations, a container will start, but its internal services still need some time to start up before the service will be available via the container’s exposed port(s).

Example

In the following example, we will be deploying the ELK Stack (ElasticSearch, Kibana, Logstash). On docker deployment, we need to execute several post-deployment operations, like uploading Kibana dashboards. This seems like a straightforward problem to solve, but we have to deal with the added complexity of the Kibana server startup time. Once the Kibana docker container is created and we’ve exposed port 5601 (the default Kibana port), the Kibana server will take approximately 30 seconds to be ready; until then, any attempt to access the service at port 5601 will result in a Connection refused .

File Structure

Let’s define the necessary files.

*I have purposely left out the Kibana, Logstash and ElasticSearch Dockerfiles from this article to reduce complexity.

docker-compose.yml

version: "3.2"
services:

elasticsearch:
build:
context: elasticsearch/…

--

--

Edward Cernera
Edward Cernera

Written by Edward Cernera

Software Engineer | I write software tutorials because I can’t find them anywhere. Creator of DealPunkk - dealpunkk.com | About Me - cernera.me/edward

No responses yet