在创建其他服务之前,等待在docker compose中准备好mysql服务

问题描述 投票:0回答:2

我正在尝试在我的wait-for-it中使用docker-compose.yaml等待mysql准备好,然后再创建依赖它的服务。这是我的docker-compose.yaml

version: '3.5'

services:
  mysql:
    image: mysql:5.6
    ports:
      - "3307:3306"
    networks:
      - integration-tests
    environment:
      - MYSQL_DATABASE=mydb
      - MYSQL_USER=root
      - MYSQL_ROOT_PASSWORD=mypassword
    entrypoint: ./wait-for-it.sh mysql:3306
networks:
  integration-tests:
    name: integration-tests

尝试使用docker-compose运行时出现此错误:

启动integration-tests_mysql_1 ...错误

错误:for integration-tests_mysql_1无法启动服务mysql:OCI运行时创建失败:container_linux.go:348:启动容器进程导致“exec:\”./ wait-for-it.sh \“:stat ./wait-for- it.sh:没有这样的文件或目录“:未知

错误:对于mysql无法启动服务mysql:OCI运行时创建失败:container_linux.go:348:启动容器进程导致“exec:\”./ wait-for-it.sh \“:stat ./wait-for-it。 sh:没有这样的文件或目录“:unknown ERROR:在启动项目时遇到错误。

wait-for-it.sh脚本与我的docker-compose.yaml文件位于同一级别,因此我不明白为什么它没有找到。

docker docker-compose
2个回答
3
投票

您的问题是,您正在尝试执行不属于您图像的内容。你告诉docker从mysql创建一个容器:5.6,它不包含wait-for-it.sh,然后你告诉它通过启动wait-for-it.sh来启动容器。

我建议您创建自己的图像,其中包含以下内容:

#Dockerfile
FROM mysql:5.6

COPY wait-for-it.sh /wait-for-it.sh
RUN chmod +x /wait-for-it.sh

然后你将mysql:5.6替换为你的图像,你应该能够执行wait-for-it.sh。我也会通过命令而不是入口点来执行它:

#docker-compose.yml
...
mysql:
  image: yourmysql:5.6
  command:  bash -c "/wait-for-it.sh -t 0 mysql:3306"
...

其中-t 0将在没有超时的情况下等待mysql。


2
投票

您可以使用docker depends_on选项控制服务启动顺序。

© www.soinside.com 2019 - 2024. All rights reserved.