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"]
4
  • Write a bash script containing those two commands, add it to the image, and run that script instead. 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
    – Raji M
    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?
    – David Maze
    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
    – Raji M
    Apr 18, 2022 at 20:17

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.