Jeff Hernandez
Jeff Hernandez

Reputation: 405

difficulty connecting to SQL Server database inside a docker container using Sequelize

I have an SQL Server Docker image called microsoft/mssql-server-linux which was used to spin up an SQL Server database. I have an API which I made outside of Docker and I've unsuccessfully tried to construct a connection string to connect to this database.

I have successfully connected to this database using Azure Data Studio just with the following connection settings

Connection Type: Microsoft SQL Server
Server: localhost
Username: (username I created)
Password: (password I created)

I created an API project using Feathersjs and am using sequelize as an ORM. Here is how I've been trying to create the connection.

  const sequelize = new Sequelize(database, username, password, {
    dialect: "mssql",
    host: localhost,
    encrypt: false,
    logging: false,
    operatorsAliases: false,
    define: {
      freezeTableName: true
    },
    dialectOptions: {
      port: 1433
      // instanceName: "NameOfTheMSSQLInstance"
    }
  });

I'm using port 1433 because when I ran 'docker ps' in the terminal, it had this information listed under port.

 0.0.0.0:1433->1433/tcp

I know the username/password are correct because I'm successfully connecting through Azure Data Studio and have changed them in a docker a few times just to make sure it wasn't a credentials issue.

I'm getting the following error message when trying to connect

error: Unhandled Rejection at: Promise {"_bitField":18087936,"_fulfillmentHandler0":{"name":"SequelizeConnectionError","parent":{"message":"Failed to connect to 172.17.0.2:1433 in 15000ms","code":"ETIMEOUT"},"original":{"message":"Failed to connect to 172.17.0.2:1433 in 15000ms","code":"ETIMEOUT"}},"name":"SequelizeConnectionError","parent":{"message":"Failed to connect to 172.17.0.2:1433 in 15000ms","code":"ETIMEOUT"},"original":{"message":"Failed to connect to 172.17.0.2:1433 in 15000ms","code":"ETIMEOUT"}} error: GeneralError: Failed to connect to 172.17.0.2:1433 in 15000ms

Upvotes: 1

Views: 1764

Answers (2)

Andrei S
Andrei S

Reputation: 58

Late to the party but I used your question to fix my errors and it works now so I thought I'd share

I ran the MSSQL DB locally on Docker with:

docker run --name sql_server_demo -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=1Secure*Password1' -e 'MSSQL_PID=Developer' -p 1433:1433 -d mcr.microsoft.com/mssql/server:2017-latest

To connect to it via sequelize, I created an 'environment' as follows:

"mssql": {
   "username": "sa",
   "password": "1Secure*Password1",
   "database": "master",
   "host": "127.0.0.1",
   "dialect": "mssql"
}

This allowed me to run my migrations as my .sequelizerc file used that same environment:

// .sequelizerc

const path = require('path');

module.exports = {
    'env': 'mssql',
    'config': path.resolve('src/config/env', 'sequelize_db.json'),
    'models-path': path.resolve('src', 'models'),
    'seeders-path': path.resolve('src', 'seeders'),
    'migrations-path': path.resolve('src', 'migrations')
};

then at run-time I configured the sequelize as follows (note that I used a config set up in my Nodejs server to just pull variables from the sequelize environment file, but I replaced them here):

this.sequelize = new Sequelize('master',  'sa', '1Secure*Password1', {
      host: '127.0.0.1',
      dialect: 'mssql',
      encrypt: false,
      define: {
            freezeTableName: true
      },
      dialectOptions: {
             port: 1433
      }
});

Keep in mind I'm new to MSSQL, and this might not be the best way to do it, but I'm just happy I finally got it working

Cheers!

Upvotes: 0

A. Granados
A. Granados

Reputation: 131

is your docker database container ready to accept connections? when you use docker with sequelize, both database and app container up at same time, so you need to wait for database container to get ready to accept connections to connect sequelize with your database, besides, make sure that you are linking properly both containers. if you are using docker compose, check docs to link containers or you can use networks . https://docs.docker.com/compose/networking/#links

Upvotes: 1

Related Questions