DEV Community

Discussion on: Dockerize a Node.js app with VS Code

 
quinncuatro profile image
Henry Quinn

For the Node -> DB bit, do you mind sharing your docker-compose.yaml and the connection string you're using in your Node container?

Thread Thread
 
bassemibrahim profile image
Bassem

Yeah sure, No problem!

version: "2"

networks:
  app-tier:
    driver: bridge

services:
  # Mysql database
  frostdb:
    image: frost/db
    hostname: frostdb

    networks:
      - app-tier
    ports:
      - "3306:3306"

  # NodeJS backend server
  frostback:
    image: frost/back
    hostname: frostback

    networks:
      - app-tier
    links:
      - frostdb:frostdb
    depends_on:
      - frostdb
    ports:
      - "49160:8080"

  # Frontend server
  frostgui:
    image: frost/gui
    hostname: frostgui

    links:
      - frostback:frostback
    ports:
      - "49170:3000"

I think that I am exposing ports using the "3306:3306". and for the backend config, I have a .env file

NODE_ENV=development

DB_TYPE=mysql
DB_NAME=frost
DB_HOST=localhost
DB_USER=USER
DB_PASSWORD=PASS
DB_PORT=3306

Actually, I have no idea what the username/password for the DB should be. But that's not the issue anyway according to the logs.

Thread Thread
 
quinncuatro profile image
Henry Quinn

You're correct that you're exposing port 3306.

Your ENV file looks good, but what about the line of code that's using those ENV variables?

Thread Thread
 
Sloan, the sloth mascot
Comment deleted
 
bassemibrahim profile image
Bassem

I am using a package called Sequelize for connecting to the DB and here is how :

const url = `${process.env.DB_TYPE}://${process.env.DB_USER}:${
      process.env.DB_PASSWORD
    }@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}`;
    this.db = new Sequelize(url, {
      define: {
        charset: "utf8mb4",
        collate: "utf8mb4_general_ci",
        timestamps: true
      }
    });

Running docker-compose up produces this error :

Unhandled rejection SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:3306

Could it be that the DB is not yet up and running?

Thread Thread
 
quinncuatro profile image
Henry Quinn • Edited

I mean, could be. Have you tried using Kitematic or docker exec -it <container name> /bin/bash?

Also, what is your output for docker ps -a when you have your compose file spun up?

Edit: I see a couple things that could be at fault here, but I want to see the answer to those two questions before I take a stab at it. ^

Thread Thread
 
quinncuatro profile image
Henry Quinn

I'd consider updating your docker-compose.yaml to version 3.x and use the container_name item to force a name on your container.

Then, in your ENV file, use whatever you set that container_name to DB_HOST. I understand why you're trying to use localhost, but if all of these containers are on one Docker network, they'll want to refer to each other by their container names.

Thread Thread
 
bassemibrahim profile image
Bassem

I feel like i need to have a better understanding of docker.

Definitely will try doing that and will update you.

Thread Thread
 
quinncuatro profile image
Henry Quinn

We all start somewhere, my dude. :)