Try to search your question here, if you can't find : Ask Any Question Now ?

Docker Compose wait for container X before starting Y

HomeCategory: stackoverflowDocker Compose wait for container X before starting Y
Avatarjulie asked 4 months ago

I am using rabbitmq and a simple python sample from here
together with docker-compose. My problem is that I need to wait for rabbitmq to fully started. From what I searched so far, I don’t know how to wait with container x ( in my case worker ) until y (rabbitmq) is started.

I found this blogpost where he checks if the other host is online.
I also found this docker command:


Usage: docker wait CONTAINER [CONTAINER…]

Block until a container stops, then print its exit code.

Waiting for a container to stop is maybe not what I am looking for but if
it is, is it possible to use that command inside the docker-compose.yml ?
My solution so far is to wait some seconds and check the port, but is this the way to achieve this?. If I don’t wait I get an error.


    build: myapp/.
    - myapp/.:/usr/src/app:ro

    - rabbitmq
    image: rabbitmq:3-management

python hello sample (

import pika
import time

import socket

pingcounter = 0
isreachable = False
while isreachable is False and pingcounter < 5:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect(('rabbitmq', 5672))
        isreachable = True
    except socket.error as e:
        pingcounter += 1

if isreachable:
    connection = pika.BlockingConnection(pika.ConnectionParameters(
    channel =


                          body='Hello World!')
    print (" [x] Sent 'Hello World!'")

Dockerfile for worker:

FROM python:2-onbuild
RUN ["pip", "install", "pika"]

CMD ["python",""]

Update Nov 2015:

A shell script or waiting inside your program is maybe a possible solution. But after seeing this Issue I am looking for a command or feature of docker/docker-compose itself.

They mention a solution for implementing a health check, which may be the best option. A open tcp connection does not mean your service is ready or may remain ready. In addition to that I need to change my entrypoint in my dockerfile.

So I am hoping for an answer with docker-compose on board commands, which will hopefully the case if they finish this issue.

Update March 2016

There is a proposal for providing a built-in way to determine if a container is “alive”. So docker-compose can maybe make use of it in near future.

Update June 2016

It seems that the healthcheck will be integrated into docker in Version 1.12.0

Update January 2017

I found a docker-compose solution see:
Docker Compose wait for container X before starting Y

1 Answers
Best Answer
Avatarnaveen answered 4 months ago
Your Answer

0 + 3 =

Popular Tags

WP Facebook Auto Publish Powered By :