threxx
threxx

Reputation: 1235

Docker compose run multiple commands one after another

I recently started working with docker-compose and I am running into an issue where I won't get any helpful answers while googling. So what I want to do is running 2 commands on after another. First I want to train my model which places a trained model in a folder. The second command then runs the model. However, right now both commands start together and the image is loaded twice as well as the volume is created twice.

So my question is if it is possible to run multiple commands one after another, how does that work? I wonder as well, how my trained model is put into the volume docker-compose is running on? Can I somehow set a path to that volume as an output?

My docker-compose file so far:

version: '3.3'
networks: {rasa-network: {}}
services:
  rasa:
    image: rasa/rasa:latest-full
    ports:
      - "5005:5005"
    volumes:
      - ./rasa/:/app/
    command:  run -vv -m models/test_model/ --enable-api --endpoints endpoints.yml --credentials credentials.yml
    networks: ['rasa-network']
    depends_on:
      - training
      - duckling
  duckling:
    image: rasa/duckling:latest
    ports:
      - "8000:8000"
    networks: ['rasa-network']
  training:
    build: .
    image: rasa/rasa:latest-full
    command: train --data data/ -c config.yml -d domain.yml --out models/test_model
    volumes:
      - ./rasa/:/app/

Upvotes: 1

Views: 5885

Answers (1)

Adam
Adam

Reputation: 807

According to the documentation of depends_on, Docker compose is not able to determine the readiness of a container, so as soon as the dependencies have started, the last container will start, ignoring if the other ones are ready or not.

The workaround you could do is to do a wrapper shell script that controls that the dependencies (duckling and training) have finished doing their stuff before starting rasa. This means, if rasa needs some files from the other two containers, you can create an script to check if these files exist with a loop. If so, exit the loop and run the command you have. Otherwise, sleep some seconds and retry.

Then, the rasa command would execute only this script, for example:

command: ["./wait-for-dependencies.sh", "duckling", "training"]

You can have a look here: https://docs.docker.com/compose/startup-order/, they have made some examples for a similar use-case.

Upvotes: 1

Related Questions