遇到问题,尝试连接 Node.js 应用程序(容器 A)和 SQL Server(容器 B)。

huangapple go评论93阅读模式
英文:

facing an issue while connecting nodejs app(container a) and sql server (container b)

问题

这是您提供的代码的翻译部分:

web docker file

  1. FROM node:14
  2. WORKDIR /app
  3. COPY package*.json ./
  4. RUN npm install
  5. COPY . .
  6. EXPOSE 3000
  7. CMD ["npm", "start"]

db docker file

  1. FROM mcr.microsoft.com/mssql/server:2019-latest
  2. EXPOSE 1433

docker compose file

  1. version: "3.8"
  2. services:
  3. db:
  4. build: ./db
  5. environment:
  6. SA_PASSWORD: mahe@123
  7. ACCEPT_EULA: Y
  8. MSSQL_PID: Developer
  9. ports:
  10. - "1433:1433"
  11. restart: always
  12. web:
  13. build: ./web
  14. environment:
  15. DB_CONNECTION_STRING: Server=localhost;Database=Bitespeed;User Id=sa;Password=mahe@123;
  16. ports:
  17. - "3000:3000"
  18. depends_on:
  19. - db
  20. restart: on-failure

dbconfig.js file

  1. const config = {
  2. user: 'sa',
  3. password: 'mahe@123',
  4. server: 'localhost',
  5. database: 'Bitespeed',
  6. options: {
  7. enableArithAbort: true,
  8. trustServerCertificate: true
  9. },
  10. port: 1433
  11. }
  12. module.exports = config;

server.js file

  1. const express = require('express')
  2. const app = express()
  3. const bodyParser = require('body-parser')
  4. const sql = require('mssql');
  5. const config = require("./dbconfig")
  6. require('dotenv').config()
  7. const port = process.env.PORT
  8. app.use(bodyParser.urlencoded({extended: true}))
  9. app.use(bodyParser.json())
  10. const mainRouter = require('./router')
  11. app.use('/api', mainRouter)
  12. function connectWithRetry(){
  13. return sql.connect(config, (err) => {
  14. if (err) {
  15. console.log(`Connection to DB failed, retry in 5s `, err.message)
  16. sql.close()
  17. setTimeout(connectWithRetry(), 5000);
  18. }
  19. else {
  20. console.log('Connected to SQL Server successfully');
  21. }
  22. });
  23. }
  24. connectWithRetry()
  25. app.listen(port, () => {
  26. console.log("app is listening on port " + port)
  27. })

希望这能帮助您解决代码中的问题。如果您有进一步的问题或需要更多帮助,请随时提出。

英文:

web docker file

  1. FROM node:14
  2. WORKDIR /app
  3. COPY package*.json ./
  4. RUN npm install
  5. COPY . .
  6. EXPOSE 3000
  7. CMD ["npm", "start"

db docker file

  1. FROM mcr.microsoft.com/mssql/server:2019-latest
  2. EXPOSE 1433

docker compose file

  1. version: "3.8"
  2. services:
  3. db:
  4. build: ./db
  5. environment:
  6. SA_PASSWORD: mahe@123
  7. ACCEPT_EULA: Y
  8. MSSQL_PID: Developer
  9. ports:
  10. - "1433:1433"
  11. restart: always
  12. web:
  13. build: ./web
  14. environment:
  15. DB_CONNECTION_STRING: Server=localhost;Database=Bitespeed;User Id=sa;Password=mahe@123;
  16. ports:
  17. - "3000:3000"
  18. depends_on:
  19. - db
  20. restart: on-failure

dbconfig.js file

  1. const config = {
  2. user: 'sa',
  3. password: 'mahe@123',
  4. server: 'localhost',
  5. database: 'Bitespeed',
  6. options: {
  7. enableArithAort: true,
  8. trustServerCertificate:true
  9. },
  10. port: 1433
  11. }
  12. module.exports = config;

server.js file

  1. const express = require('express')
  2. const app = express()
  3. const bodyParser = require('body-parser')
  4. const sql = require('mssql');
  5. const config = require("./dbconfig")
  6. require('dotenv').config()
  7. const port = process.env.PORT
  8. app.use(bodyParser.urlencoded({extended: true}))
  9. app.use(bodyParser.json())
  10. const mainRouter = require('./router')
  11. app.use('/api',mainRouter)
  12. function connectWithRetry(){
  13. return sql.connect(config,(err) => {
  14. if (err) {
  15. console.log(`Connection to DB failed, retry in 5s `,err.message)
  16. sql.close()
  17. setTimeout(connectWithRetry(), 5000);
  18. }
  19. else {
  20. console.log('Connected to SQL Server successfully');
  21. }
  22. });
  23. }
  24. connectWithRetry()
  25. app.listen(port , ()=>{
  26. console.log("app is listening on port " + port)
  27. })

can any plase help me what is the error. and i am getting the following error

enter image description here

and my db is running fine..enter image description here

can any one help me with this..

i am expecting my node app will connect to the sql server.

答案1

得分: 0

在您的情况下,localhost 将指向容器本身(web)。您可以在配置中指定数据库服务的 docker-compose 别名(db):

  1. const config = {
  2. ...
  3. server: 'db',
  4. ...
  5. port: 1433
  6. }

由于您还在 docker-compose.myml 中设置了一个环境变量 DB_CONNECTION_STRING,它似乎会覆盖您的设置,您可能还需要更改该环境变量中的 "localhost":

  1. web:
  2. build: ./web
  3. environment:
  4. DB_CONNECTION_STRING: Server=db;Database=Bitespeed;User Id=sa;Password=mahe@123;
英文:

In your case, localhost will point to the container itself (web). You can specify the docker-compose alias of the database service (db) instead in your config:

  1. const config = {
  2. ...
  3. server: 'db',
  4. ...
  5. port: 1433
  6. }

Since you are also setting an environment variable DB_CONNECTION_STRING in your docker-compose.myml which seems to overwrite your settings, you probably also need to change the "localhost" in that environment variable:

  1. web:
  2. build: ./web
  3. environment:
  4. DB_CONNECTION_STRING: Server=db;Database=Bitespeed;User Id=sa;Password=mahe@123;`

huangapple
  • 本文由 发表于 2023年6月26日 14:55:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定