Docker Compose wait for container X before starting Y









up vote
178
down vote

favorite
65












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:




wait



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.



docker-compose.yml



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

links:
- rabbitmq
rabbitmq:
image: rabbitmq:3-management


python hello sample (rabbit.py):



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)
try:
s.connect(('rabbitmq', 5672))
isreachable = True
except socket.error as e:
time.sleep(2)
pingcounter += 1
s.close()

if isreachable:
connection = pika.BlockingConnection(pika.ConnectionParameters(
host="rabbitmq"))
channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print (" [x] Sent 'Hello World!'")
connection.close()


Dockerfile for worker:



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

CMD ["python","rabbit.py"]


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










share|improve this question























  • Using healthchecks in has been deprecated in docker-compose 2.3 to encourage making distributed systems fault tolerant. See: docs.docker.com/compose/startup-order
    – Kmaid
    Jul 11 '17 at 15:29










  • as pointed out in the answer, but thanks ...
    – svenhornberg
    Jul 12 '17 at 6:16














up vote
178
down vote

favorite
65












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:




wait



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.



docker-compose.yml



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

links:
- rabbitmq
rabbitmq:
image: rabbitmq:3-management


python hello sample (rabbit.py):



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)
try:
s.connect(('rabbitmq', 5672))
isreachable = True
except socket.error as e:
time.sleep(2)
pingcounter += 1
s.close()

if isreachable:
connection = pika.BlockingConnection(pika.ConnectionParameters(
host="rabbitmq"))
channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print (" [x] Sent 'Hello World!'")
connection.close()


Dockerfile for worker:



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

CMD ["python","rabbit.py"]


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










share|improve this question























  • Using healthchecks in has been deprecated in docker-compose 2.3 to encourage making distributed systems fault tolerant. See: docs.docker.com/compose/startup-order
    – Kmaid
    Jul 11 '17 at 15:29










  • as pointed out in the answer, but thanks ...
    – svenhornberg
    Jul 12 '17 at 6:16












up vote
178
down vote

favorite
65









up vote
178
down vote

favorite
65






65





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:




wait



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.



docker-compose.yml



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

links:
- rabbitmq
rabbitmq:
image: rabbitmq:3-management


python hello sample (rabbit.py):



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)
try:
s.connect(('rabbitmq', 5672))
isreachable = True
except socket.error as e:
time.sleep(2)
pingcounter += 1
s.close()

if isreachable:
connection = pika.BlockingConnection(pika.ConnectionParameters(
host="rabbitmq"))
channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print (" [x] Sent 'Hello World!'")
connection.close()


Dockerfile for worker:



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

CMD ["python","rabbit.py"]


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










share|improve this question















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:




wait



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.



docker-compose.yml



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

links:
- rabbitmq
rabbitmq:
image: rabbitmq:3-management


python hello sample (rabbit.py):



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)
try:
s.connect(('rabbitmq', 5672))
isreachable = True
except socket.error as e:
time.sleep(2)
pingcounter += 1
s.close()

if isreachable:
connection = pika.BlockingConnection(pika.ConnectionParameters(
host="rabbitmq"))
channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print (" [x] Sent 'Hello World!'")
connection.close()


Dockerfile for worker:



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

CMD ["python","rabbit.py"]


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







docker-compose






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 23 at 14:46









Tim S. Van Haren

8,09622434




8,09622434










asked Jul 31 '15 at 12:25









svenhornberg

3,82162643




3,82162643











  • Using healthchecks in has been deprecated in docker-compose 2.3 to encourage making distributed systems fault tolerant. See: docs.docker.com/compose/startup-order
    – Kmaid
    Jul 11 '17 at 15:29










  • as pointed out in the answer, but thanks ...
    – svenhornberg
    Jul 12 '17 at 6:16
















  • Using healthchecks in has been deprecated in docker-compose 2.3 to encourage making distributed systems fault tolerant. See: docs.docker.com/compose/startup-order
    – Kmaid
    Jul 11 '17 at 15:29










  • as pointed out in the answer, but thanks ...
    – svenhornberg
    Jul 12 '17 at 6:16















Using healthchecks in has been deprecated in docker-compose 2.3 to encourage making distributed systems fault tolerant. See: docs.docker.com/compose/startup-order
– Kmaid
Jul 11 '17 at 15:29




Using healthchecks in has been deprecated in docker-compose 2.3 to encourage making distributed systems fault tolerant. See: docs.docker.com/compose/startup-order
– Kmaid
Jul 11 '17 at 15:29












as pointed out in the answer, but thanks ...
– svenhornberg
Jul 12 '17 at 6:16




as pointed out in the answer, but thanks ...
– svenhornberg
Jul 12 '17 at 6:16












12 Answers
12






active

oldest

votes

















up vote
152
down vote



accepted










Finally found a solution with a docker-compose method. Since docker-compose file format 2.1 you can define healthchecks.



I did it in a example project
you need to install at least docker 1.12.0+.
I also needed to extend the rabbitmq-management Dockerfile, because curl isn't installed on the official image.



Now I test if the management page of the rabbitmq-container is available. If curl finishes with exitcode 0 the container app (python pika) will be started and publish a message to hello queue. Its now working (output).



docker-compose (version 2.1):



version: '2.1'

services:
app:
build: app/.
depends_on:
rabbit:
condition: service_healthy
links:
- rabbit

rabbit:
build: rabbitmq/.
ports:
- "15672:15672"
- "5672:5672"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:15672"]
interval: 30s
timeout: 10s
retries: 5


output:



rabbit_1 | =INFO REPORT==== 25-Jan-2017::14:44:21 ===
rabbit_1 | closing AMQP connection <0.718.0> (172.18.0.3:36590 -> 172.18.0.2:5672)
app_1 | [x] Sent 'Hello World!'
healthcheckcompose_app_1 exited with code 0


Dockerfile (rabbitmq + curl):



FROM rabbitmq:3-management
RUN apt-get update
RUN apt-get install -y curl
EXPOSE 4369 5671 5672 25672 15671 15672


Version 3 no longer supports the condition form of depends_on.
So i moved from depends_on to restart on-failure. Now my app container will restart 2-3 times until it is working, but it is still a docker-compose feature without overwriting the entrypoint.



docker-compose (version 3):



version: "3"

services:

rabbitmq: # login guest:guest
image: rabbitmq:management
ports:
- "4369:4369"
- "5671:5671"
- "5672:5672"
- "25672:25672"
- "15671:15671"
- "15672:15672"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:15672"]
interval: 30s
timeout: 10s
retries: 5

app:
build: ./app/
environment:
- HOSTNAMERABBIT=rabbitmq
restart: on-failure
depends_on:
- rabbitmq
links:
- rabbitmq





share|improve this answer


















  • 6




    @svenhornberg ping uses ICMP so doesn't support TCP ports. Maybe nc to test a TCP port. Probably better to use psql -h localhost -p 5432 and query something.
    – Matt
    Feb 13 '17 at 5:14






  • 23




    "depends on" has been removed in version 3 docs.docker.com/compose/compose-file/#dependson
    – nha
    Apr 3 '17 at 11:08






  • 2




    I think the main reason is, that an application itself should always be able to wait or recovery if others services are not available (look for resilience in micro service context)
    – svenhornberg
    Apr 20 '17 at 8:33







  • 27




    @nha It looks like the condition form of depends_on is removed, but depends_on itself is still around in v3
    – akivajgordon
    Jul 26 '17 at 20:25






  • 6




    How can healthchecks still be used to control startup order if depends_on with condition has been removed?
    – Franz
    Aug 31 '17 at 15:49

















up vote
61
down vote













Natively that is not possible, yet. See also this feature request.



So far you need to do that in your containers CMD to wait until all required services are there.



In the Dockerfiles CMD you could refer to your own start script that wraps starting up your container service. Before you start it, you wait for a depending one like:



Dockerfile



FROM python:2-onbuild
RUN ["pip", "install", "pika"]
ADD start.sh /start.sh
CMD ["/start.sh"]


start.sh



#!/bin/bash
while ! nc -z rabbitmq 5672; do sleep 3; done
python rabbit.py


Probably you need to install netcat in your Dockerfile as well. I do not know what is pre-installed on the python image.



There are a few tools out there that provide easy to use waiting logic, for simple tcp port checks:




  • wait-for-it

  • dockerize

For more complex waits:




  • goss - Explanation blog





share|improve this answer






















  • Could you explain what you mean by CMD ? Does this mean my programm has to do it, like i did it with a port check ? Or do you mean a specific CMD from e.g. linux for this ?
    – svenhornberg
    Jul 31 '15 at 22:56











  • thank you for explaining, I upvote your answer.But I think the upcoming feature request, would be the right answer to my question so I leave it unanswered so far.
    – svenhornberg
    Aug 1 '15 at 9:27


















up vote
32
down vote













Using restart: unless-stopped or restart: always may solve this problem.



If worker container stops when rabbitMQ is not ready, it will be restarted until it is.






share|improve this answer
















  • 3




    I like this solution for this case, but it doesn't work for containers that don't exit when one of the subprocesses that it runs fails. For example, a Tomcat container would continue to run even if a Java servlet that it ran were to fail to connect to a database server. Granted, Docker containers render servlet containers like Tomcat mostly unnecessary.
    – Derek Mahar
    May 6 '16 at 1:50











  • @DerekMahar, if you have a Java-based web application that only serves REST calls, what do you use instead of Jetty/Tomcat?
    – JoeG
    Dec 21 '16 at 19:31






  • 2




    @JoeG, I meant Tomcat the servlet container that can host many applications, not embedded Tomcat. Docker renders the former mostly unnecessary, while making the latter more popular for microservices, for example.
    – Derek Mahar
    Dec 21 '16 at 20:11

















up vote
26
down vote













Quite recently they've added the depends_on feature.



Edit:



As of compose version 2.1+ you can use depends_on in conjunction with healthcheck to achieve this:



From the docs:



version: '2.1'
services:
web:
build: .
depends_on:
db:
condition: service_healthy
redis:
condition: service_started
redis:
image: redis
db:
image: redis
healthcheck:
test: "exit 0"


Before version 2.1



You can still use depends_on, but it only effects the order in which services are started - not if they are ready before the dependant service is started.



It seems to require at least version 1.6.0.



Usage would look something like this:



version: '2'
services:
web:
build: .
depends_on:
- db
- redis
redis:
image: redis
db:
image: postgres


From the docs:




Express dependency between services, which has two effects:



  • docker-compose up will start services in dependency order. In the following example, db and redis will be started before web.

  • docker-compose up SERVICE will automatically include SERVICE’s dependencies. In the following example, docker-compose up web will also create and start db and redis.



Note: As I understand it, although this does set the order in which containers are loaded. It does not guarantee that the service inside the container has actually loaded.



For example, you postgres container might be up. But the postgres service itself might still be initializing within the container.






share|improve this answer


















  • 9




    dnephin wrote: depends_on is only ordering. To actually delay the starting of another container there would need to be some way to detect when a process has finished initializing itself.
    – svenhornberg
    Mar 1 '16 at 7:15






  • 11




    "Version 3 no longer supports the condition form of depends_on." docs.docker.com/compose/compose-file/#dependson
    – akauppi
    Apr 12 '17 at 14:30










  • depends_on doesn't wait until the container is in ready state(whatever that can mean in your case). It only waits until the container is in 'running' state.
    – htyagi
    Aug 31 '17 at 13:20

















up vote
14
down vote













you can also just add it to the command option eg.



command: bash -c "sleep 5; start.sh"


https://github.com/docker/compose/issues/374#issuecomment-156546513



to wait on a port you can also use something like this



command: bash -c "while ! curl -s rabbitmq:5672 > /dev/null; do echo waiting for xxx; sleep 3; done; start.sh"


to increment the waiting time you can hack a bit more:



command: bash -c "for i in 1..100 ; do if ! curl -s rabbitmq:5672 > /dev/null ; then echo waiting on rabbitmq for $i seconds; sleep $i; fi; done; start.sh"





share|improve this answer





























    up vote
    5
    down vote













    You can also solve this by setting an endpoint which waits for the service to be up by using netcat (using the docker-wait script). I like this approach as you still have a clean command section in your docker-compose.yml and you don't need to add docker specific code to your application:



    version: '2'
    services:
    db:
    image: postgres
    django:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    entrypoint: ./docker-entrypoint.sh db 5432
    volumes:
    - .:/code
    ports:
    - "8000:8000"
    depends_on:
    - db


    Then your docker-entrypoint.sh:



    #!/bin/sh

    postgres_host=$1
    postgres_port=$2
    shift 2
    cmd="$@"

    # wait for the postgres docker to be running
    while ! nc $postgres_host $postgres_port; do
    >&2 echo "Postgres is unavailable - sleeping"
    sleep 1
    done

    >&2 echo "Postgres is up - executing command"

    # run the command
    exec $cmd


    This is nowadays documented in the official docker documentation.



    PS: You should install netcat in your docker instance if this is not available. To do so add this to your Docker file :



    RUN apt-get update && apt-get install netcat-openbsd -y 





    share|improve this answer





























      up vote
      5
      down vote













      For container start ordering use



      depends_on:


      For waiting previous container start use script



      entrypoint: ./wait-for-it.sh db:5432


      This article will help you
      https://docs.docker.com/compose/startup-order/






      share|improve this answer




















      • stackoverflow.com/a/35476843/1584115
        – svenhornberg
        Nov 24 '16 at 9:43






      • 4




        @svenhornberg in the comment, you link, there is no explanation about wait-for-it.sh feature.
        – quit
        Dec 2 '16 at 19:13

















      up vote
      5
      down vote













      restart: on-failure
      did the trick for me..see below



      ---
      version: '2.1'
      services:
      consumer:
      image: golang:alpine
      volumes:
      - ./:/go/src/srv-consumer
      working_dir: /go/src/srv-consumer
      environment:
      AMQP_DSN: "amqp://guest:guest@rabbitmq:5672"
      command: go run cmd/main.go
      links:
      - rabbitmq
      restart: on-failure

      rabbitmq:
      image: rabbitmq:3.7-management-alpine
      ports:
      - "15672:15672"
      - "5672:5672"





      share|improve this answer



























        up vote
        4
        down vote













        There is a ready to use utility called "docker-wait" that can be used for waiting.






        share|improve this answer
















        • 1




          Thank you, but it is only a shell script so it is like h3nrik answer or waiting inside python. It is not a feature of docker-compose itself. May you have a look in github.com/docker/compose/issues/374 they plan to implement a healthcheck which would be the best way. 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.
          – svenhornberg
          Nov 11 '15 at 14:48


















        up vote
        1
        down vote













        basing on this blog post https://8thlight.com/blog/dariusz-pasciak/2016/10/17/docker-compose-wait-for-dependencies.html



        I configured my docker-compose.yml as shown below:



        version: "3.1"

        services:
        rabbitmq:
        image: rabbitmq:3.7.2-management-alpine
        restart: always
        environment:
        RABBITMQ_HIPE_COMPILE: 1
        RABBITMQ_MANAGEMENT: 1
        RABBITMQ_VM_MEMORY_HIGH_WATERMARK: 0.2
        RABBITMQ_DEFAULT_USER: "rabbitmq"
        RABBITMQ_DEFAULT_PASS: "rabbitmq"
        ports:
        - "15672:15672"
        - "5672:5672"
        volumes:
        - data:/var/lib/rabbitmq:rw

        start_dependencies:
        image: alpine:latest
        links:
        - rabbitmq
        command: >
        /bin/sh -c "
        echo Waiting for rabbitmq service start...;
        while ! nc -z rabbitmq 5672;
        do
        sleep 1;
        done;
        echo Connected!;
        "

        volumes:
        data:


        Then I do for run =>:



        docker-compose up start_dependencies



        rabbitmq service will start in daemon mode, start_dependencies will finish the work.






        share|improve this answer




















        • lol, making query via "curl", "-f", "http://localhost:15672" for which u need to install management plugin and using healthcheck that already deprecated - its best answer. Simple working example with check via nc its - downvote. ha, ok...
          – Igor Komar
          Jan 12 at 1:26











        • the answer does not use a native docker feature, its irrelevant if you use curl, nc or other tools. while! nc is the same as already posted in other answers.
          – svenhornberg
          Jan 12 at 8:09






        • 1




          native docker features: 1. docs.docker.com/compose/startup-order 2. github.com/docker/compose/issues/5007 3. github.com/docker/compose/issues/374
          – Igor Komar
          Jan 12 at 9:00











        • i will upvote your link
          – svenhornberg
          Jan 12 at 9:34










        • @IgorKomar, thanks man, you saved my day! :3 I used almost same mechanic to check mysql server is ready before actual application is started. ;) I am passing similar command to the docker-compose run --name app-test --rm "app" bash -l -c 'echo Waiting for mysql service start... && while ! nc -z db-server 3306; do sleep 1; done && echo Connected! && /bin/bash /script/ci_tests.sh'
          – TooroSan
          Oct 11 at 0:49


















        up vote
        0
        down vote













        One of the alternative solution is to use a container orchestration solution like Kubernetes. Kubernetes has support for init containers which run to completion before other containers can start. You can find an example here with SQL Server 2017 Linux container where API container uses init container to initialise a database



        https://www.handsonarchitect.com/2018/08/understand-kubernetes-object-init.html






        share|improve this answer



























          up vote
          0
          down vote













          I couldn't use this approach, I actually can't use volume in docker-compose, in both cases I didn't work:




          s-flyway_1_d7a78571d25c | env: can't execute 'bash': No such file or
          directory docker_s-flyway_1_d7a78571d25c exited with code 127




          What am I missing here ? I'm a little lost about it right now.....




          Project link:
          https://github.com/jbrasileiro/introducing-example/blob/master/introducing-example-docker-flyway/src/docker/docker-compose.yml




          Dockerfile



          FROM dhoer/flyway:alpine
          COPY ./sql/*.sql /flyway/sql
          VOLUME ./wait-for-it.sh:/usr/local/bin/wait-for-it.sh
          ADD ./wait-for-it.sh /
          RUN chmod 777 /wait-for-it.sh


          docker version : '3'



          version: '3'
          services:
          hello-world:
          image: ubuntu
          volumes:
          - ./wait-for-it.sh:/usr/local/bin/wait-for-it.sh
          command:
          - wait-for-it.sh
          - github.com:80
          - --
          - echo
          - "hello world"


          docker version : '2'



          version: '2'
          services:
          s-postgres:
          image: postgres:9.4-alpine
          hostname: host-postgres
          environment:
          - POSTGRES_USER=postgres
          - POSTGRES_PASSWORD=postgres
          - POSTGRES_DB=postgres
          networks:
          - network-docker
          s-flyway:
          image: dhoer/flyway:alpine
          build: flyway/
          entrypoint: /wait-for-it.sh s-postgres 5432 --timeout=0 --strict
          command: -url=jdbc:postgresql://s-postgres:5432/postgres -baselineOnMigrate=true -schemas=public -user=postgres -password=postgres migrate
          networks:
          - network-docker
          depends_on:
          - s-postgres
          networks:
          network-docker:
          driver: bridge





          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f31746182%2fdocker-compose-wait-for-container-x-before-starting-y%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            12 Answers
            12






            active

            oldest

            votes








            12 Answers
            12






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            152
            down vote



            accepted










            Finally found a solution with a docker-compose method. Since docker-compose file format 2.1 you can define healthchecks.



            I did it in a example project
            you need to install at least docker 1.12.0+.
            I also needed to extend the rabbitmq-management Dockerfile, because curl isn't installed on the official image.



            Now I test if the management page of the rabbitmq-container is available. If curl finishes with exitcode 0 the container app (python pika) will be started and publish a message to hello queue. Its now working (output).



            docker-compose (version 2.1):



            version: '2.1'

            services:
            app:
            build: app/.
            depends_on:
            rabbit:
            condition: service_healthy
            links:
            - rabbit

            rabbit:
            build: rabbitmq/.
            ports:
            - "15672:15672"
            - "5672:5672"
            healthcheck:
            test: ["CMD", "curl", "-f", "http://localhost:15672"]
            interval: 30s
            timeout: 10s
            retries: 5


            output:



            rabbit_1 | =INFO REPORT==== 25-Jan-2017::14:44:21 ===
            rabbit_1 | closing AMQP connection <0.718.0> (172.18.0.3:36590 -> 172.18.0.2:5672)
            app_1 | [x] Sent 'Hello World!'
            healthcheckcompose_app_1 exited with code 0


            Dockerfile (rabbitmq + curl):



            FROM rabbitmq:3-management
            RUN apt-get update
            RUN apt-get install -y curl
            EXPOSE 4369 5671 5672 25672 15671 15672


            Version 3 no longer supports the condition form of depends_on.
            So i moved from depends_on to restart on-failure. Now my app container will restart 2-3 times until it is working, but it is still a docker-compose feature without overwriting the entrypoint.



            docker-compose (version 3):



            version: "3"

            services:

            rabbitmq: # login guest:guest
            image: rabbitmq:management
            ports:
            - "4369:4369"
            - "5671:5671"
            - "5672:5672"
            - "25672:25672"
            - "15671:15671"
            - "15672:15672"
            healthcheck:
            test: ["CMD", "curl", "-f", "http://localhost:15672"]
            interval: 30s
            timeout: 10s
            retries: 5

            app:
            build: ./app/
            environment:
            - HOSTNAMERABBIT=rabbitmq
            restart: on-failure
            depends_on:
            - rabbitmq
            links:
            - rabbitmq





            share|improve this answer


















            • 6




              @svenhornberg ping uses ICMP so doesn't support TCP ports. Maybe nc to test a TCP port. Probably better to use psql -h localhost -p 5432 and query something.
              – Matt
              Feb 13 '17 at 5:14






            • 23




              "depends on" has been removed in version 3 docs.docker.com/compose/compose-file/#dependson
              – nha
              Apr 3 '17 at 11:08






            • 2




              I think the main reason is, that an application itself should always be able to wait or recovery if others services are not available (look for resilience in micro service context)
              – svenhornberg
              Apr 20 '17 at 8:33







            • 27




              @nha It looks like the condition form of depends_on is removed, but depends_on itself is still around in v3
              – akivajgordon
              Jul 26 '17 at 20:25






            • 6




              How can healthchecks still be used to control startup order if depends_on with condition has been removed?
              – Franz
              Aug 31 '17 at 15:49














            up vote
            152
            down vote



            accepted










            Finally found a solution with a docker-compose method. Since docker-compose file format 2.1 you can define healthchecks.



            I did it in a example project
            you need to install at least docker 1.12.0+.
            I also needed to extend the rabbitmq-management Dockerfile, because curl isn't installed on the official image.



            Now I test if the management page of the rabbitmq-container is available. If curl finishes with exitcode 0 the container app (python pika) will be started and publish a message to hello queue. Its now working (output).



            docker-compose (version 2.1):



            version: '2.1'

            services:
            app:
            build: app/.
            depends_on:
            rabbit:
            condition: service_healthy
            links:
            - rabbit

            rabbit:
            build: rabbitmq/.
            ports:
            - "15672:15672"
            - "5672:5672"
            healthcheck:
            test: ["CMD", "curl", "-f", "http://localhost:15672"]
            interval: 30s
            timeout: 10s
            retries: 5


            output:



            rabbit_1 | =INFO REPORT==== 25-Jan-2017::14:44:21 ===
            rabbit_1 | closing AMQP connection <0.718.0> (172.18.0.3:36590 -> 172.18.0.2:5672)
            app_1 | [x] Sent 'Hello World!'
            healthcheckcompose_app_1 exited with code 0


            Dockerfile (rabbitmq + curl):



            FROM rabbitmq:3-management
            RUN apt-get update
            RUN apt-get install -y curl
            EXPOSE 4369 5671 5672 25672 15671 15672


            Version 3 no longer supports the condition form of depends_on.
            So i moved from depends_on to restart on-failure. Now my app container will restart 2-3 times until it is working, but it is still a docker-compose feature without overwriting the entrypoint.



            docker-compose (version 3):



            version: "3"

            services:

            rabbitmq: # login guest:guest
            image: rabbitmq:management
            ports:
            - "4369:4369"
            - "5671:5671"
            - "5672:5672"
            - "25672:25672"
            - "15671:15671"
            - "15672:15672"
            healthcheck:
            test: ["CMD", "curl", "-f", "http://localhost:15672"]
            interval: 30s
            timeout: 10s
            retries: 5

            app:
            build: ./app/
            environment:
            - HOSTNAMERABBIT=rabbitmq
            restart: on-failure
            depends_on:
            - rabbitmq
            links:
            - rabbitmq





            share|improve this answer


















            • 6




              @svenhornberg ping uses ICMP so doesn't support TCP ports. Maybe nc to test a TCP port. Probably better to use psql -h localhost -p 5432 and query something.
              – Matt
              Feb 13 '17 at 5:14






            • 23




              "depends on" has been removed in version 3 docs.docker.com/compose/compose-file/#dependson
              – nha
              Apr 3 '17 at 11:08






            • 2




              I think the main reason is, that an application itself should always be able to wait or recovery if others services are not available (look for resilience in micro service context)
              – svenhornberg
              Apr 20 '17 at 8:33







            • 27




              @nha It looks like the condition form of depends_on is removed, but depends_on itself is still around in v3
              – akivajgordon
              Jul 26 '17 at 20:25






            • 6




              How can healthchecks still be used to control startup order if depends_on with condition has been removed?
              – Franz
              Aug 31 '17 at 15:49












            up vote
            152
            down vote



            accepted







            up vote
            152
            down vote



            accepted






            Finally found a solution with a docker-compose method. Since docker-compose file format 2.1 you can define healthchecks.



            I did it in a example project
            you need to install at least docker 1.12.0+.
            I also needed to extend the rabbitmq-management Dockerfile, because curl isn't installed on the official image.



            Now I test if the management page of the rabbitmq-container is available. If curl finishes with exitcode 0 the container app (python pika) will be started and publish a message to hello queue. Its now working (output).



            docker-compose (version 2.1):



            version: '2.1'

            services:
            app:
            build: app/.
            depends_on:
            rabbit:
            condition: service_healthy
            links:
            - rabbit

            rabbit:
            build: rabbitmq/.
            ports:
            - "15672:15672"
            - "5672:5672"
            healthcheck:
            test: ["CMD", "curl", "-f", "http://localhost:15672"]
            interval: 30s
            timeout: 10s
            retries: 5


            output:



            rabbit_1 | =INFO REPORT==== 25-Jan-2017::14:44:21 ===
            rabbit_1 | closing AMQP connection <0.718.0> (172.18.0.3:36590 -> 172.18.0.2:5672)
            app_1 | [x] Sent 'Hello World!'
            healthcheckcompose_app_1 exited with code 0


            Dockerfile (rabbitmq + curl):



            FROM rabbitmq:3-management
            RUN apt-get update
            RUN apt-get install -y curl
            EXPOSE 4369 5671 5672 25672 15671 15672


            Version 3 no longer supports the condition form of depends_on.
            So i moved from depends_on to restart on-failure. Now my app container will restart 2-3 times until it is working, but it is still a docker-compose feature without overwriting the entrypoint.



            docker-compose (version 3):



            version: "3"

            services:

            rabbitmq: # login guest:guest
            image: rabbitmq:management
            ports:
            - "4369:4369"
            - "5671:5671"
            - "5672:5672"
            - "25672:25672"
            - "15671:15671"
            - "15672:15672"
            healthcheck:
            test: ["CMD", "curl", "-f", "http://localhost:15672"]
            interval: 30s
            timeout: 10s
            retries: 5

            app:
            build: ./app/
            environment:
            - HOSTNAMERABBIT=rabbitmq
            restart: on-failure
            depends_on:
            - rabbitmq
            links:
            - rabbitmq





            share|improve this answer














            Finally found a solution with a docker-compose method. Since docker-compose file format 2.1 you can define healthchecks.



            I did it in a example project
            you need to install at least docker 1.12.0+.
            I also needed to extend the rabbitmq-management Dockerfile, because curl isn't installed on the official image.



            Now I test if the management page of the rabbitmq-container is available. If curl finishes with exitcode 0 the container app (python pika) will be started and publish a message to hello queue. Its now working (output).



            docker-compose (version 2.1):



            version: '2.1'

            services:
            app:
            build: app/.
            depends_on:
            rabbit:
            condition: service_healthy
            links:
            - rabbit

            rabbit:
            build: rabbitmq/.
            ports:
            - "15672:15672"
            - "5672:5672"
            healthcheck:
            test: ["CMD", "curl", "-f", "http://localhost:15672"]
            interval: 30s
            timeout: 10s
            retries: 5


            output:



            rabbit_1 | =INFO REPORT==== 25-Jan-2017::14:44:21 ===
            rabbit_1 | closing AMQP connection <0.718.0> (172.18.0.3:36590 -> 172.18.0.2:5672)
            app_1 | [x] Sent 'Hello World!'
            healthcheckcompose_app_1 exited with code 0


            Dockerfile (rabbitmq + curl):



            FROM rabbitmq:3-management
            RUN apt-get update
            RUN apt-get install -y curl
            EXPOSE 4369 5671 5672 25672 15671 15672


            Version 3 no longer supports the condition form of depends_on.
            So i moved from depends_on to restart on-failure. Now my app container will restart 2-3 times until it is working, but it is still a docker-compose feature without overwriting the entrypoint.



            docker-compose (version 3):



            version: "3"

            services:

            rabbitmq: # login guest:guest
            image: rabbitmq:management
            ports:
            - "4369:4369"
            - "5671:5671"
            - "5672:5672"
            - "25672:25672"
            - "15671:15671"
            - "15672:15672"
            healthcheck:
            test: ["CMD", "curl", "-f", "http://localhost:15672"]
            interval: 30s
            timeout: 10s
            retries: 5

            app:
            build: ./app/
            environment:
            - HOSTNAMERABBIT=rabbitmq
            restart: on-failure
            depends_on:
            - rabbitmq
            links:
            - rabbitmq






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 9 at 10:39

























            answered Jan 25 '17 at 15:10









            svenhornberg

            3,82162643




            3,82162643







            • 6




              @svenhornberg ping uses ICMP so doesn't support TCP ports. Maybe nc to test a TCP port. Probably better to use psql -h localhost -p 5432 and query something.
              – Matt
              Feb 13 '17 at 5:14






            • 23




              "depends on" has been removed in version 3 docs.docker.com/compose/compose-file/#dependson
              – nha
              Apr 3 '17 at 11:08






            • 2




              I think the main reason is, that an application itself should always be able to wait or recovery if others services are not available (look for resilience in micro service context)
              – svenhornberg
              Apr 20 '17 at 8:33







            • 27




              @nha It looks like the condition form of depends_on is removed, but depends_on itself is still around in v3
              – akivajgordon
              Jul 26 '17 at 20:25






            • 6




              How can healthchecks still be used to control startup order if depends_on with condition has been removed?
              – Franz
              Aug 31 '17 at 15:49












            • 6




              @svenhornberg ping uses ICMP so doesn't support TCP ports. Maybe nc to test a TCP port. Probably better to use psql -h localhost -p 5432 and query something.
              – Matt
              Feb 13 '17 at 5:14






            • 23




              "depends on" has been removed in version 3 docs.docker.com/compose/compose-file/#dependson
              – nha
              Apr 3 '17 at 11:08






            • 2




              I think the main reason is, that an application itself should always be able to wait or recovery if others services are not available (look for resilience in micro service context)
              – svenhornberg
              Apr 20 '17 at 8:33







            • 27




              @nha It looks like the condition form of depends_on is removed, but depends_on itself is still around in v3
              – akivajgordon
              Jul 26 '17 at 20:25






            • 6




              How can healthchecks still be used to control startup order if depends_on with condition has been removed?
              – Franz
              Aug 31 '17 at 15:49







            6




            6




            @svenhornberg ping uses ICMP so doesn't support TCP ports. Maybe nc to test a TCP port. Probably better to use psql -h localhost -p 5432 and query something.
            – Matt
            Feb 13 '17 at 5:14




            @svenhornberg ping uses ICMP so doesn't support TCP ports. Maybe nc to test a TCP port. Probably better to use psql -h localhost -p 5432 and query something.
            – Matt
            Feb 13 '17 at 5:14




            23




            23




            "depends on" has been removed in version 3 docs.docker.com/compose/compose-file/#dependson
            – nha
            Apr 3 '17 at 11:08




            "depends on" has been removed in version 3 docs.docker.com/compose/compose-file/#dependson
            – nha
            Apr 3 '17 at 11:08




            2




            2




            I think the main reason is, that an application itself should always be able to wait or recovery if others services are not available (look for resilience in micro service context)
            – svenhornberg
            Apr 20 '17 at 8:33





            I think the main reason is, that an application itself should always be able to wait or recovery if others services are not available (look for resilience in micro service context)
            – svenhornberg
            Apr 20 '17 at 8:33





            27




            27




            @nha It looks like the condition form of depends_on is removed, but depends_on itself is still around in v3
            – akivajgordon
            Jul 26 '17 at 20:25




            @nha It looks like the condition form of depends_on is removed, but depends_on itself is still around in v3
            – akivajgordon
            Jul 26 '17 at 20:25




            6




            6




            How can healthchecks still be used to control startup order if depends_on with condition has been removed?
            – Franz
            Aug 31 '17 at 15:49




            How can healthchecks still be used to control startup order if depends_on with condition has been removed?
            – Franz
            Aug 31 '17 at 15:49












            up vote
            61
            down vote













            Natively that is not possible, yet. See also this feature request.



            So far you need to do that in your containers CMD to wait until all required services are there.



            In the Dockerfiles CMD you could refer to your own start script that wraps starting up your container service. Before you start it, you wait for a depending one like:



            Dockerfile



            FROM python:2-onbuild
            RUN ["pip", "install", "pika"]
            ADD start.sh /start.sh
            CMD ["/start.sh"]


            start.sh



            #!/bin/bash
            while ! nc -z rabbitmq 5672; do sleep 3; done
            python rabbit.py


            Probably you need to install netcat in your Dockerfile as well. I do not know what is pre-installed on the python image.



            There are a few tools out there that provide easy to use waiting logic, for simple tcp port checks:




            • wait-for-it

            • dockerize

            For more complex waits:




            • goss - Explanation blog





            share|improve this answer






















            • Could you explain what you mean by CMD ? Does this mean my programm has to do it, like i did it with a port check ? Or do you mean a specific CMD from e.g. linux for this ?
              – svenhornberg
              Jul 31 '15 at 22:56











            • thank you for explaining, I upvote your answer.But I think the upcoming feature request, would be the right answer to my question so I leave it unanswered so far.
              – svenhornberg
              Aug 1 '15 at 9:27















            up vote
            61
            down vote













            Natively that is not possible, yet. See also this feature request.



            So far you need to do that in your containers CMD to wait until all required services are there.



            In the Dockerfiles CMD you could refer to your own start script that wraps starting up your container service. Before you start it, you wait for a depending one like:



            Dockerfile



            FROM python:2-onbuild
            RUN ["pip", "install", "pika"]
            ADD start.sh /start.sh
            CMD ["/start.sh"]


            start.sh



            #!/bin/bash
            while ! nc -z rabbitmq 5672; do sleep 3; done
            python rabbit.py


            Probably you need to install netcat in your Dockerfile as well. I do not know what is pre-installed on the python image.



            There are a few tools out there that provide easy to use waiting logic, for simple tcp port checks:




            • wait-for-it

            • dockerize

            For more complex waits:




            • goss - Explanation blog





            share|improve this answer






















            • Could you explain what you mean by CMD ? Does this mean my programm has to do it, like i did it with a port check ? Or do you mean a specific CMD from e.g. linux for this ?
              – svenhornberg
              Jul 31 '15 at 22:56











            • thank you for explaining, I upvote your answer.But I think the upcoming feature request, would be the right answer to my question so I leave it unanswered so far.
              – svenhornberg
              Aug 1 '15 at 9:27













            up vote
            61
            down vote










            up vote
            61
            down vote









            Natively that is not possible, yet. See also this feature request.



            So far you need to do that in your containers CMD to wait until all required services are there.



            In the Dockerfiles CMD you could refer to your own start script that wraps starting up your container service. Before you start it, you wait for a depending one like:



            Dockerfile



            FROM python:2-onbuild
            RUN ["pip", "install", "pika"]
            ADD start.sh /start.sh
            CMD ["/start.sh"]


            start.sh



            #!/bin/bash
            while ! nc -z rabbitmq 5672; do sleep 3; done
            python rabbit.py


            Probably you need to install netcat in your Dockerfile as well. I do not know what is pre-installed on the python image.



            There are a few tools out there that provide easy to use waiting logic, for simple tcp port checks:




            • wait-for-it

            • dockerize

            For more complex waits:




            • goss - Explanation blog





            share|improve this answer














            Natively that is not possible, yet. See also this feature request.



            So far you need to do that in your containers CMD to wait until all required services are there.



            In the Dockerfiles CMD you could refer to your own start script that wraps starting up your container service. Before you start it, you wait for a depending one like:



            Dockerfile



            FROM python:2-onbuild
            RUN ["pip", "install", "pika"]
            ADD start.sh /start.sh
            CMD ["/start.sh"]


            start.sh



            #!/bin/bash
            while ! nc -z rabbitmq 5672; do sleep 3; done
            python rabbit.py


            Probably you need to install netcat in your Dockerfile as well. I do not know what is pre-installed on the python image.



            There are a few tools out there that provide easy to use waiting logic, for simple tcp port checks:




            • wait-for-it

            • dockerize

            For more complex waits:




            • goss - Explanation blog






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 5 '16 at 2:55









            Ahmed Elsabbahy

            1052




            1052










            answered Jul 31 '15 at 19:25









            h3nrik

            23.6k73145




            23.6k73145











            • Could you explain what you mean by CMD ? Does this mean my programm has to do it, like i did it with a port check ? Or do you mean a specific CMD from e.g. linux for this ?
              – svenhornberg
              Jul 31 '15 at 22:56











            • thank you for explaining, I upvote your answer.But I think the upcoming feature request, would be the right answer to my question so I leave it unanswered so far.
              – svenhornberg
              Aug 1 '15 at 9:27

















            • Could you explain what you mean by CMD ? Does this mean my programm has to do it, like i did it with a port check ? Or do you mean a specific CMD from e.g. linux for this ?
              – svenhornberg
              Jul 31 '15 at 22:56











            • thank you for explaining, I upvote your answer.But I think the upcoming feature request, would be the right answer to my question so I leave it unanswered so far.
              – svenhornberg
              Aug 1 '15 at 9:27
















            Could you explain what you mean by CMD ? Does this mean my programm has to do it, like i did it with a port check ? Or do you mean a specific CMD from e.g. linux for this ?
            – svenhornberg
            Jul 31 '15 at 22:56





            Could you explain what you mean by CMD ? Does this mean my programm has to do it, like i did it with a port check ? Or do you mean a specific CMD from e.g. linux for this ?
            – svenhornberg
            Jul 31 '15 at 22:56













            thank you for explaining, I upvote your answer.But I think the upcoming feature request, would be the right answer to my question so I leave it unanswered so far.
            – svenhornberg
            Aug 1 '15 at 9:27





            thank you for explaining, I upvote your answer.But I think the upcoming feature request, would be the right answer to my question so I leave it unanswered so far.
            – svenhornberg
            Aug 1 '15 at 9:27











            up vote
            32
            down vote













            Using restart: unless-stopped or restart: always may solve this problem.



            If worker container stops when rabbitMQ is not ready, it will be restarted until it is.






            share|improve this answer
















            • 3




              I like this solution for this case, but it doesn't work for containers that don't exit when one of the subprocesses that it runs fails. For example, a Tomcat container would continue to run even if a Java servlet that it ran were to fail to connect to a database server. Granted, Docker containers render servlet containers like Tomcat mostly unnecessary.
              – Derek Mahar
              May 6 '16 at 1:50











            • @DerekMahar, if you have a Java-based web application that only serves REST calls, what do you use instead of Jetty/Tomcat?
              – JoeG
              Dec 21 '16 at 19:31






            • 2




              @JoeG, I meant Tomcat the servlet container that can host many applications, not embedded Tomcat. Docker renders the former mostly unnecessary, while making the latter more popular for microservices, for example.
              – Derek Mahar
              Dec 21 '16 at 20:11














            up vote
            32
            down vote













            Using restart: unless-stopped or restart: always may solve this problem.



            If worker container stops when rabbitMQ is not ready, it will be restarted until it is.






            share|improve this answer
















            • 3




              I like this solution for this case, but it doesn't work for containers that don't exit when one of the subprocesses that it runs fails. For example, a Tomcat container would continue to run even if a Java servlet that it ran were to fail to connect to a database server. Granted, Docker containers render servlet containers like Tomcat mostly unnecessary.
              – Derek Mahar
              May 6 '16 at 1:50











            • @DerekMahar, if you have a Java-based web application that only serves REST calls, what do you use instead of Jetty/Tomcat?
              – JoeG
              Dec 21 '16 at 19:31






            • 2




              @JoeG, I meant Tomcat the servlet container that can host many applications, not embedded Tomcat. Docker renders the former mostly unnecessary, while making the latter more popular for microservices, for example.
              – Derek Mahar
              Dec 21 '16 at 20:11












            up vote
            32
            down vote










            up vote
            32
            down vote









            Using restart: unless-stopped or restart: always may solve this problem.



            If worker container stops when rabbitMQ is not ready, it will be restarted until it is.






            share|improve this answer












            Using restart: unless-stopped or restart: always may solve this problem.



            If worker container stops when rabbitMQ is not ready, it will be restarted until it is.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 3 '16 at 7:21









            Toilal

            2,2971726




            2,2971726







            • 3




              I like this solution for this case, but it doesn't work for containers that don't exit when one of the subprocesses that it runs fails. For example, a Tomcat container would continue to run even if a Java servlet that it ran were to fail to connect to a database server. Granted, Docker containers render servlet containers like Tomcat mostly unnecessary.
              – Derek Mahar
              May 6 '16 at 1:50











            • @DerekMahar, if you have a Java-based web application that only serves REST calls, what do you use instead of Jetty/Tomcat?
              – JoeG
              Dec 21 '16 at 19:31






            • 2




              @JoeG, I meant Tomcat the servlet container that can host many applications, not embedded Tomcat. Docker renders the former mostly unnecessary, while making the latter more popular for microservices, for example.
              – Derek Mahar
              Dec 21 '16 at 20:11












            • 3




              I like this solution for this case, but it doesn't work for containers that don't exit when one of the subprocesses that it runs fails. For example, a Tomcat container would continue to run even if a Java servlet that it ran were to fail to connect to a database server. Granted, Docker containers render servlet containers like Tomcat mostly unnecessary.
              – Derek Mahar
              May 6 '16 at 1:50











            • @DerekMahar, if you have a Java-based web application that only serves REST calls, what do you use instead of Jetty/Tomcat?
              – JoeG
              Dec 21 '16 at 19:31






            • 2




              @JoeG, I meant Tomcat the servlet container that can host many applications, not embedded Tomcat. Docker renders the former mostly unnecessary, while making the latter more popular for microservices, for example.
              – Derek Mahar
              Dec 21 '16 at 20:11







            3




            3




            I like this solution for this case, but it doesn't work for containers that don't exit when one of the subprocesses that it runs fails. For example, a Tomcat container would continue to run even if a Java servlet that it ran were to fail to connect to a database server. Granted, Docker containers render servlet containers like Tomcat mostly unnecessary.
            – Derek Mahar
            May 6 '16 at 1:50





            I like this solution for this case, but it doesn't work for containers that don't exit when one of the subprocesses that it runs fails. For example, a Tomcat container would continue to run even if a Java servlet that it ran were to fail to connect to a database server. Granted, Docker containers render servlet containers like Tomcat mostly unnecessary.
            – Derek Mahar
            May 6 '16 at 1:50













            @DerekMahar, if you have a Java-based web application that only serves REST calls, what do you use instead of Jetty/Tomcat?
            – JoeG
            Dec 21 '16 at 19:31




            @DerekMahar, if you have a Java-based web application that only serves REST calls, what do you use instead of Jetty/Tomcat?
            – JoeG
            Dec 21 '16 at 19:31




            2




            2




            @JoeG, I meant Tomcat the servlet container that can host many applications, not embedded Tomcat. Docker renders the former mostly unnecessary, while making the latter more popular for microservices, for example.
            – Derek Mahar
            Dec 21 '16 at 20:11




            @JoeG, I meant Tomcat the servlet container that can host many applications, not embedded Tomcat. Docker renders the former mostly unnecessary, while making the latter more popular for microservices, for example.
            – Derek Mahar
            Dec 21 '16 at 20:11










            up vote
            26
            down vote













            Quite recently they've added the depends_on feature.



            Edit:



            As of compose version 2.1+ you can use depends_on in conjunction with healthcheck to achieve this:



            From the docs:



            version: '2.1'
            services:
            web:
            build: .
            depends_on:
            db:
            condition: service_healthy
            redis:
            condition: service_started
            redis:
            image: redis
            db:
            image: redis
            healthcheck:
            test: "exit 0"


            Before version 2.1



            You can still use depends_on, but it only effects the order in which services are started - not if they are ready before the dependant service is started.



            It seems to require at least version 1.6.0.



            Usage would look something like this:



            version: '2'
            services:
            web:
            build: .
            depends_on:
            - db
            - redis
            redis:
            image: redis
            db:
            image: postgres


            From the docs:




            Express dependency between services, which has two effects:



            • docker-compose up will start services in dependency order. In the following example, db and redis will be started before web.

            • docker-compose up SERVICE will automatically include SERVICE’s dependencies. In the following example, docker-compose up web will also create and start db and redis.



            Note: As I understand it, although this does set the order in which containers are loaded. It does not guarantee that the service inside the container has actually loaded.



            For example, you postgres container might be up. But the postgres service itself might still be initializing within the container.






            share|improve this answer


















            • 9




              dnephin wrote: depends_on is only ordering. To actually delay the starting of another container there would need to be some way to detect when a process has finished initializing itself.
              – svenhornberg
              Mar 1 '16 at 7:15






            • 11




              "Version 3 no longer supports the condition form of depends_on." docs.docker.com/compose/compose-file/#dependson
              – akauppi
              Apr 12 '17 at 14:30










            • depends_on doesn't wait until the container is in ready state(whatever that can mean in your case). It only waits until the container is in 'running' state.
              – htyagi
              Aug 31 '17 at 13:20














            up vote
            26
            down vote













            Quite recently they've added the depends_on feature.



            Edit:



            As of compose version 2.1+ you can use depends_on in conjunction with healthcheck to achieve this:



            From the docs:



            version: '2.1'
            services:
            web:
            build: .
            depends_on:
            db:
            condition: service_healthy
            redis:
            condition: service_started
            redis:
            image: redis
            db:
            image: redis
            healthcheck:
            test: "exit 0"


            Before version 2.1



            You can still use depends_on, but it only effects the order in which services are started - not if they are ready before the dependant service is started.



            It seems to require at least version 1.6.0.



            Usage would look something like this:



            version: '2'
            services:
            web:
            build: .
            depends_on:
            - db
            - redis
            redis:
            image: redis
            db:
            image: postgres


            From the docs:




            Express dependency between services, which has two effects:



            • docker-compose up will start services in dependency order. In the following example, db and redis will be started before web.

            • docker-compose up SERVICE will automatically include SERVICE’s dependencies. In the following example, docker-compose up web will also create and start db and redis.



            Note: As I understand it, although this does set the order in which containers are loaded. It does not guarantee that the service inside the container has actually loaded.



            For example, you postgres container might be up. But the postgres service itself might still be initializing within the container.






            share|improve this answer


















            • 9




              dnephin wrote: depends_on is only ordering. To actually delay the starting of another container there would need to be some way to detect when a process has finished initializing itself.
              – svenhornberg
              Mar 1 '16 at 7:15






            • 11




              "Version 3 no longer supports the condition form of depends_on." docs.docker.com/compose/compose-file/#dependson
              – akauppi
              Apr 12 '17 at 14:30










            • depends_on doesn't wait until the container is in ready state(whatever that can mean in your case). It only waits until the container is in 'running' state.
              – htyagi
              Aug 31 '17 at 13:20












            up vote
            26
            down vote










            up vote
            26
            down vote









            Quite recently they've added the depends_on feature.



            Edit:



            As of compose version 2.1+ you can use depends_on in conjunction with healthcheck to achieve this:



            From the docs:



            version: '2.1'
            services:
            web:
            build: .
            depends_on:
            db:
            condition: service_healthy
            redis:
            condition: service_started
            redis:
            image: redis
            db:
            image: redis
            healthcheck:
            test: "exit 0"


            Before version 2.1



            You can still use depends_on, but it only effects the order in which services are started - not if they are ready before the dependant service is started.



            It seems to require at least version 1.6.0.



            Usage would look something like this:



            version: '2'
            services:
            web:
            build: .
            depends_on:
            - db
            - redis
            redis:
            image: redis
            db:
            image: postgres


            From the docs:




            Express dependency between services, which has two effects:



            • docker-compose up will start services in dependency order. In the following example, db and redis will be started before web.

            • docker-compose up SERVICE will automatically include SERVICE’s dependencies. In the following example, docker-compose up web will also create and start db and redis.



            Note: As I understand it, although this does set the order in which containers are loaded. It does not guarantee that the service inside the container has actually loaded.



            For example, you postgres container might be up. But the postgres service itself might still be initializing within the container.






            share|improve this answer














            Quite recently they've added the depends_on feature.



            Edit:



            As of compose version 2.1+ you can use depends_on in conjunction with healthcheck to achieve this:



            From the docs:



            version: '2.1'
            services:
            web:
            build: .
            depends_on:
            db:
            condition: service_healthy
            redis:
            condition: service_started
            redis:
            image: redis
            db:
            image: redis
            healthcheck:
            test: "exit 0"


            Before version 2.1



            You can still use depends_on, but it only effects the order in which services are started - not if they are ready before the dependant service is started.



            It seems to require at least version 1.6.0.



            Usage would look something like this:



            version: '2'
            services:
            web:
            build: .
            depends_on:
            - db
            - redis
            redis:
            image: redis
            db:
            image: postgres


            From the docs:




            Express dependency between services, which has two effects:



            • docker-compose up will start services in dependency order. In the following example, db and redis will be started before web.

            • docker-compose up SERVICE will automatically include SERVICE’s dependencies. In the following example, docker-compose up web will also create and start db and redis.



            Note: As I understand it, although this does set the order in which containers are loaded. It does not guarantee that the service inside the container has actually loaded.



            For example, you postgres container might be up. But the postgres service itself might still be initializing within the container.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 13 '17 at 19:08

























            answered Feb 18 '16 at 8:58









            toast38coza

            4,6662824




            4,6662824







            • 9




              dnephin wrote: depends_on is only ordering. To actually delay the starting of another container there would need to be some way to detect when a process has finished initializing itself.
              – svenhornberg
              Mar 1 '16 at 7:15






            • 11




              "Version 3 no longer supports the condition form of depends_on." docs.docker.com/compose/compose-file/#dependson
              – akauppi
              Apr 12 '17 at 14:30










            • depends_on doesn't wait until the container is in ready state(whatever that can mean in your case). It only waits until the container is in 'running' state.
              – htyagi
              Aug 31 '17 at 13:20












            • 9




              dnephin wrote: depends_on is only ordering. To actually delay the starting of another container there would need to be some way to detect when a process has finished initializing itself.
              – svenhornberg
              Mar 1 '16 at 7:15






            • 11




              "Version 3 no longer supports the condition form of depends_on." docs.docker.com/compose/compose-file/#dependson
              – akauppi
              Apr 12 '17 at 14:30










            • depends_on doesn't wait until the container is in ready state(whatever that can mean in your case). It only waits until the container is in 'running' state.
              – htyagi
              Aug 31 '17 at 13:20







            9




            9




            dnephin wrote: depends_on is only ordering. To actually delay the starting of another container there would need to be some way to detect when a process has finished initializing itself.
            – svenhornberg
            Mar 1 '16 at 7:15




            dnephin wrote: depends_on is only ordering. To actually delay the starting of another container there would need to be some way to detect when a process has finished initializing itself.
            – svenhornberg
            Mar 1 '16 at 7:15




            11




            11




            "Version 3 no longer supports the condition form of depends_on." docs.docker.com/compose/compose-file/#dependson
            – akauppi
            Apr 12 '17 at 14:30




            "Version 3 no longer supports the condition form of depends_on." docs.docker.com/compose/compose-file/#dependson
            – akauppi
            Apr 12 '17 at 14:30












            depends_on doesn't wait until the container is in ready state(whatever that can mean in your case). It only waits until the container is in 'running' state.
            – htyagi
            Aug 31 '17 at 13:20




            depends_on doesn't wait until the container is in ready state(whatever that can mean in your case). It only waits until the container is in 'running' state.
            – htyagi
            Aug 31 '17 at 13:20










            up vote
            14
            down vote













            you can also just add it to the command option eg.



            command: bash -c "sleep 5; start.sh"


            https://github.com/docker/compose/issues/374#issuecomment-156546513



            to wait on a port you can also use something like this



            command: bash -c "while ! curl -s rabbitmq:5672 > /dev/null; do echo waiting for xxx; sleep 3; done; start.sh"


            to increment the waiting time you can hack a bit more:



            command: bash -c "for i in 1..100 ; do if ! curl -s rabbitmq:5672 > /dev/null ; then echo waiting on rabbitmq for $i seconds; sleep $i; fi; done; start.sh"





            share|improve this answer


























              up vote
              14
              down vote













              you can also just add it to the command option eg.



              command: bash -c "sleep 5; start.sh"


              https://github.com/docker/compose/issues/374#issuecomment-156546513



              to wait on a port you can also use something like this



              command: bash -c "while ! curl -s rabbitmq:5672 > /dev/null; do echo waiting for xxx; sleep 3; done; start.sh"


              to increment the waiting time you can hack a bit more:



              command: bash -c "for i in 1..100 ; do if ! curl -s rabbitmq:5672 > /dev/null ; then echo waiting on rabbitmq for $i seconds; sleep $i; fi; done; start.sh"





              share|improve this answer
























                up vote
                14
                down vote










                up vote
                14
                down vote









                you can also just add it to the command option eg.



                command: bash -c "sleep 5; start.sh"


                https://github.com/docker/compose/issues/374#issuecomment-156546513



                to wait on a port you can also use something like this



                command: bash -c "while ! curl -s rabbitmq:5672 > /dev/null; do echo waiting for xxx; sleep 3; done; start.sh"


                to increment the waiting time you can hack a bit more:



                command: bash -c "for i in 1..100 ; do if ! curl -s rabbitmq:5672 > /dev/null ; then echo waiting on rabbitmq for $i seconds; sleep $i; fi; done; start.sh"





                share|improve this answer














                you can also just add it to the command option eg.



                command: bash -c "sleep 5; start.sh"


                https://github.com/docker/compose/issues/374#issuecomment-156546513



                to wait on a port you can also use something like this



                command: bash -c "while ! curl -s rabbitmq:5672 > /dev/null; do echo waiting for xxx; sleep 3; done; start.sh"


                to increment the waiting time you can hack a bit more:



                command: bash -c "for i in 1..100 ; do if ! curl -s rabbitmq:5672 > /dev/null ; then echo waiting on rabbitmq for $i seconds; sleep $i; fi; done; start.sh"






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 4 '16 at 15:28

























                answered Feb 4 '16 at 13:54









                AmanicA

                3,32112644




                3,32112644




















                    up vote
                    5
                    down vote













                    You can also solve this by setting an endpoint which waits for the service to be up by using netcat (using the docker-wait script). I like this approach as you still have a clean command section in your docker-compose.yml and you don't need to add docker specific code to your application:



                    version: '2'
                    services:
                    db:
                    image: postgres
                    django:
                    build: .
                    command: python manage.py runserver 0.0.0.0:8000
                    entrypoint: ./docker-entrypoint.sh db 5432
                    volumes:
                    - .:/code
                    ports:
                    - "8000:8000"
                    depends_on:
                    - db


                    Then your docker-entrypoint.sh:



                    #!/bin/sh

                    postgres_host=$1
                    postgres_port=$2
                    shift 2
                    cmd="$@"

                    # wait for the postgres docker to be running
                    while ! nc $postgres_host $postgres_port; do
                    >&2 echo "Postgres is unavailable - sleeping"
                    sleep 1
                    done

                    >&2 echo "Postgres is up - executing command"

                    # run the command
                    exec $cmd


                    This is nowadays documented in the official docker documentation.



                    PS: You should install netcat in your docker instance if this is not available. To do so add this to your Docker file :



                    RUN apt-get update && apt-get install netcat-openbsd -y 





                    share|improve this answer


























                      up vote
                      5
                      down vote













                      You can also solve this by setting an endpoint which waits for the service to be up by using netcat (using the docker-wait script). I like this approach as you still have a clean command section in your docker-compose.yml and you don't need to add docker specific code to your application:



                      version: '2'
                      services:
                      db:
                      image: postgres
                      django:
                      build: .
                      command: python manage.py runserver 0.0.0.0:8000
                      entrypoint: ./docker-entrypoint.sh db 5432
                      volumes:
                      - .:/code
                      ports:
                      - "8000:8000"
                      depends_on:
                      - db


                      Then your docker-entrypoint.sh:



                      #!/bin/sh

                      postgres_host=$1
                      postgres_port=$2
                      shift 2
                      cmd="$@"

                      # wait for the postgres docker to be running
                      while ! nc $postgres_host $postgres_port; do
                      >&2 echo "Postgres is unavailable - sleeping"
                      sleep 1
                      done

                      >&2 echo "Postgres is up - executing command"

                      # run the command
                      exec $cmd


                      This is nowadays documented in the official docker documentation.



                      PS: You should install netcat in your docker instance if this is not available. To do so add this to your Docker file :



                      RUN apt-get update && apt-get install netcat-openbsd -y 





                      share|improve this answer
























                        up vote
                        5
                        down vote










                        up vote
                        5
                        down vote









                        You can also solve this by setting an endpoint which waits for the service to be up by using netcat (using the docker-wait script). I like this approach as you still have a clean command section in your docker-compose.yml and you don't need to add docker specific code to your application:



                        version: '2'
                        services:
                        db:
                        image: postgres
                        django:
                        build: .
                        command: python manage.py runserver 0.0.0.0:8000
                        entrypoint: ./docker-entrypoint.sh db 5432
                        volumes:
                        - .:/code
                        ports:
                        - "8000:8000"
                        depends_on:
                        - db


                        Then your docker-entrypoint.sh:



                        #!/bin/sh

                        postgres_host=$1
                        postgres_port=$2
                        shift 2
                        cmd="$@"

                        # wait for the postgres docker to be running
                        while ! nc $postgres_host $postgres_port; do
                        >&2 echo "Postgres is unavailable - sleeping"
                        sleep 1
                        done

                        >&2 echo "Postgres is up - executing command"

                        # run the command
                        exec $cmd


                        This is nowadays documented in the official docker documentation.



                        PS: You should install netcat in your docker instance if this is not available. To do so add this to your Docker file :



                        RUN apt-get update && apt-get install netcat-openbsd -y 





                        share|improve this answer














                        You can also solve this by setting an endpoint which waits for the service to be up by using netcat (using the docker-wait script). I like this approach as you still have a clean command section in your docker-compose.yml and you don't need to add docker specific code to your application:



                        version: '2'
                        services:
                        db:
                        image: postgres
                        django:
                        build: .
                        command: python manage.py runserver 0.0.0.0:8000
                        entrypoint: ./docker-entrypoint.sh db 5432
                        volumes:
                        - .:/code
                        ports:
                        - "8000:8000"
                        depends_on:
                        - db


                        Then your docker-entrypoint.sh:



                        #!/bin/sh

                        postgres_host=$1
                        postgres_port=$2
                        shift 2
                        cmd="$@"

                        # wait for the postgres docker to be running
                        while ! nc $postgres_host $postgres_port; do
                        >&2 echo "Postgres is unavailable - sleeping"
                        sleep 1
                        done

                        >&2 echo "Postgres is up - executing command"

                        # run the command
                        exec $cmd


                        This is nowadays documented in the official docker documentation.



                        PS: You should install netcat in your docker instance if this is not available. To do so add this to your Docker file :



                        RUN apt-get update && apt-get install netcat-openbsd -y 






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Jul 25 '16 at 15:49

























                        answered Jul 25 '16 at 10:21









                        Martijn Jacobs

                        19827




                        19827




















                            up vote
                            5
                            down vote













                            For container start ordering use



                            depends_on:


                            For waiting previous container start use script



                            entrypoint: ./wait-for-it.sh db:5432


                            This article will help you
                            https://docs.docker.com/compose/startup-order/






                            share|improve this answer




















                            • stackoverflow.com/a/35476843/1584115
                              – svenhornberg
                              Nov 24 '16 at 9:43






                            • 4




                              @svenhornberg in the comment, you link, there is no explanation about wait-for-it.sh feature.
                              – quit
                              Dec 2 '16 at 19:13














                            up vote
                            5
                            down vote













                            For container start ordering use



                            depends_on:


                            For waiting previous container start use script



                            entrypoint: ./wait-for-it.sh db:5432


                            This article will help you
                            https://docs.docker.com/compose/startup-order/






                            share|improve this answer




















                            • stackoverflow.com/a/35476843/1584115
                              – svenhornberg
                              Nov 24 '16 at 9:43






                            • 4




                              @svenhornberg in the comment, you link, there is no explanation about wait-for-it.sh feature.
                              – quit
                              Dec 2 '16 at 19:13












                            up vote
                            5
                            down vote










                            up vote
                            5
                            down vote









                            For container start ordering use



                            depends_on:


                            For waiting previous container start use script



                            entrypoint: ./wait-for-it.sh db:5432


                            This article will help you
                            https://docs.docker.com/compose/startup-order/






                            share|improve this answer












                            For container start ordering use



                            depends_on:


                            For waiting previous container start use script



                            entrypoint: ./wait-for-it.sh db:5432


                            This article will help you
                            https://docs.docker.com/compose/startup-order/







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Oct 4 '16 at 12:12









                            quit

                            12216




                            12216











                            • stackoverflow.com/a/35476843/1584115
                              – svenhornberg
                              Nov 24 '16 at 9:43






                            • 4




                              @svenhornberg in the comment, you link, there is no explanation about wait-for-it.sh feature.
                              – quit
                              Dec 2 '16 at 19:13
















                            • stackoverflow.com/a/35476843/1584115
                              – svenhornberg
                              Nov 24 '16 at 9:43






                            • 4




                              @svenhornberg in the comment, you link, there is no explanation about wait-for-it.sh feature.
                              – quit
                              Dec 2 '16 at 19:13















                            stackoverflow.com/a/35476843/1584115
                            – svenhornberg
                            Nov 24 '16 at 9:43




                            stackoverflow.com/a/35476843/1584115
                            – svenhornberg
                            Nov 24 '16 at 9:43




                            4




                            4




                            @svenhornberg in the comment, you link, there is no explanation about wait-for-it.sh feature.
                            – quit
                            Dec 2 '16 at 19:13




                            @svenhornberg in the comment, you link, there is no explanation about wait-for-it.sh feature.
                            – quit
                            Dec 2 '16 at 19:13










                            up vote
                            5
                            down vote













                            restart: on-failure
                            did the trick for me..see below



                            ---
                            version: '2.1'
                            services:
                            consumer:
                            image: golang:alpine
                            volumes:
                            - ./:/go/src/srv-consumer
                            working_dir: /go/src/srv-consumer
                            environment:
                            AMQP_DSN: "amqp://guest:guest@rabbitmq:5672"
                            command: go run cmd/main.go
                            links:
                            - rabbitmq
                            restart: on-failure

                            rabbitmq:
                            image: rabbitmq:3.7-management-alpine
                            ports:
                            - "15672:15672"
                            - "5672:5672"





                            share|improve this answer
























                              up vote
                              5
                              down vote













                              restart: on-failure
                              did the trick for me..see below



                              ---
                              version: '2.1'
                              services:
                              consumer:
                              image: golang:alpine
                              volumes:
                              - ./:/go/src/srv-consumer
                              working_dir: /go/src/srv-consumer
                              environment:
                              AMQP_DSN: "amqp://guest:guest@rabbitmq:5672"
                              command: go run cmd/main.go
                              links:
                              - rabbitmq
                              restart: on-failure

                              rabbitmq:
                              image: rabbitmq:3.7-management-alpine
                              ports:
                              - "15672:15672"
                              - "5672:5672"





                              share|improve this answer






















                                up vote
                                5
                                down vote










                                up vote
                                5
                                down vote









                                restart: on-failure
                                did the trick for me..see below



                                ---
                                version: '2.1'
                                services:
                                consumer:
                                image: golang:alpine
                                volumes:
                                - ./:/go/src/srv-consumer
                                working_dir: /go/src/srv-consumer
                                environment:
                                AMQP_DSN: "amqp://guest:guest@rabbitmq:5672"
                                command: go run cmd/main.go
                                links:
                                - rabbitmq
                                restart: on-failure

                                rabbitmq:
                                image: rabbitmq:3.7-management-alpine
                                ports:
                                - "15672:15672"
                                - "5672:5672"





                                share|improve this answer












                                restart: on-failure
                                did the trick for me..see below



                                ---
                                version: '2.1'
                                services:
                                consumer:
                                image: golang:alpine
                                volumes:
                                - ./:/go/src/srv-consumer
                                working_dir: /go/src/srv-consumer
                                environment:
                                AMQP_DSN: "amqp://guest:guest@rabbitmq:5672"
                                command: go run cmd/main.go
                                links:
                                - rabbitmq
                                restart: on-failure

                                rabbitmq:
                                image: rabbitmq:3.7-management-alpine
                                ports:
                                - "15672:15672"
                                - "5672:5672"






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Mar 17 at 21:55









                                Edwin Ikechukwu

                                1,8141923




                                1,8141923




















                                    up vote
                                    4
                                    down vote













                                    There is a ready to use utility called "docker-wait" that can be used for waiting.






                                    share|improve this answer
















                                    • 1




                                      Thank you, but it is only a shell script so it is like h3nrik answer or waiting inside python. It is not a feature of docker-compose itself. May you have a look in github.com/docker/compose/issues/374 they plan to implement a healthcheck which would be the best way. 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.
                                      – svenhornberg
                                      Nov 11 '15 at 14:48















                                    up vote
                                    4
                                    down vote













                                    There is a ready to use utility called "docker-wait" that can be used for waiting.






                                    share|improve this answer
















                                    • 1




                                      Thank you, but it is only a shell script so it is like h3nrik answer or waiting inside python. It is not a feature of docker-compose itself. May you have a look in github.com/docker/compose/issues/374 they plan to implement a healthcheck which would be the best way. 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.
                                      – svenhornberg
                                      Nov 11 '15 at 14:48













                                    up vote
                                    4
                                    down vote










                                    up vote
                                    4
                                    down vote









                                    There is a ready to use utility called "docker-wait" that can be used for waiting.






                                    share|improve this answer












                                    There is a ready to use utility called "docker-wait" that can be used for waiting.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 11 '15 at 14:41









                                    Adrian Mitev

                                    3,77632351




                                    3,77632351







                                    • 1




                                      Thank you, but it is only a shell script so it is like h3nrik answer or waiting inside python. It is not a feature of docker-compose itself. May you have a look in github.com/docker/compose/issues/374 they plan to implement a healthcheck which would be the best way. 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.
                                      – svenhornberg
                                      Nov 11 '15 at 14:48













                                    • 1




                                      Thank you, but it is only a shell script so it is like h3nrik answer or waiting inside python. It is not a feature of docker-compose itself. May you have a look in github.com/docker/compose/issues/374 they plan to implement a healthcheck which would be the best way. 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.
                                      – svenhornberg
                                      Nov 11 '15 at 14:48








                                    1




                                    1




                                    Thank you, but it is only a shell script so it is like h3nrik answer or waiting inside python. It is not a feature of docker-compose itself. May you have a look in github.com/docker/compose/issues/374 they plan to implement a healthcheck which would be the best way. 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.
                                    – svenhornberg
                                    Nov 11 '15 at 14:48





                                    Thank you, but it is only a shell script so it is like h3nrik answer or waiting inside python. It is not a feature of docker-compose itself. May you have a look in github.com/docker/compose/issues/374 they plan to implement a healthcheck which would be the best way. 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.
                                    – svenhornberg
                                    Nov 11 '15 at 14:48











                                    up vote
                                    1
                                    down vote













                                    basing on this blog post https://8thlight.com/blog/dariusz-pasciak/2016/10/17/docker-compose-wait-for-dependencies.html



                                    I configured my docker-compose.yml as shown below:



                                    version: "3.1"

                                    services:
                                    rabbitmq:
                                    image: rabbitmq:3.7.2-management-alpine
                                    restart: always
                                    environment:
                                    RABBITMQ_HIPE_COMPILE: 1
                                    RABBITMQ_MANAGEMENT: 1
                                    RABBITMQ_VM_MEMORY_HIGH_WATERMARK: 0.2
                                    RABBITMQ_DEFAULT_USER: "rabbitmq"
                                    RABBITMQ_DEFAULT_PASS: "rabbitmq"
                                    ports:
                                    - "15672:15672"
                                    - "5672:5672"
                                    volumes:
                                    - data:/var/lib/rabbitmq:rw

                                    start_dependencies:
                                    image: alpine:latest
                                    links:
                                    - rabbitmq
                                    command: >
                                    /bin/sh -c "
                                    echo Waiting for rabbitmq service start...;
                                    while ! nc -z rabbitmq 5672;
                                    do
                                    sleep 1;
                                    done;
                                    echo Connected!;
                                    "

                                    volumes:
                                    data:


                                    Then I do for run =>:



                                    docker-compose up start_dependencies



                                    rabbitmq service will start in daemon mode, start_dependencies will finish the work.






                                    share|improve this answer




















                                    • lol, making query via "curl", "-f", "http://localhost:15672" for which u need to install management plugin and using healthcheck that already deprecated - its best answer. Simple working example with check via nc its - downvote. ha, ok...
                                      – Igor Komar
                                      Jan 12 at 1:26











                                    • the answer does not use a native docker feature, its irrelevant if you use curl, nc or other tools. while! nc is the same as already posted in other answers.
                                      – svenhornberg
                                      Jan 12 at 8:09






                                    • 1




                                      native docker features: 1. docs.docker.com/compose/startup-order 2. github.com/docker/compose/issues/5007 3. github.com/docker/compose/issues/374
                                      – Igor Komar
                                      Jan 12 at 9:00











                                    • i will upvote your link
                                      – svenhornberg
                                      Jan 12 at 9:34










                                    • @IgorKomar, thanks man, you saved my day! :3 I used almost same mechanic to check mysql server is ready before actual application is started. ;) I am passing similar command to the docker-compose run --name app-test --rm "app" bash -l -c 'echo Waiting for mysql service start... && while ! nc -z db-server 3306; do sleep 1; done && echo Connected! && /bin/bash /script/ci_tests.sh'
                                      – TooroSan
                                      Oct 11 at 0:49















                                    up vote
                                    1
                                    down vote













                                    basing on this blog post https://8thlight.com/blog/dariusz-pasciak/2016/10/17/docker-compose-wait-for-dependencies.html



                                    I configured my docker-compose.yml as shown below:



                                    version: "3.1"

                                    services:
                                    rabbitmq:
                                    image: rabbitmq:3.7.2-management-alpine
                                    restart: always
                                    environment:
                                    RABBITMQ_HIPE_COMPILE: 1
                                    RABBITMQ_MANAGEMENT: 1
                                    RABBITMQ_VM_MEMORY_HIGH_WATERMARK: 0.2
                                    RABBITMQ_DEFAULT_USER: "rabbitmq"
                                    RABBITMQ_DEFAULT_PASS: "rabbitmq"
                                    ports:
                                    - "15672:15672"
                                    - "5672:5672"
                                    volumes:
                                    - data:/var/lib/rabbitmq:rw

                                    start_dependencies:
                                    image: alpine:latest
                                    links:
                                    - rabbitmq
                                    command: >
                                    /bin/sh -c "
                                    echo Waiting for rabbitmq service start...;
                                    while ! nc -z rabbitmq 5672;
                                    do
                                    sleep 1;
                                    done;
                                    echo Connected!;
                                    "

                                    volumes:
                                    data:


                                    Then I do for run =>:



                                    docker-compose up start_dependencies



                                    rabbitmq service will start in daemon mode, start_dependencies will finish the work.






                                    share|improve this answer




















                                    • lol, making query via "curl", "-f", "http://localhost:15672" for which u need to install management plugin and using healthcheck that already deprecated - its best answer. Simple working example with check via nc its - downvote. ha, ok...
                                      – Igor Komar
                                      Jan 12 at 1:26











                                    • the answer does not use a native docker feature, its irrelevant if you use curl, nc or other tools. while! nc is the same as already posted in other answers.
                                      – svenhornberg
                                      Jan 12 at 8:09






                                    • 1




                                      native docker features: 1. docs.docker.com/compose/startup-order 2. github.com/docker/compose/issues/5007 3. github.com/docker/compose/issues/374
                                      – Igor Komar
                                      Jan 12 at 9:00











                                    • i will upvote your link
                                      – svenhornberg
                                      Jan 12 at 9:34










                                    • @IgorKomar, thanks man, you saved my day! :3 I used almost same mechanic to check mysql server is ready before actual application is started. ;) I am passing similar command to the docker-compose run --name app-test --rm "app" bash -l -c 'echo Waiting for mysql service start... && while ! nc -z db-server 3306; do sleep 1; done && echo Connected! && /bin/bash /script/ci_tests.sh'
                                      – TooroSan
                                      Oct 11 at 0:49













                                    up vote
                                    1
                                    down vote










                                    up vote
                                    1
                                    down vote









                                    basing on this blog post https://8thlight.com/blog/dariusz-pasciak/2016/10/17/docker-compose-wait-for-dependencies.html



                                    I configured my docker-compose.yml as shown below:



                                    version: "3.1"

                                    services:
                                    rabbitmq:
                                    image: rabbitmq:3.7.2-management-alpine
                                    restart: always
                                    environment:
                                    RABBITMQ_HIPE_COMPILE: 1
                                    RABBITMQ_MANAGEMENT: 1
                                    RABBITMQ_VM_MEMORY_HIGH_WATERMARK: 0.2
                                    RABBITMQ_DEFAULT_USER: "rabbitmq"
                                    RABBITMQ_DEFAULT_PASS: "rabbitmq"
                                    ports:
                                    - "15672:15672"
                                    - "5672:5672"
                                    volumes:
                                    - data:/var/lib/rabbitmq:rw

                                    start_dependencies:
                                    image: alpine:latest
                                    links:
                                    - rabbitmq
                                    command: >
                                    /bin/sh -c "
                                    echo Waiting for rabbitmq service start...;
                                    while ! nc -z rabbitmq 5672;
                                    do
                                    sleep 1;
                                    done;
                                    echo Connected!;
                                    "

                                    volumes:
                                    data:


                                    Then I do for run =>:



                                    docker-compose up start_dependencies



                                    rabbitmq service will start in daemon mode, start_dependencies will finish the work.






                                    share|improve this answer












                                    basing on this blog post https://8thlight.com/blog/dariusz-pasciak/2016/10/17/docker-compose-wait-for-dependencies.html



                                    I configured my docker-compose.yml as shown below:



                                    version: "3.1"

                                    services:
                                    rabbitmq:
                                    image: rabbitmq:3.7.2-management-alpine
                                    restart: always
                                    environment:
                                    RABBITMQ_HIPE_COMPILE: 1
                                    RABBITMQ_MANAGEMENT: 1
                                    RABBITMQ_VM_MEMORY_HIGH_WATERMARK: 0.2
                                    RABBITMQ_DEFAULT_USER: "rabbitmq"
                                    RABBITMQ_DEFAULT_PASS: "rabbitmq"
                                    ports:
                                    - "15672:15672"
                                    - "5672:5672"
                                    volumes:
                                    - data:/var/lib/rabbitmq:rw

                                    start_dependencies:
                                    image: alpine:latest
                                    links:
                                    - rabbitmq
                                    command: >
                                    /bin/sh -c "
                                    echo Waiting for rabbitmq service start...;
                                    while ! nc -z rabbitmq 5672;
                                    do
                                    sleep 1;
                                    done;
                                    echo Connected!;
                                    "

                                    volumes:
                                    data:


                                    Then I do for run =>:



                                    docker-compose up start_dependencies



                                    rabbitmq service will start in daemon mode, start_dependencies will finish the work.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jan 11 at 20:22









                                    Igor Komar

                                    54210




                                    54210











                                    • lol, making query via "curl", "-f", "http://localhost:15672" for which u need to install management plugin and using healthcheck that already deprecated - its best answer. Simple working example with check via nc its - downvote. ha, ok...
                                      – Igor Komar
                                      Jan 12 at 1:26











                                    • the answer does not use a native docker feature, its irrelevant if you use curl, nc or other tools. while! nc is the same as already posted in other answers.
                                      – svenhornberg
                                      Jan 12 at 8:09






                                    • 1




                                      native docker features: 1. docs.docker.com/compose/startup-order 2. github.com/docker/compose/issues/5007 3. github.com/docker/compose/issues/374
                                      – Igor Komar
                                      Jan 12 at 9:00











                                    • i will upvote your link
                                      – svenhornberg
                                      Jan 12 at 9:34










                                    • @IgorKomar, thanks man, you saved my day! :3 I used almost same mechanic to check mysql server is ready before actual application is started. ;) I am passing similar command to the docker-compose run --name app-test --rm "app" bash -l -c 'echo Waiting for mysql service start... && while ! nc -z db-server 3306; do sleep 1; done && echo Connected! && /bin/bash /script/ci_tests.sh'
                                      – TooroSan
                                      Oct 11 at 0:49

















                                    • lol, making query via "curl", "-f", "http://localhost:15672" for which u need to install management plugin and using healthcheck that already deprecated - its best answer. Simple working example with check via nc its - downvote. ha, ok...
                                      – Igor Komar
                                      Jan 12 at 1:26











                                    • the answer does not use a native docker feature, its irrelevant if you use curl, nc or other tools. while! nc is the same as already posted in other answers.
                                      – svenhornberg
                                      Jan 12 at 8:09






                                    • 1




                                      native docker features: 1. docs.docker.com/compose/startup-order 2. github.com/docker/compose/issues/5007 3. github.com/docker/compose/issues/374
                                      – Igor Komar
                                      Jan 12 at 9:00











                                    • i will upvote your link
                                      – svenhornberg
                                      Jan 12 at 9:34










                                    • @IgorKomar, thanks man, you saved my day! :3 I used almost same mechanic to check mysql server is ready before actual application is started. ;) I am passing similar command to the docker-compose run --name app-test --rm "app" bash -l -c 'echo Waiting for mysql service start... && while ! nc -z db-server 3306; do sleep 1; done && echo Connected! && /bin/bash /script/ci_tests.sh'
                                      – TooroSan
                                      Oct 11 at 0:49
















                                    lol, making query via "curl", "-f", "http://localhost:15672" for which u need to install management plugin and using healthcheck that already deprecated - its best answer. Simple working example with check via nc its - downvote. ha, ok...
                                    – Igor Komar
                                    Jan 12 at 1:26





                                    lol, making query via "curl", "-f", "http://localhost:15672" for which u need to install management plugin and using healthcheck that already deprecated - its best answer. Simple working example with check via nc its - downvote. ha, ok...
                                    – Igor Komar
                                    Jan 12 at 1:26













                                    the answer does not use a native docker feature, its irrelevant if you use curl, nc or other tools. while! nc is the same as already posted in other answers.
                                    – svenhornberg
                                    Jan 12 at 8:09




                                    the answer does not use a native docker feature, its irrelevant if you use curl, nc or other tools. while! nc is the same as already posted in other answers.
                                    – svenhornberg
                                    Jan 12 at 8:09




                                    1




                                    1




                                    native docker features: 1. docs.docker.com/compose/startup-order 2. github.com/docker/compose/issues/5007 3. github.com/docker/compose/issues/374
                                    – Igor Komar
                                    Jan 12 at 9:00





                                    native docker features: 1. docs.docker.com/compose/startup-order 2. github.com/docker/compose/issues/5007 3. github.com/docker/compose/issues/374
                                    – Igor Komar
                                    Jan 12 at 9:00













                                    i will upvote your link
                                    – svenhornberg
                                    Jan 12 at 9:34




                                    i will upvote your link
                                    – svenhornberg
                                    Jan 12 at 9:34












                                    @IgorKomar, thanks man, you saved my day! :3 I used almost same mechanic to check mysql server is ready before actual application is started. ;) I am passing similar command to the docker-compose run --name app-test --rm "app" bash -l -c 'echo Waiting for mysql service start... && while ! nc -z db-server 3306; do sleep 1; done && echo Connected! && /bin/bash /script/ci_tests.sh'
                                    – TooroSan
                                    Oct 11 at 0:49





                                    @IgorKomar, thanks man, you saved my day! :3 I used almost same mechanic to check mysql server is ready before actual application is started. ;) I am passing similar command to the docker-compose run --name app-test --rm "app" bash -l -c 'echo Waiting for mysql service start... && while ! nc -z db-server 3306; do sleep 1; done && echo Connected! && /bin/bash /script/ci_tests.sh'
                                    – TooroSan
                                    Oct 11 at 0:49











                                    up vote
                                    0
                                    down vote













                                    One of the alternative solution is to use a container orchestration solution like Kubernetes. Kubernetes has support for init containers which run to completion before other containers can start. You can find an example here with SQL Server 2017 Linux container where API container uses init container to initialise a database



                                    https://www.handsonarchitect.com/2018/08/understand-kubernetes-object-init.html






                                    share|improve this answer
























                                      up vote
                                      0
                                      down vote













                                      One of the alternative solution is to use a container orchestration solution like Kubernetes. Kubernetes has support for init containers which run to completion before other containers can start. You can find an example here with SQL Server 2017 Linux container where API container uses init container to initialise a database



                                      https://www.handsonarchitect.com/2018/08/understand-kubernetes-object-init.html






                                      share|improve this answer






















                                        up vote
                                        0
                                        down vote










                                        up vote
                                        0
                                        down vote









                                        One of the alternative solution is to use a container orchestration solution like Kubernetes. Kubernetes has support for init containers which run to completion before other containers can start. You can find an example here with SQL Server 2017 Linux container where API container uses init container to initialise a database



                                        https://www.handsonarchitect.com/2018/08/understand-kubernetes-object-init.html






                                        share|improve this answer












                                        One of the alternative solution is to use a container orchestration solution like Kubernetes. Kubernetes has support for init containers which run to completion before other containers can start. You can find an example here with SQL Server 2017 Linux container where API container uses init container to initialise a database



                                        https://www.handsonarchitect.com/2018/08/understand-kubernetes-object-init.html







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 10 at 3:34









                                        Nilesh Gule

                                        1,224911




                                        1,224911




















                                            up vote
                                            0
                                            down vote













                                            I couldn't use this approach, I actually can't use volume in docker-compose, in both cases I didn't work:




                                            s-flyway_1_d7a78571d25c | env: can't execute 'bash': No such file or
                                            directory docker_s-flyway_1_d7a78571d25c exited with code 127




                                            What am I missing here ? I'm a little lost about it right now.....




                                            Project link:
                                            https://github.com/jbrasileiro/introducing-example/blob/master/introducing-example-docker-flyway/src/docker/docker-compose.yml




                                            Dockerfile



                                            FROM dhoer/flyway:alpine
                                            COPY ./sql/*.sql /flyway/sql
                                            VOLUME ./wait-for-it.sh:/usr/local/bin/wait-for-it.sh
                                            ADD ./wait-for-it.sh /
                                            RUN chmod 777 /wait-for-it.sh


                                            docker version : '3'



                                            version: '3'
                                            services:
                                            hello-world:
                                            image: ubuntu
                                            volumes:
                                            - ./wait-for-it.sh:/usr/local/bin/wait-for-it.sh
                                            command:
                                            - wait-for-it.sh
                                            - github.com:80
                                            - --
                                            - echo
                                            - "hello world"


                                            docker version : '2'



                                            version: '2'
                                            services:
                                            s-postgres:
                                            image: postgres:9.4-alpine
                                            hostname: host-postgres
                                            environment:
                                            - POSTGRES_USER=postgres
                                            - POSTGRES_PASSWORD=postgres
                                            - POSTGRES_DB=postgres
                                            networks:
                                            - network-docker
                                            s-flyway:
                                            image: dhoer/flyway:alpine
                                            build: flyway/
                                            entrypoint: /wait-for-it.sh s-postgres 5432 --timeout=0 --strict
                                            command: -url=jdbc:postgresql://s-postgres:5432/postgres -baselineOnMigrate=true -schemas=public -user=postgres -password=postgres migrate
                                            networks:
                                            - network-docker
                                            depends_on:
                                            - s-postgres
                                            networks:
                                            network-docker:
                                            driver: bridge





                                            share|improve this answer
























                                              up vote
                                              0
                                              down vote













                                              I couldn't use this approach, I actually can't use volume in docker-compose, in both cases I didn't work:




                                              s-flyway_1_d7a78571d25c | env: can't execute 'bash': No such file or
                                              directory docker_s-flyway_1_d7a78571d25c exited with code 127




                                              What am I missing here ? I'm a little lost about it right now.....




                                              Project link:
                                              https://github.com/jbrasileiro/introducing-example/blob/master/introducing-example-docker-flyway/src/docker/docker-compose.yml




                                              Dockerfile



                                              FROM dhoer/flyway:alpine
                                              COPY ./sql/*.sql /flyway/sql
                                              VOLUME ./wait-for-it.sh:/usr/local/bin/wait-for-it.sh
                                              ADD ./wait-for-it.sh /
                                              RUN chmod 777 /wait-for-it.sh


                                              docker version : '3'



                                              version: '3'
                                              services:
                                              hello-world:
                                              image: ubuntu
                                              volumes:
                                              - ./wait-for-it.sh:/usr/local/bin/wait-for-it.sh
                                              command:
                                              - wait-for-it.sh
                                              - github.com:80
                                              - --
                                              - echo
                                              - "hello world"


                                              docker version : '2'



                                              version: '2'
                                              services:
                                              s-postgres:
                                              image: postgres:9.4-alpine
                                              hostname: host-postgres
                                              environment:
                                              - POSTGRES_USER=postgres
                                              - POSTGRES_PASSWORD=postgres
                                              - POSTGRES_DB=postgres
                                              networks:
                                              - network-docker
                                              s-flyway:
                                              image: dhoer/flyway:alpine
                                              build: flyway/
                                              entrypoint: /wait-for-it.sh s-postgres 5432 --timeout=0 --strict
                                              command: -url=jdbc:postgresql://s-postgres:5432/postgres -baselineOnMigrate=true -schemas=public -user=postgres -password=postgres migrate
                                              networks:
                                              - network-docker
                                              depends_on:
                                              - s-postgres
                                              networks:
                                              network-docker:
                                              driver: bridge





                                              share|improve this answer






















                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote









                                                I couldn't use this approach, I actually can't use volume in docker-compose, in both cases I didn't work:




                                                s-flyway_1_d7a78571d25c | env: can't execute 'bash': No such file or
                                                directory docker_s-flyway_1_d7a78571d25c exited with code 127




                                                What am I missing here ? I'm a little lost about it right now.....




                                                Project link:
                                                https://github.com/jbrasileiro/introducing-example/blob/master/introducing-example-docker-flyway/src/docker/docker-compose.yml




                                                Dockerfile



                                                FROM dhoer/flyway:alpine
                                                COPY ./sql/*.sql /flyway/sql
                                                VOLUME ./wait-for-it.sh:/usr/local/bin/wait-for-it.sh
                                                ADD ./wait-for-it.sh /
                                                RUN chmod 777 /wait-for-it.sh


                                                docker version : '3'



                                                version: '3'
                                                services:
                                                hello-world:
                                                image: ubuntu
                                                volumes:
                                                - ./wait-for-it.sh:/usr/local/bin/wait-for-it.sh
                                                command:
                                                - wait-for-it.sh
                                                - github.com:80
                                                - --
                                                - echo
                                                - "hello world"


                                                docker version : '2'



                                                version: '2'
                                                services:
                                                s-postgres:
                                                image: postgres:9.4-alpine
                                                hostname: host-postgres
                                                environment:
                                                - POSTGRES_USER=postgres
                                                - POSTGRES_PASSWORD=postgres
                                                - POSTGRES_DB=postgres
                                                networks:
                                                - network-docker
                                                s-flyway:
                                                image: dhoer/flyway:alpine
                                                build: flyway/
                                                entrypoint: /wait-for-it.sh s-postgres 5432 --timeout=0 --strict
                                                command: -url=jdbc:postgresql://s-postgres:5432/postgres -baselineOnMigrate=true -schemas=public -user=postgres -password=postgres migrate
                                                networks:
                                                - network-docker
                                                depends_on:
                                                - s-postgres
                                                networks:
                                                network-docker:
                                                driver: bridge





                                                share|improve this answer












                                                I couldn't use this approach, I actually can't use volume in docker-compose, in both cases I didn't work:




                                                s-flyway_1_d7a78571d25c | env: can't execute 'bash': No such file or
                                                directory docker_s-flyway_1_d7a78571d25c exited with code 127




                                                What am I missing here ? I'm a little lost about it right now.....




                                                Project link:
                                                https://github.com/jbrasileiro/introducing-example/blob/master/introducing-example-docker-flyway/src/docker/docker-compose.yml




                                                Dockerfile



                                                FROM dhoer/flyway:alpine
                                                COPY ./sql/*.sql /flyway/sql
                                                VOLUME ./wait-for-it.sh:/usr/local/bin/wait-for-it.sh
                                                ADD ./wait-for-it.sh /
                                                RUN chmod 777 /wait-for-it.sh


                                                docker version : '3'



                                                version: '3'
                                                services:
                                                hello-world:
                                                image: ubuntu
                                                volumes:
                                                - ./wait-for-it.sh:/usr/local/bin/wait-for-it.sh
                                                command:
                                                - wait-for-it.sh
                                                - github.com:80
                                                - --
                                                - echo
                                                - "hello world"


                                                docker version : '2'



                                                version: '2'
                                                services:
                                                s-postgres:
                                                image: postgres:9.4-alpine
                                                hostname: host-postgres
                                                environment:
                                                - POSTGRES_USER=postgres
                                                - POSTGRES_PASSWORD=postgres
                                                - POSTGRES_DB=postgres
                                                networks:
                                                - network-docker
                                                s-flyway:
                                                image: dhoer/flyway:alpine
                                                build: flyway/
                                                entrypoint: /wait-for-it.sh s-postgres 5432 --timeout=0 --strict
                                                command: -url=jdbc:postgresql://s-postgres:5432/postgres -baselineOnMigrate=true -schemas=public -user=postgres -password=postgres migrate
                                                networks:
                                                - network-docker
                                                depends_on:
                                                - s-postgres
                                                networks:
                                                network-docker:
                                                driver: bridge






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Nov 23 at 21:37









                                                João Carlos Brasileiro

                                                13618




                                                13618



























                                                    draft saved

                                                    draft discarded
















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid


                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.

                                                    To learn more, see our tips on writing great answers.





                                                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                    Please pay close attention to the following guidance:


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid


                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.

                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f31746182%2fdocker-compose-wait-for-container-x-before-starting-y%23new-answer', 'question_page');

                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                                                    Syphilis

                                                    Darth Vader #20