Search code examples
dockerdocker-composestartup

Wait for a docker container to be ready with command in docker-compose.yml


I have a mysql-db and prisma image in my docker-compose.yml. I want prisma to wait for the db to be ready, cause otherwise prisma keeps restarting and it wont work at all. And I know from here, that I can use ./wait-for-it but I was not able to connect the pieces after searching for a while.

version: '3'
services:
  prisma:
    image: prismagraphql/prisma:1.25
    restart: unless-stopped
    ports:
    - "4001:4466"
    depends_on:
    - db

    # I added this command
    command: ["./wait-for-it.sh", "db:33061", "--"] 

    environment:
      PRISMA_CONFIG: |
        managementApiSecret: server.secret.123
        port: 4466
        databases:
          default:
            connector: mysql
            active: true
            host: db
            port: 3306
            user: ***
            password: ***
  db:
    image: mysql:5.7
    restart: unless-stopped
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_USER: ***
      MYSQL_ROOT_PASSWORD: ***
    ports:
      - "33061:3306"
    volumes:
      - /docker/mysql:/var/lib/mysql

I added the command above but nothing changed, not even an error in the logs but as I understand, the command is run inside the container.

  1. How do I get the ./wait-for-it.sh into the container?
  2. And can this even work this way with the command or does this depend on the prisma-image?
  3. Otherwise, how would I achieve the waiting?

I just have the docker-compose file and want to do docker-compose up -d


Solution

  • Now I found out how to include wait-for-it.sh into the container.

    I downloaded the wait-for-it.sh into the project folder and then I created a file called Dockerfile with the contents:

    FROM prismagraphql/prisma:1.25
    COPY ./wait-for-it.sh /app/wait-for-it.sh
    RUN chmod +x /app/wait-for-it.sh
    ENTRYPOINT ["/bin/sh","-c","/app/wait-for-it.sh db:3306 -t 30 -- /app/start.sh"]
    

    In my docker-compose.yml I replaced image: prismagraphql/prisma:1.25 with build: . which causes a new build from the Dockerfile in my project path.

    Now the new image will be built from the prisma image and the wait-for-it.sh will be copied into the new image. Then the ENTRYPOINT is overridden and prisma will wait until the db is ready.