如何在依赖关系“真正”完成之前停止一个容器的启动?

我试图在依赖完成之前从头开始(准备好接受连接,因为它是Apache + PHP)。 这是我docker-compose.yml样子:

 version: '3' services: webserver: image: reynierpm/docker-lamp dns: - 8.8.8.8 - 8.8.4.4 ports: - "80:80" volumes: - f:/sources:/var/www/html depends_on: - mysqldb - mongodb restart: on-failure environment: SERVER_URL: 'localhost' SERVER_SCHEME: 'http' HTTP_PORT: '80' mysqldb: image: mysql:latest healthcheck: test: "exit 0" interval: 1m30s timeout: 10s retries: 3 env_file: .env environment: MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} MYSQL_DATABASE: ${MYSQL_DATABASE} MYSQL_USER: ${MYSQL_USER} MYSQL_PASSWORD: ${MYSQL_PASSWORD} volumes: - f:/sources/db_dump:/docker-entrypoint-initdb.d ports: - "3306:3306" restart: on-failure mongodb: image: mongo:latest env_file: .env environment: MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME} MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD} MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE} ports: - "27017:27017" restart: on-failure 

但是docker-compose up的结果如下:

 mysqldb_1 | mysql: [Warning] Using a password on the command line interface can be insecure. mysqldb_1 | mysqldb_1 | mysqldb_1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/language.sql mysqldb_1 | mysql: [Warning] Using a password on the command line interface can be insecure. mysqldb_1 | mysqldb_1 | mysqldb_1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/license.sql mysqldb_1 | mysql: [Warning] Using a password on the command line interface can be insecure. webserver_1 | Enabling module rewrite. webserver_1 | Enabling module expires. webserver_1 | To activate the new configuration, you need to run: webserver_1 | service apache2 restart webserver_1 | AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.18.0.4. Set the 'ServerName' directive globally to suppress this message mysqldb_1 | mysqldb_1 | mysqldb_1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/license_agreement.sql mysqldb_1 | mysql: [Warning] Using a password on the command line interface can be insecure. mysqldb_1 | mysqldb_1 | mysqldb_1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/license_forcast.sql mysqldb_1 | mysql: [Warning] Using a password on the command line interface can be insecure. 

正如你可以在MySQL导入过程中看到的那样, webserver_1容器已经启动了, mysqldb_1还没有启动。 如果我尝试访问应用程序,它将失败(空白页),因为MySQL尚未准备好接受连接或读取一些所需的表,因为它们尚未创build。

我如何避免这种情况? 我需要在docker-compose.yml做什么更改才能实现?

在你的'networking服务器'启动脚本/入口点你可以传递睡眠命令,或者你可以使用:WAIT命令,如下所述:

 https://docs.docker.com/engine/reference/commandline/wait/