title: docker-compose start NodeJS app container after MySQL fully loaded
body: <p>I have Docker-compose file</p>
  <pre><code>version: &quot;3.0&quot;
  services:
      mysql:
          image: mysql
          restart: unless-stopped
          environment:
            MYSQL_ROOT_PASSWORD: root
            MYSQL_DATABASE: crud
          ports:
            - &quot;3306:3306&quot;
          hostname: mysql
          volumes:
            - ./mysql/employees.sql:/docker-entrypoint-initdb.d/employees.sql       
      app:
          build: .
          container_name: employees_app
          hostname: app
          depends_on:
            - mysql 
          ports:
            - &quot;3000:3000&quot;
  </code></pre>
  <p>and Dockerfile</p>
  <pre><code>FROM node:slim
  COPY ./app ./
  RUN npm install
  EXPOSE 3000
  CMD [&quot;npm&quot; , &quot;start&quot;]
  </code></pre>
  <p>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.</p>
  <p>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 )</p>
  <p>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</p>
  <p>the error log is :</p>
  <p>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</p>
  <pre><code>PS C:\Users\Itay-Win\Desktop\K8S-Statefulset-NodeJs-App-With-MySql&gt; docker-compose up
  &gt;&gt;
  Creating network &quot;k8s-statefulset-nodejs-app-with-mysql_default&quot; 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 | &gt; nodejs-crud@0.0.0 start
  employees_app | &gt; 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.&lt;anonymous&gt; (/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.
  </code></pre>