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

Viewed 449

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

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

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. The docker version is Docker version 19.03.13, build 4484c46d9d and I'm in a 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.

[EDIT 1] My index.js where I set up the port configuration and start the application

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

[EDIT 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"]
1 Answers

With a slightly modified Dockerfile / app.js, I am able to curl localhost:8090 and get a response.

Dockerfile:

FROM node:14-alpine

WORKDIR /app

COPY package.json .

RUN npm install

COPY . .

EXPOSE 8080

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

App.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.

Related