Neglected Sanity
Neglected Sanity

Reputation: 1934

docker-compose failing sayin image doesn't exist

I am trying to take a very difficult, multi-step docker setup and make it into an easy docker-compose. I haven't really used docker-compose before. I am used to using a dockerfile to build and image, then using something like

docker run --name mysql -v ${PWD}/sql-files:/docker-entrypoint-initdb.d ... -h mysql -d mariadb:10.4

Then running the web app in the same manner after building the dockerfile that is simple. Trying to combine these into a docker-compose.yml file seems to be quite difficult. I'll post up my docker-compose.yml file, edited to remove passwords and such and the error I am getting, hopefully someone can figure out why it's failing, because I have no idea...

docker-compose.yml

version: "3.7"

services:

    mysql:
        image: mariadb:10.4
        container_name: mysql
        environment:
            MYSQL_ROOT_PASSWORD: passwordd1
            MYSQL_ALLOW_EMPTY_PASSWORD: "true"
        volumes:
            - ./sql-files:/docker-entrypoint-initdb.d
            - ./ssl:/var/lib/mysql/ssl
            - ./tls.cnf:/etc/mysql/conf.d/tls.cnf
        healthcheck:
            test: ["CMD", "mysqladmin ping"]
            interval: 10s
            timeout: 2s
            retries: 10
            
    web:
        build: ./base/newdockerfile
        container_name: web
        hostname: dev.website.com
        volumes:
            - ./ssl:/etc/httpd/ssl
            - ./webfiles:/var/www
        depends_on:
            mysql:
                condition: service_healthy
        ports:
            - "8443:443"
            - "8888:80"
        entrypoint:
            - "/sbin/httpd"
            - "-D"
            - "FOREGROUND"

The error I get when running docker-compose up in the terminal window is...

Service 'web' depends on service 'mysql' which is undefined.

Why would mysql be undefined. It's the first definition in the yml file and has a health check associated with it. Also, it fails very quickly, like within a few seconds, so there's no way all the healthchecks ran and failed, and I'm not getting any other errors in the terminal window. I do docker-compose up and within a couple seconds I get that error. Any help would be greatly appreciated. Thank you.

Upvotes: 0

Views: 270

Answers (1)

Dory Nguyen
Dory Nguyen

Reputation: 371

according to this documentation

depends_on does not wait for db and redis to be “ready” before starting web - only until they have been started. If you need to wait for a service to be ready, see Controlling startup order for more on this problem and strategies for solving it.

Designing your application so it's resilient when database is not available or set up yet is what we all have to deal with. Healthcheck doesn't guarantee you database is ready before the next stage. The best way is probably write a wait-for-it script or wait-for and run it after depends-on:

depends_on:
  - "db"
command: ["./wait-for-it.sh"] 

Upvotes: 1

Related Questions