0

I'm new to docker. I tried to find other solution same as mine but it didn't work at all. When run docker compose up, it responded connection error. Here's my full code Link

error

errno: -111, server-api-1 | code: 'ECONNREFUSED', server-api-1 | syscall: 'connect', server-api-1 | address: '127.0.0.1', server-api-1 | port: 3306, server-api-1 | fatal: true server-api-1 | }, server-api-1 | original: Error: connect ECONNREFUSED 127.0.0.1:3306 server-api-1 | at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16) { server-api-1 | errno: -111, server-api-1 | code: 'ECONNREFUSED', server-api-1 | syscall: 'connect', server-api-1 | address: '127.0.0.1', server-api-1 | port: 3306, server-api-1 | fatal: true server-api-1 | } server-api-1 | }

config.json

{
  "development": {
    "username": "root",
    "password": "Password",
    "database": "vc_chat",
    "dialect": "mysql"
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "production": {
    "username": "root",
    "password": null,
    "database": "database_production",
    "host": "127.0.0.1",
    "dialect": "mysql"
  }
}

Dockerfile

FROM node:16-alpine

WORKDIR /app/server
ADD server /app/server
RUN npm install


COPY . .


CMD [ "npm", "start" ]

docker-compose.yml

version: "3.9"

services:
  # Mysql
  mysql_db:
    container_name: db_container
    image: mysql:latest
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_DATABASE: "vc_chat"
      MYSQL_USER: "root"
      MYSQL_PASSWORD: "Password"
      MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
      DB_HOSTNAME: mysql
    volumes:
      - mysql_db:/var/lib/mysql

  api:
    build: .
    ports:
      - 4000:4000
    environment:
      - EMAIL= "test@gmail.com"
      - PASSWORD= "test123"
      - JWT_SECRET= "DENNY"
    depends_on:
      - mysql_db
volumes:
  mysql_db: {}
Denny
  • 167
  • 4
  • 8

1 Answers1

1

You can't use 127.0.0.1 (localhost) in the context of docker since each container will view that as inside itself. When running docker-compose, the services will be entered into the same docker network. In that docker network, the services are reachable by the contianer name,

so the database should be reachable at db_container:3306 (meaning, you will have to change the confguration for db host in config.json)

tbjorch
  • 1,544
  • 1
  • 8
  • 21