For a project I'm trying to use Node.js unit tests in GitHub actions. But I'm having problems connecting the Node.js with my database running in docker (via GitHub).
This is my workflow:
name: Node.js CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
# Service containers to run with `runner-job`
services:
# Label used to access the service container
biddify-product-database:
# Docker Hub image
image: robfontys/biddify-product-database:latest
#
ports:
# Opens tcp port 6379 on the host and service container
- 3306:3306
timeout-minutes: 1
strategy:
matrix:
node-version: [12.x, 14.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test
When I run the workflow this is the error:

So my question is how to connect Node.js to the MySQL database? Is the IP address of the contrainer 127.0.0.1 (localhost)?
A better way to solve this would be to use healthcheck. For example if the service that is taking a long time to get to a healthy state is mysql you can add a health check to that container
healthcheck:
test: mysqladmin ping -h mysql --user=$$MYSQL_USER --password=$$MYSQL_ROOT_PASSWORD
interval: 30s
timeout: 12s
retries: 10
and then on your server add a dependency that the mysql service is in a healthy condition
depends_on:
mysql:
condition: service_healthy
This way when you bring up your stack it will wait until mysql is healthy, bring up the server and then detach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With