8

I'm trying to add depends_on for an external MySQL container named "mysql" (I don't want to create a new mysql container for this stack; I want to use the existing container).

My code so far looks like:

version: '2'

services:
  wordpress:
    image: wordpress:latest
    hostname: mia
    restart: unless-stopped
    ports:
      - 80
    environment:
      WORDPRESS_DB_HOST: mysql:3306
      WORDPRESS_DB_USER: "mia"
      WORDPRESS_DB_PASSWORD: "12345"
      WORDPRESS_DB_NAME: "mia"
    volumes:
      - /f/Sites/mia:/var/www/html
    depends_on:
      - mysql
    networks:
      - occms
      - ocdb

  mysql:
      name: mysql

networks:
  occms:
    external:
      name: cms
  ocdb:
    external:
      name: db

Anyone know a solution?

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
  • 1
    Did you know you can split up `docker-compose.yml` and then start multiple at the same time with the `-f` parameter ([source](https://github.com/docker/compose/issues/3951#issuecomment-248979919))? I know it does not exactly answer your question, but it did solve a similar issue for me, so who knows... – Harm Oct 16 '20 at 14:55
  • PS You can set the same in `COMPOSE_FILE` within `.env` ([source](https://docs.docker.com/compose/reference/envvars/#compose_file)) – Harm Oct 16 '20 at 16:34
  • Also, I think this discussion covers your exact use case: https://github.com/docker/compose/issues/2075 – Harm Oct 16 '20 at 16:34

2 Answers2

10

The depends_on directive only works with services in the same compose project. They may be in different files if you merge those files into the same project, e.g.

docker-compose -f compose-app.yaml -f compose-db.yaml up

Otherwise, I'd recommend moving the startup dependency out of compose and into your application's entrypoint. The common example of this is wait-for-it.sh which would allow you to have an entrypoint script that does:

#!/bin/sh
# delay for mysql startup using wait-for-it for up to 5 minutes
wait-for-it.sh -h mysql -p 3306 -t 300
# call the original wordpress entrypoint script with any args
exec docker-entrypoint.sh "$@"
BMitch
  • 231,797
  • 42
  • 475
  • 450
0

it sounds like you want to manage the MySQL Container by another Compose stack.

so option one: Check if external links are working for you: Docker-compose external_links not able to connect

Another option is to wire it all through the hosts Network Stack:

In the current stack, you need to change this part

WORDPRESS_DB_HOST: mysql:3306

As proof of concept, replace mysql with the IP address of the MySQL container and remove any depends_on.

On the other hand, you should make sure that when the MySQL container is started, the container port is linked to the host port. So the two are communicating via Host stack. Also, hostname mysql can be configured as a second step.

aholbreich
  • 4,951
  • 2
  • 23
  • 38