wait-for-it icon indicating copy to clipboard operation
wait-for-it copied to clipboard

Use within dockerfile before running another command against now running app

Open rachael-ross opened this issue 4 years ago • 4 comments

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?

rachael-ross avatar Jun 17 '20 20:06 rachael-ross

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.

gioamato avatar Jun 17 '20 21:06 gioamato

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

luizsla avatar Mar 19 '21 16:03 luizsla

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']

akauppi avatar Mar 05 '22 19:03 akauppi

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

deiga avatar Mar 08 '22 10:03 deiga