vishnubob / wait-for-it

Pure bash script to test and wait on the availability of a TCP host and port
MIT License
9.39k stars 2.27k forks source link

Use within dockerfile before running another command against now running app #101

Open rachael-ross opened 4 years ago

rachael-ross commented 4 years ago

I have a scenario where I want to start a server within a container and then, once it's up and running, hit it with a curl command to further configure it. Does anyone have an example of something like this?

gioamato commented 4 years ago

Add wait-for-it to the container and use a docker entrypoint:

# DOCKERFILE
FROM <image:tag>

# Add 'wait-for-it' to check upstream availability
COPY wait-for-it.sh /usr/local/bin/wait-for-it
RUN chmod +x /usr/local/bin/wait-for-it

# Add entrypoint script
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/docker-entrypoint

ENTRYPOINT ["docker-entrypoint"]
CMD []

Then you can do your things inside the entrypoint script:

# ENTRYPOINT
#!/bin/bash
set -euo pipefail

# eventually start your server

# wait upstream server availability
wait-for-it <server>:<port> --timeout=300 --strict

# do your things
curl <server>:<port>

# eventually keep your container running
exec tail -f /dev/null

This works but it is not optimal. You should design your container to run just one process, in the foreground. Everything depends on your process, so it's hard to guess without knowing more about your use case. You could think to start the server and do your things in the entrypoint and then restart it and keep it running with a CMD line at the end of Dockerfile or entrypoint.

luizsla commented 3 years ago

Thanks for the tip @gioamato, it helped a lot!

akauppi commented 2 years ago

I do this with Docker Compose, by depends_on to the service(s) to wait for.

services:
  warm-up:

  warmed-up:
    ...
    command: sh -c
      'wait-for-it warm-up:6768 --timeout=60
      '
    depends_on: ['warm-up']
    profiles: ['manual']
deiga commented 2 years ago

I do this with Docker Compose, by depends_on to the service(s) to wait for.

services:
  warm-up:

  warmed-up:
    ...
    command: sh -c
      'wait-for-it warm-up:6768 --timeout=60
      '
    depends_on: ['warm-up']
    profiles: ['manual']

This only works for any environment you use docker-compose with, if you use anything like Fargate or k8s then this is not an option