0

My docker-compose.yml looks something like this

version: '3.8'

services:

  database:
    image: "postgres:10.16"
    environment:
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=pg_database
    volumes:
      - database-data:/var/lib/postgresql/data/
    ports:
      - "35432:5432"

  api:
    build: ../../api
    environment:
      - POSTGRES_PASSWORD=password
      - POSTGRES_USER=user
      - POSTGRES_DB=pg_database
      - POSTGRES_HOST=database
      - POSTGRES_PORT=35432
    ports:
      - "3030:3030"
    depends_on:
      - "database"

  migrations:
    image: "node:14.17.5-alpine3.14"
    volumes:
      - "../migrations/scripts:/tmp"
      - "../../api:/tmp/api"
    links:
      - database
    command: "sh /tmp/migration_test.sh postgresql://user:password@host.docker.internal:35432/pg_database"
    depends_on:
      - "api"
      - "database"

volumes:
  database-data:

This works if I'm running one shell script in migration services. However, I want to be able to run 2 shell scripts. One right after the other. Not at the same time. Something like this

  migrations:
    image: "node:14.17.5-alpine3.14"
    volumes:
      - "../migrations/scripts:/tmp"
      - "../../api:/tmp/api"
    links:
      - database
    command: >
      "sh /tmp/migration_test.sh postgresql://user:password@host.docker.internal:35432/pg_database" &&
      "sh /tmp/migration_test2.sh postgresql://user:password@host.docker.internal:35432/pg_database"
    depends_on:
      - "api"
      - "database"

I'm not getting the right syntax to get this to work. I've tried different ways and can't figure it out. Does anyone have any suggestions? Is it possible to run two shell scripts one after the other?

C. Yee
  • 127
  • 2
  • 15
  • 1
    Does this answer your question? [Using Docker-Compose, how to execute multiple commands](https://stackoverflow.com/questions/30063907/using-docker-compose-how-to-execute-multiple-commands) – Hackerman Sep 16 '21 at 21:43
  • I followed the advice in that post and it didn't work for me. :( I'm thinking there's something wrong with my syntax - that I can't get right – C. Yee Sep 16 '21 at 21:51
  • Also consider that you can `docker-compose run api some_other_command` from the host, where `some_other_command` is built into the `api` image, and it will pick up the environment variables from the Compose setup. – David Maze Sep 16 '21 at 23:09
  • You can concatenate the commands under one `sh` prefix and add the `&&` operator between them so if the first succeeds the second one will fire – Noam Yizraeli Sep 17 '21 at 11:03
  • Sorry, I'm not following. I've tried something like this command: > sh " migration_test.sh && migration_test2.sh " and that doesn't work either :( – C. Yee Sep 17 '21 at 14:17

0 Answers0