在进行docker-compose时出现以下错误。该应用程序正在运行,但无法发出任何api发布/获取请求。快递服务器正在使用端口5000。
] [HPM]尝试代理请求/ api / users / user时发生错误 从localhost:3000到http://localhost:5000/(ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
尽管已设置反应代理,但错误仍然存在。
setupProxy.js
const proxy = require('http-proxy-middleware');
module.exports = function(app) {
// app.use(proxy('/auth/github', { target: 'http://localhost:3000/' }))
app.use(proxy('/api/users/auth/github', { target: 'http://localhost:5000/' }))
app.use(proxy('/api/users/', { target: 'http://localhost:5000/' }))
app.use(proxy('/api/posts/', { target: 'http://localhost:5000/' }))
}
Dockerfile
FROM node:8.10.0-alpine
EXPOSE 3000
COPY . /home/app
WORKDIR /home/app
RUN npm install
CMD ["npm", "start"]
docker-compose.yml
# docker-compose.yml
version: "3"
services:
app:
build: .
depends_on:
- postgres
ports:
- "3000:3000"
- "9229:9229"
postgres:
image: postgres:9.6.8-alpine
expose:
- 5432
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: user
POSTGRES_DB: db
package.json
{
"name": "sequelize-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"client": "cd ./client && npm start",
"server": "nodemon app.js --ignore client",
"build": "echo 'hello build'",
"start": "concurrently --kill-others \"npm run server\" \"npm run client\""
},
"author": "",
"license": "ISC",
"dependencies": {
"async": "^2.6.1",
"bcrypt": "^3.0.3",
"body-parser": "^1.18.3",
"concurrently": "^4.1.0",
"cookie-parser": "^1.4.3",
"cookie-session": "^2.0.0-beta.3",
"cors": "^2.8.5",
"crypto": "^1.0.1",
"dotenv": "^6.2.0",
"express": "^4.16.4",
"express-flash": "0.0.2",
"express-session": "^1.15.6",
"jsonwebtoken": "^8.4.0",
"morgan": "^1.9.1",
"nodemailer": "^5.1.1",
"nodemon": "^1.18.9",
"passport": "^0.4.0",
"passport-github": "^1.1.0",
"passport-github2": "^0.1.11",
"passport-jwt": "^4.0.0",
"passport-local": "^1.0.0",
"pg": "^7.8.0",
"pg-hstore": "^2.3.2",
"sequelize": "^4.42.0"
}
}
答案 0 :(得分:1)
我认为您需要在 docker-compose.yml 中将 localhost:5000 更改为服务的名称。在这种情况下,您需要在撰写文件中设置在 localhost:5000 上运行的任何服务器。请记住,您的容器在Docker网络中运行,因此,它们无法在主机上访问您的 localhost 。
在此示例中,您可以看到我需要将 PMA_HOST 环境变量设置为 db 服务而不是“普通主机”。在数据库配置中,在我的源代码上,我还将主机设置为相同的 db 服务,并且一切正常。
版本:“ 3”
services:
app:
build: .
ports:
- 80:80
depends_on:
- db
db:
image: mariadb:10.0
volumes:
- db:/var/lib/mysql
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: 123
phpmyadmin:
image: phpmyadmin/phpmyadmin
ports:
- 8080:80
environment:
PMA_HOST: db
depends_on:
- db
volumes:
db:
这是必需的,因为每次您运行新容器时,它将为新Docker网络获取一个不同的IP。在这种情况下,我们总是需要检查该新IP并将其设置在我们的代码中。 Docker通过让我们将新主机附加到一个简单且不可变的名称来解决此问题,该名称就是服务的名称。
答案 1 :(得分:0)
我改变了几件事。
Dockerfile
FROM node:8.10.0-alpine
EXPOSE 80:8080
COPY . /home/app
WORKDIR /home/app
RUN npm install
CMD ["npm", "start"]
version: "3"
services:
app:
build: .
depends_on:
- postgres
ports:
- "8080:8080"
environment:
- PORT=8080
- NODE_ENV=dev
- ENV_NAME=local
postgres:
image: postgres:9.6.8-alpine
expose:
- 5432
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: user
POSTGRES_DB: db
setupProxy.js
const proxy = require('http-proxy-middleware');
module.exports = function(app) {
app.use(proxy('/auth/github', { target: 'http://127.0.0.1:8080/' }))
app.use(proxy('/api/users/auth/github', { target: 'http://127.0.0.1:8080/' }))
app.use(proxy('/api/users/', { target: 'http://127.0.0.1:8080/', "secure": false }))
app.use(proxy('/api/posts/', { target: 'http://127.0.0.1:8080/' }))
}