1

I am using Docker-Compose and wait-for-it to start my backend with Node.js once the MySQL service is ready to receive connections. My problem is that the script does not realize the connection is ready by itself. I can make it work if I set the timeout to 20s since MySQL would be running by then, but that could change in different environments and the app would crash if it took more time for a specific environment. I want it to check it periodically since I think it is the right thing to do.

This is my docker-compose.yml:

version: "3.8"

services:

  app:
    image: pfmc
    ports:
      - 4005:4005
    working_dir: /usr/src/app
    command: sh -c './wait-for-it.sh -t 0 db:3306 -- npm start'
    environment: 
      MYSQL_ROOT_PASSWORD: pfmc123
      MYSQL_HOST: mysql
      MYSQL_USER: root
      MYSQL_PASSWORD: pfmc123
      MYSQL_DATABASE: pfmc

  mysql:
    image: mysql:5
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    volumes:
      - ./db-startup:/docker-entrypoint-initdb.d
    environment:
      MYSQL_ROOT_PASSWORD: pfmc123
      MYSQL_DATABASE: pfmc

The logs:

app_1    | wait-for-it.sh: waiting for db:3306 without a timeout
mysql_1  | 2020-05-25 19:59:58+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.30-1debian10 started.
mysql_1  | 2020-05-25 19:59:58+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql_1  | 2020-05-25 19:59:58+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.30-1debian10 started.
mysql_1  | 2020-05-25 19:59:58+00:00 [Note] [Entrypoint]: Initializing database files

and it stays at this point:

mysql_1  | 2020-05-25T20:00:08.243448Z 0 [Note] mysqld: ready for connections.
mysql_1  | Version: '5.7.30'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)

Thank you very much for your help.

2
  • can you please also post you /wait-for-it.sh file ? Commented May 25, 2020 at 20:22
  • @NiteshSharma the file is the one included in the repo link I posted. Commented May 25, 2020 at 20:37

1 Answer 1

1

You do have one issues in here: your MySQL container is a service named mysql not db you should either adapt the service to be named db or the wait-for-it invocation to poll on a connection to mysql:3306

All together, your docker-compose.yml should look like this – I added the fix with comment stating @fixme:

version: "3.8"

services:

  app:
    image: pfmc
    ports:
      - 4005:4005
    working_dir: /usr/src/app
    command: sh -c './wait-for-it.sh -t 0 db:3306 -- npm start'
    environment: 
      MYSQL_ROOT_PASSWORD: pfmc123
      # A first @fixme is here 
      MYSQL_HOST: db
      MYSQL_USER: root
      MYSQL_PASSWORD: pfmc123
      MYSQL_DATABASE: pfmc

  # And a second @fixme is here
  db: 
    image: mysql:5
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    volumes:
      - ./db-startup:/docker-entrypoint-initdb.d
    environment:
      MYSQL_ROOT_PASSWORD: pfmc123
      MYSQL_DATABASE: pfmc
Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic! One thing though before accepting this answer. I had to change the value of MYSQL_HOST: from mysql to db to match the new container name. Now it works like a charm, thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.