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

时间:2017-10-01 14:48:45

标签: docker docker-compose

我试图在依赖完成之前从开始(准备好接受连接,因为它是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尚未准备好接受连接或读取一些所需的表,因为它们尚未创建导入。

我该如何避免这种情况?我需要在docker-compose.yml中做出哪些改变才能实现它?

1 个答案:

答案 0 :(得分:0)

在“webserver”启动脚本/入口点,您可以传递sleep命令,或者您可以使用:WAIT命令,如下所述:

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