2

I want to run 2 different commands for my service from docker-compose.

  1. bash script.sh
  2. config /etc/config.yaml

Currently, my docker-compose looks like the below. I want the bash script to run after the config command

docker-compose.yaml:

services:
   API:
        build: .
        ports:
            - 8080:8080
        environment:
         - "USER=${USER}"
         - "PASSWORD=${PASSWORD}"
        volumes:
            - ./conf/config.yaml:/etc/api.yaml
        command: ["-config", "/etc/api.yaml"]
5
  • Write a bash script containing those two commands, add it to the image, and run that script instead. Commented Apr 18, 2022 at 19:58
  • I am not sure how that -config /etc/api.yaml works and how to add it to the script. How can I mention that config command inside a script Commented Apr 18, 2022 at 20:06
  • -config looks like an argument to some other command. What's in the image's Dockerfile; does it have an ENTRYPOINT declaration, to which this command: is arguments? Commented Apr 18, 2022 at 20:13
  • FROM xenial as api COPY --from=api-builder /app/go_source/bin/api /bin/api ENTRYPOINT ["/bin/api"] I guess its an argument for /bin/api. @DavidMaze Commented Apr 18, 2022 at 20:17
  • I do ACK that entrypoint is the best practice here, BUT, I would like to ask this precise question: is it IMPOSSIBLE to achieve this without an entrypoint script? Commented May 16, 2024 at 20:51

1 Answer 1

3

Write a new entrypoint script.

#!/bin/bash

/bin/api "$@"

# perhaps
source /script.sh

# or exec
exec /script.sh

Copy this into your image on build.

COPY --chmod=755 entrypoint.sh /
ENTRYPOINT ["/entrypoint.sh"]

This will result in the arguments you are passing in your compose file to be passed to /bin/api, and after that your script is executed.

I am not sure if that would be actually useful. The API command looks like it's initiating a long-running process, so your script might never really run.

You could also dome something like this in your entrypoint.

#!/bin/bash

run_script(){
  sleep 5
  source /script.sh
}

run_script &

exec /bin/api "$@"

It would hide errors from that script though, so its not really solid.

This would run the function in the background, which would sleep 5 seconds to give the API time to start, and then run the script while the API is running.

It's hard to say, without knowing what your script is doing, what would be a good solution. The second suggestion feels definitely a bit hacky.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.