Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Compose: Depends_On with an external container?

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?

like image 215
J. Scott Elblein Avatar asked Dec 14 '19 20:12

J. Scott Elblein


People also ask

What is Depends_on in docker-compose?

depends_on is a Docker Compose keyword to set the order in which services must start and stop. For example, suppose we want our web application, which we'll build as a web-app image, to start after our Postgres container.

Can you run docker-compose in a container?

Compose can also be run inside a container, from a small bash script wrapper.

What is external true in docker-compose?

external: True flag is used to make the containers join a pre-existing network instead. From docs: external. If set to true, specifies that this network has been created outside of Compose. docker-compose up does not attempt to create it, and raises an error if it doesn't exist.09-Jan-2020.

Is docker-compose deprecated?

Compose V1 is marked as deprecated, and we'll begin patching only high-severity vulnerabilities or fixing critical bugs until the next milestone. Developers can continue to alias docker-compose to use docker compose.


1 Answers

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 "$@"
like image 166
BMitch Avatar answered Oct 05 '22 23:10

BMitch