Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
How to access Docker container app on local?

I have a simple Node.js/Express app:

const port = 3000

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, () => console.log(`Example app listening on port ${port}!`))

It works fine when I start it like: node src/app.js

Now I'm trying to run it in a Docker container. Dockerfile is:

FROM node:8

WORKDIR /app
ADD src/. /app/src
ADD package.json package-lock.json /app/

RUN npm install

COPY . /app

EXPOSE 3000

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

It starts fine: docker run <my image>:

Listening on port 3000

But now I cannot access it in my browser: http://localhost:3000

This site can’t be reached localhost refused to connect.

Same happen if I try to run it within docker-compose:

version: '3.4'

services:
  service1:
    image: xxxxxx
    ports:
      - 8080:8080
    volumes:
      - xxxxxxxx
  myapp:
    build: 
      context: .
      dockerfile: Dockerfile
    networks:
      - private
    ports:
      - 3000
    command:
      node src/app.js

Not sure if I deal right with ports in both docker files

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

When you work with docker you must define the host for your app as 0.0.0.0 instead of localhost.

For your express application you can define the host on app.listen call.

Check the documentation: app.listen([port[, host[, backlog]]][, callback])

Your express code should be updated to:

const port = 3000
const host = '0.0.0.0'

app.get('/', (req, res) => res.send('Hello World!'))

app.listen(port, host, () => console.log(`Example app listening on ${port}!`))

It's also important publish docker ports:

  • Running docker: docker run -p 3000:3000 <my image>
  • Running docker-compose:
services:
  myapp:
    build: 
      context: .
      dockerfile: Dockerfile
    networks:
      - private
    ports:
      - 3000:3000
    command:
      node src/app.js
over 4 years ago · Santiago Trujillo Report

0

You need to publish ports

docker run -p 3000:3000 <my image>

-p - stands for publish

over 4 years ago · Santiago Trujillo Report

0

try this:

services:
  myapp:
    build: 
      context: .
      dockerfile: Dockerfile
    networks:
      - private
    ports:
      - 3000:3000 ##THIS IS THE CHANGE, YOU NEED TO MAP MACHINE PORT TO CONTAINER
    command:
      node src/app.js
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!