Quesofat
Quesofat

Reputation: 1531

Cannot Access Docker Container's Exposed Port

Hello I cannot access the exposed port. It's a node server (no framework). Chrome sends ERR_EMPTY_RESPONSE. Each time I change the file and test it I run docker-compose build. How can I get this up and running so my browser can ping port 3000?

EDIT: I included my server.js file incase I'm binding the port wrong in node.

Dockerfile 

FROM node:8.11.1-alpine

WORKDIR /usr/src/app

VOLUME [ "/usr/src/app" ]

RUN npm install -g nodemon

EXPOSE 3000

CMD [ "nodemon", "-L", "src/index.js" ]

Docker-compose.yml

version: '3'

services:
  node:
    build:
      context: ./node
      dockerfile: Dockerfile
    working_dir: /usr/src/app
    volumes:
      - ./node:/usr/src/app
    networks:
      - app-network
    env_file: ./.env
    environment:
      - MESSAGE_QUEUE=amqp://rabbitmq
    ports:
    - "3000:3000"
    links:
      - rabbitmq

  python:
    build:
      context: ./python
      dockerfile: Dockerfile
    working_dir: /usr/src/app
    volumes:
       - ./python:/usr/src/app
    networks:
      - app-network
    env_file: ./.env
    links:
      - rabbitmq

  rabbitmq:
    image: rabbitmq:3.7.4
    networks:
      - app-network

networks:
  app-network:
    driver: bridge

Server.js

const mongoose = require('mongoose')

const hostname = '127.0.0.1';
const port = 3000;

const server = require('./controllers/index');

server.listen(port, hostname, () => {
    // Connect To Mongo 
    mongoose.connect(process.env.MONGO_URI, { keepAlive: true, keepAliveInitialDelay: 300000, useNewUrlParser: true });
    mongoose.connection.on('disconnected', () => {
        console.error('MongoDB Disconnected')
    })
    mongoose.connection.on('error', (err) => {
        console.error(err)
        console.error('MongoDB Error')
    })
    mongoose.connection.on('reconnected', () => {
        console.error('MongoDB Reconnected')
    })
    mongoose.connection.on('connected', () => {
        console.error('MongoDB Connected')
    })

    console.log(`Server running at http://${hostname}:${port}/`);
});

Upvotes: 0

Views: 1621

Answers (1)

sebbalex
sebbalex

Reputation: 397

Try to bind your app to 0.0.0.0 like this

const hostname = '0.0.0.0';

it will listen on all network addresses.

Upvotes: 2

Related Questions