0

I am trying to hold a container from start (ready to accept connection since it's Apache+PHP) before a dependency has finished. This is how my docker-compose.yml looks like:

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

But the result from docker-compose up is as follow:

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.

As you can see in between of MySQL import process the webserver_1 container has started already and mysqldb_1 hasn't. If I try to access the application it will fails (blank page) because MySQL isn't ready to accept connections or read some needed tables because they're not created|imported yet.

How do I avoid this? What changes do I need to make in my docker-compose.yml to achieve it?

ReynierPM
  • 17,594
  • 53
  • 193
  • 363
  • 2
    Possible duplicate of [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y) – eareese Oct 01 '17 at 15:11
  • @eareese I think you're right my search didn't return that one but yes is what I need, should I delete this post even if it has already an answer? – ReynierPM Oct 01 '17 at 15:13
  • I'm not sure of the best way to resolve, but I'm glad the answer there worked for you. Cheers! – eareese Oct 01 '17 at 15:18

1 Answers1

0

In your 'webserver' start-up script / entry point you could pass the sleep command or you could use: WAIT command as described here:

https://docs.docker.com/engine/reference/commandline/wait/
Sergiu
  • 2,928
  • 3
  • 27
  • 37