Daniel Taub
Daniel Taub

Reputation: 5389

Dockerfile expose port for node.js image

I'm trying to expose port for node.js image but it's doesn't expose the port outside :

FROM node:4-onbuild
ADD . /opt/app

EXPOSE 8000:8000

CMD ["npm", "start"]

only when I declare the port when running the image it's works

docker run -p=8000:8000 node

Is there something I'm missing?

Upvotes: 2

Views: 5531

Answers (1)

Dave Kerr
Dave Kerr

Reputation: 5297

The EXPOSE directive is metadata only. If you check:

https://docs.docker.com/engine/reference/builder/#expose

You'll see you still need to manually expose the port. The EXPOSE directive is useful for platforms which might want to know what a container expects to expose. For example, on OpenShift if I deployed your container it would suggest I expose port 8000.

If you don't want to manually expose the port, try this:

docker run --net=host

This will bind the container network controller to the host network . Be aware however this is a security concern, as there is no isolation between the container and the host machine (for TCPIP or UDP). This means your container can try to listen to any port on the host! Some containers do run like this, for example the consul client:

https://hub.docker.com/_/consul/

Which is a chatty client which talks over lots of ports. Binding it to the host makes it much easier to quickly run the client, but I would avoid that approach unless absolutely necessary. Unfortunately, you'll probably just need to get used to the -p flag!

Upvotes: 5

Related Questions