Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rachael-ross opened this issue Jun 17, 2020 · 4 comments

Comments

@rachael-ross
Copy link

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
Copy link

gioamato commented Jun 17, 2020

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
Copy link

luizsla commented Mar 19, 2021

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

@akauppi
Copy link

akauppi commented Mar 5, 2022

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
Copy link

deiga commented Mar 8, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants
@deiga @akauppi @gioamato @rachael-ross @luizsla and others