英文:
facing an issue while connecting nodejs app(container a) and sql server (container b)
问题
这是您提供的代码的翻译部分:
web docker file
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
db docker file
FROM mcr.microsoft.com/mssql/server:2019-latest
EXPOSE 1433
docker compose file
version: "3.8"
services:
db:
build: ./db
environment:
SA_PASSWORD: mahe@123
ACCEPT_EULA: Y
MSSQL_PID: Developer
ports:
- "1433:1433"
restart: always
web:
build: ./web
environment:
DB_CONNECTION_STRING: Server=localhost;Database=Bitespeed;User Id=sa;Password=mahe@123;
ports:
- "3000:3000"
depends_on:
- db
restart: on-failure
dbconfig.js file
const config = {
user: 'sa',
password: 'mahe@123',
server: 'localhost',
database: 'Bitespeed',
options: {
enableArithAbort: true,
trustServerCertificate: true
},
port: 1433
}
module.exports = config;
server.js file
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const sql = require('mssql');
const config = require("./dbconfig")
require('dotenv').config()
const port = process.env.PORT
app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json())
const mainRouter = require('./router')
app.use('/api', mainRouter)
function connectWithRetry(){
return sql.connect(config, (err) => {
if (err) {
console.log(`Connection to DB failed, retry in 5s `, err.message)
sql.close()
setTimeout(connectWithRetry(), 5000);
}
else {
console.log('Connected to SQL Server successfully');
}
});
}
connectWithRetry()
app.listen(port, () => {
console.log("app is listening on port " + port)
})
希望这能帮助您解决代码中的问题。如果您有进一步的问题或需要更多帮助,请随时提出。
英文:
web docker file
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"
db docker file
FROM mcr.microsoft.com/mssql/server:2019-latest
EXPOSE 1433
docker compose file
version: "3.8"
services:
db:
build: ./db
environment:
SA_PASSWORD: mahe@123
ACCEPT_EULA: Y
MSSQL_PID: Developer
ports:
- "1433:1433"
restart: always
web:
build: ./web
environment:
DB_CONNECTION_STRING: Server=localhost;Database=Bitespeed;User Id=sa;Password=mahe@123;
ports:
- "3000:3000"
depends_on:
- db
restart: on-failure
dbconfig.js file
const config = {
user: 'sa',
password: 'mahe@123',
server: 'localhost',
database: 'Bitespeed',
options: {
enableArithAort: true,
trustServerCertificate:true
},
port: 1433
}
module.exports = config;
server.js file
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const sql = require('mssql');
const config = require("./dbconfig")
require('dotenv').config()
const port = process.env.PORT
app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json())
const mainRouter = require('./router')
app.use('/api',mainRouter)
function connectWithRetry(){
return sql.connect(config,(err) => {
if (err) {
console.log(`Connection to DB failed, retry in 5s `,err.message)
sql.close()
setTimeout(connectWithRetry(), 5000);
}
else {
console.log('Connected to SQL Server successfully');
}
});
}
connectWithRetry()
app.listen(port , ()=>{
console.log("app is listening on port " + port)
})
can any plase help me what is the error. and i am getting the following error
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
):
const config = {
...
server: 'db',
...
port: 1433
}
由于您还在 docker-compose.myml
中设置了一个环境变量 DB_CONNECTION_STRING
,它似乎会覆盖您的设置,您可能还需要更改该环境变量中的 "localhost":
web:
build: ./web
environment:
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:
const config = {
...
server: 'db',
...
port: 1433
}
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:
web:
build: ./web
environment:
DB_CONNECTION_STRING: Server=db;Database=Bitespeed;User Id=sa;Password=mahe@123;`
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论