简体   繁体   中英

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

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.

You are confusing internal and external ports. Database is visible on port 3306 inside your network, so you have to wait on db:3306 and not on 33061 .

Port exposing has no effect inside user-defined bridge network, created by default by docker-compose . All ports are visible to containers inside network by default. When you expose port, you make it visible outside network.

Also, make sure what is ENTRYPOINT for image prismagraphql/prisma:1.25 . If it is not /bin/sh -c or other type of shell, your command wont get executed.

UPD

If you get ENTRYPOINT in base image different from /bin/sh -c , you can override it. Supposing you have /bin/sh -c /app/start.sh , you could do following magic:

docker-compose.yml

...
services:
  prisma:
    entrypoint: ["/bin/sh", "-c", "'./wait-for-it.sh db:3306 && /app/start.sh'"] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM