0

I have Docker-compose file

version: "3.0"
services:
    mysql:
        image: mysql
        restart: unless-stopped
        environment:
          MYSQL_ROOT_PASSWORD: root
          MYSQL_DATABASE: crud
        ports:
          - "3306:3306"
        hostname: mysql
        volumes:
          - ./mysql/employees.sql:/docker-entrypoint-initdb.d/employees.sql       
    app:
        build: .
        container_name: employees_app
        hostname: app
        depends_on:
          - mysql 
        ports:
          - "3000:3000"

and Dockerfile

FROM node:slim
COPY ./app ./
RUN npm install
EXPOSE 3000
CMD ["npm" , "start"]

I'm trying to do somehow that the Nodejs container will start only after the MySQL container is fully loaded , ad you can see I Pass to the container file with some databases in it , and it take 30 sec to load the databases and for the sql will be ready , but with this workflow that I have now , the NodeJS container starts and the databases is not ready yet and then I have errors.

my work around it to just restart the NodeJS container and then its working ( because the mysql is fully up and loaded with the db )

I know there is a Healthcheck in ver 3.0 but i dont understand how i need to write it to my docker compose file

the error log is :

As you can see the MySQL start to run first but its not ready ( finished to upload the dB and all it needs , and then immediately the NodeJS container starts and I'm getting error because the app can not read the DB and then only the databases is fully loaded and the MySQL container ready to serve connection

PS C:\Users\Itay-Win\Desktop\K8S-Statefulset-NodeJs-App-With-MySql> docker-compose up
>>
Creating network "k8s-statefulset-nodejs-app-with-mysql_default" with the default driver
Creating k8s-statefulset-nodejs-app-with-mysql_mysql_1 ... done
Creating employees_app                                 ... done
Attaching to k8s-statefulset-nodejs-app-with-mysql_mysql_1, employees_app
mysql_1  | 2021-10-16 16:58:02+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started.
mysql_1  | 2021-10-16 16:58:02+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
mysql_1  | 2021-10-16 16:58:02+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.26-1debian10 started.
mysql_1  | 2021-10-16 16:58:02+00:00 [Note] [Entrypoint]: Initializing database files
mysql_1  | 2021-10-16T16:58:02.129390Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.26) initializing of server in progress as process 43
mysql_1  | 2021-10-16T16:58:02.135027Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql_1  | 2021-10-16T16:58:02.476736Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
employees_app |
employees_app | > nodejs-crud@0.0.0 start
employees_app | > node ./bin/www
employees_app |
employees_app | Error: connect ECONNREFUSED 192.168.128.2:3306
employees_app |     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1146:16)
employees_app |     --------------------
employees_app |     at Protocol._enqueue (/node_modules/mysql/lib/protocol/Protocol.js:144:48)
employees_app |     at Protocol.handshake (/node_modules/mysql/lib/protocol/Protocol.js:51:23)
employees_app |     at Connection.connect (/node_modules/mysql/lib/Connection.js:119:18)
employees_app |     at Object.<anonymous> (/lib/db.js:8:12)
employees_app |     at Module._compile (node:internal/modules/cjs/loader:1101:14)
employees_app |     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
employees_app |     at Module.load (node:internal/modules/cjs/loader:981:32)
employees_app |     at Function.Module._load (node:internal/modules/cjs/loader:822:12)
employees_app |     at Module.require (node:internal/modules/cjs/loader:1005:19)
employees_app |     at require (node:internal/modules/cjs/helpers:102:18) {
employees_app |   errno: -111,
employees_app |   code: 'ECONNREFUSED',
employees_app |   syscall: 'connect',
employees_app |   address: '192.168.128.2',
employees_app |   port: 3306,
employees_app |   fatal: true
employees_app | }
mysql_1  | 2021-10-16T16:58:03.516695Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
mysql_1  | 2021-10-16T16:58:03.516931Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
mysql_1  | 2021-10-16T16:58:03.583294Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
mysql_1  | 2021-10-16 16:58:05+00:00 [Note] [Entrypoint]: Database files initialized
mysql_1  | 2021-10-16 16:58:05+00:00 [Note] [Entrypoint]: Starting temporary server
mysql_1  | 2021-10-16T16:58:06.021335Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.26) starting as process 92
mysql_1  | 2021-10-16T16:58:06.035144Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql_1  | 2021-10-16T16:58:06.166607Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysql_1  | 2021-10-16T16:58:06.362173Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
mysql_1  | 2021-10-16T16:58:06.362306Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
mysql_1  | 2021-10-16T16:58:06.363185Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql_1  | 2021-10-16T16:58:06.363403Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
mysql_1  | 2021-10-16T16:58:06.365719Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
mysql_1  | 2021-10-16T16:58:06.378229Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
mysql_1  | 2021-10-16T16:58:06.378420Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.26'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
mysql_1  | 2021-10-16 16:58:06+00:00 [Note] [Entrypoint]: Temporary server started.
mysql_1  | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
mysql_1  | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
mysql_1  | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
mysql_1  | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
mysql_1  | 2021-10-16 16:58:08+00:00 [Note] [Entrypoint]: Creating database crud
mysql_1  |
mysql_1  | 2021-10-16 16:58:08+00:00 [Note] [Entrypoint]: /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/employees.sql
mysql_1  |
mysql_1  |
mysql_1  | 2021-10-16 16:58:08+00:00 [Note] [Entrypoint]: Stopping temporary server
mysql_1  | 2021-10-16T16:58:08.283921Z 12 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.26).
mysql_1  | 2021-10-16T16:58:10.042344Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.26)  MySQL Community Server - GPL.
mysql_1  | 2021-10-16 16:58:10+00:00 [Note] [Entrypoint]: Temporary server stopped
mysql_1  |
mysql_1  | 2021-10-16 16:58:10+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
mysql_1  |
mysql_1  | 2021-10-16T16:58:10.490529Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.26) starting as process 1
mysql_1  | 2021-10-16T16:58:10.497312Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
mysql_1  | 2021-10-16T16:58:10.612515Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
mysql_1  | 2021-10-16T16:58:10.780961Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
mysql_1  | 2021-10-16T16:58:10.781069Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
mysql_1  | 2021-10-16T16:58:10.781794Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
mysql_1  | 2021-10-16T16:58:10.781924Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
mysql_1  | 2021-10-16T16:58:10.784944Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
mysql_1  | 2021-10-16T16:58:10.797745Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
mysql_1  | 2021-10-16T16:58:10.797839Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.26'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.
  • `depends_on` in conjunction with a health check on the database will not lead to the node.js container being started iff. the database is `healthy` (see [the documentation](https://docs.docker.com/compose/compose-file/compose-file-v3/#depends_on) for details). What we could do is to set a health check on the node.js application that fails when the database is not (yet) ready so that the application is restarted automatically. – Turing85 Oct 16 '21 at 17:11
  • can you please assist me to write it in my docker-compose file ? –  Oct 16 '21 at 17:19
  • add restart: always to your node js container. which will restart until your db is ready – LiquidDeath Oct 16 '21 at 19:56

0 Answers0