Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker EXPOSE. Can't get it

Tags:

node.js

docker

this past two day I'm having trouble with docker and i can get it. Following to the docker doc you can expose the ports on which a container will listen for connections with EXPOSE. So far, so good!

If my app listen on port 8080 I should expose my docker container with EXPOSE 8080 and bind it to port 80 of the main host with docker run -p 80:8080.

Here is my Dockerfile:

# DOCKER-VERSION 0.0.1

FROM ubuntu:14.10

# make sure apt is up to date
RUN apt-get update

# install nodejs and npm
RUN apt-get install -y nodejs-legacy npm git git-core

ADD package.json /root/
ADD server.js /root/

# start script
ADD start.sh /root/
RUN chmod +x /root/start.sh

EXPOSE 8080

CMD ./root/start.sh

And my start.sh just runan cd /root/ & npm install & node server.js.

I got a simple express nodejs app:

var express = require('express');

// Constants
var PORT = 8080;

// App
var app = express();
app.get('/', function (req, res) {
  res.send('Hello world\n');
});

app.listen(PORT);
console.log('Running on http://localhost:' + PORT);

Here is how i build my docker image: docker build -t app1 . And how i launch my docker: docker run -it -p 80:8080 --name app1 app1

What is really wired, this is not working. To make it work i have to change EXPOSE 8080 to EXPOSE 80. I don't get it.

Any explanation?

Thanks for reading, Tom

like image 584
Tommy V Avatar asked Oct 31 '14 14:10

Tommy V


People also ask

What is the difference between expose and expose in Docker?

The above line will instruct Docker that the container’s service can be connected to via port 8080. By default, the EXPOSE keyword specifies that the port listens on TCP protocol. On the other hand, –expose is a runtime flag that lets you expose a specific port or a range of ports inside the container.

How do I expose a port in Docker?

There are two ways you can expose a port: 1 Using the EXPOSE Dockerfile instruction. 2 Using --expose with docker CLI or expose key in docker-compose. More ...

How do I use -P in dockerfile?

Docker allows you to add -P at runtime and convert the EXPOSE instructions in the Dockerfile to specific port mapping rules. Docker identifies all ports exposed using the EXPOSE directive and those exposed using the –expose parameter. Then, each exposed port is mapped automatically to a random port on the host interface.

What does the expose flag in a docker run string mean?

Here is an example of how to use the flag in a Docker run string: Basically, EXPOSE is a documentation mechanism that gives configuration information another command can use, provides a hint about which initial incoming ports will provide services, or informs the decisions that the container operator makes.


Video Answer


1 Answers

In your nodejs app, you have the instruction app.listen(PORT); which tells nodejs to start a server listening for connections on the loopback interface on port PORT. As a result your app will only by able to see connections originating from localhost (the container itself).

You need to tell your app to listen on all interfaces on port PORT:

app.listen(PORT, "0.0.0.0");

This way it will see the connections originating from outside your Docker container.

like image 75
Thomasleveil Avatar answered Sep 19 '22 11:09

Thomasleveil