Home > database >  Can't connect Node.js with Docker MySQL database in GitHub actions
Can't connect Node.js with Docker MySQL database in GitHub actions

Time:11-10

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:

Github actions

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)?

CodePudding user response:

You need to wait for the Docker container to be fully started up, so you need to add a sleep for a sufficient amount of time. I found that 2 minutes is more than enough for small projects. Try to run the following:

    name: Node.js CI
    on:
      push:
        branches: [ master ]
      pull_request:
        branches: [ master ]
    jobs:
      build:
        runs-on: ubuntu-latest
         uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm install
    - run: npm run build --if-present
    - run: docker-compose up -d biddify-product-database
    - name: Sleep for 120 seconds
      uses: jakejarvis/wait-action@master
      with:
        time: '120s'
    - run: npm test
  • Related