Why does my app listen on port 80 instead of port 3000 as I set it running inside docker container?

Viewed 2987

What I'm trying?

I'm trying to run my nodejs app inside a docker container and want to use it outside of the container(through my browser on port 3000).

DockerFile

FROM node:8
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
RUN npm run build
COPY . .
EXPOSE 3000
CMD [ "npm", "start" ]

app.ts(Relevant portion)

const port = process.env.port || 3000;   
let app = express();
app.listen(port, () => {
    console.log(`Listening on port ${port}!`);
});

command I'm using to run

$ docker run --net=host <imgName>

OS --> Windows 7

And in the Oracle Virtual Box I've changed network settings to bridged networks.

The command runs successfully and the server starts listening on port 80. And I can access it from outside the container on port 80 through postman,curl,browser,etc.

Where I am doing wrong? How can I make it to listen on port 3000? And also how is it even able to listen on port if I haven't exposed it explicitly?

I think the docker is passing port as an environment variable and setting it to 80 as I am not passing any environment variable myself.

Please help I'm very new to docker.

1 Answers

Just exposing in your Dockerfile won't do it for you. You will need to map it when you do docker run or execute using docker-example.yaml.

To achieve that you will need to use $ docker run -p 3000:3000. Following that you won't need to use --net=host that should be the reason why the service is available on port 80.

Hope it helps!

Cheers!

Related