Home > Enterprise >  How to run 2 different commands from docker-compose command:
How to run 2 different commands from docker-compose command:

Time:04-20

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

CodePudding user response:

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.

  • Related