简体   繁体   English

docker-compose.yml 中的绑定端口不起作用

[英]Bind port in docker-compose.yml doesn't work

I have a NodeJS and React application made with a docker-compose.yml and I'm trying to put my application on port :8090 , but when I do a curl , I get this error我有一个使用 docker-compose.yml 制作的 NodeJS 和 React 应用程序,我试图将我的应用程序放在端口:8090 ,但是当我执行curl ,出现此错误

curl localhost:8090
curl: (56) Recv failure: Connection reset by peer

docker-compose.yml docker-compose.yml

version: '3.1'

services:

  web:
    build: 
      context: .
      args:
        # Variaveis do frontend
        - BACKEND_URL=http://localhost:8090
    restart: always
    container_name: petcertificados
    ports:
      - "8090:8080"
    volumes:
      - ./certificados:/usr/app/server/certificados

If I go inside my container, I can access my website, but not if I'm outside如果我进入我的容器,我可以访问我的网站,但如果我在外面则不行

[root@blastoise certificados]# docker exec -it petcertificados /bin/bash
root@a0d690c11c38:/usr/app/server# curl localhost:8080/api
<!DOCTYPE html>
<html>
  <head>
    <title>Certificados - Backend</title>
  </head>
  <body>
    <h3>Certificados - BackEnd</h3>
  </body>

Because in my personal computer this works, I think it may be a error with the docker in the machine.因为在我的个人电脑上这是有效的,我认为可能是机器中的docker有错误。 The docker version is Docker version 19.03.13, build 4484c46d9d and I'm in a CentOS. Docker version 19.03.13, build 4484c46d9d版本是Docker version 19.03.13, build 4484c46d9d ,我在 CentOS 中。 I tried to restart the docker with service docker restart but doesn't work and I don't have ideia of what could be.我试图用service docker restart docker 但不起作用,我不知道可能是什么。

[EDIT 1] My index.js where I set up the port configuration and start the application [编辑 1]我在 index.js 中设置端口配置并启动应用程序

const express = require('express')
const app = express();
const path = require('path');
const cors = require('cors')

const { BACKEND_PORT } = require("./constants/index.js");

const CLIENT_BUILD_PATH = path.join(__dirname, '../../frontend/build');
app.use(express.static(CLIENT_BUILD_PATH));

app.use(express.json());

app.use(cors());
app.use((req, res, next) => {
    next();
})

app.get('/api', (req, res) => {
    res.sendFile(path.join(__dirname, 'views', 'index.html'));
});

app.use('/api/certificados', require('./routes/Certificados'));
app.use('/api/eventos', require('./routes/Eventos'));

app.listen(BACKEND_PORT, () => { 
    console.info(`Iniciando PETCertificados!`)
});

I tried to put "0.0.0.0" but this is already the default on NodeJS我试图把“0.0.0.0”,但这已经是NodeJS的默认值

[EDIT 2] Dockerfile [编辑 2] Dockerfile

FROM node:14.5.0 as frontend

WORKDIR /usr/app/frontend

COPY frontend/package*.json ./
RUN npm install -qy

COPY frontend/ ./

ARG BACKEND_URL
ENV REACT_APP_BACKEND_URL ${BACKEND_URL}

RUN npm run build

FROM node:14.5.0
WORKDIR /usr/app/ 

COPY --from=frontend /usr/app/frontend/build ./frontend/build

WORKDIR /usr/app/server/
COPY backend/package*.json ./

RUN npm install -qy
COPY backend/ ./

CMD ["npm", "start"]

With a slightly modified Dockerfile / app.js, I am able to curl localhost:8090 and get a response.通过稍微修改 Dockerfile/app.js,我可以 curl localhost:8090 并得到响应。

Dockerfile: Dockerfile:

FROM node:14-alpine

WORKDIR /app

COPY package.json .

RUN npm install

COPY . .

EXPOSE 8080

CMD [ "node", "app.js" ]

App.js应用程序.js

const express = require('express')
const app = express();
const path = require('path');
const cors = require('cors')

const CLIENT_BUILD_PATH = path.join(__dirname, '../../frontend/build');
app.use(express.static(CLIENT_BUILD_PATH));

app.use(express.json());

app.use(cors());
app.use((req, res, next) => {
    next();
})

app.get('/', (req, res) => {
    res.send('Hello world!')
});

app.listen(8080, '0.0.0.0', () => { 
    console.info(`Iniciando PETCertificados!`)
});

docker-compose.yml stays the same. docker-compose.yml 保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM