How to run local server in host?
I’ve just installed nodejs using docker, and I run a test server:
docker exec -i -t my-running-app bash root@46593eb5df5c:/usr/src/app# npm start npm info it worked if it ends with ok npm info using firstname.lastname@example.org npm info using email@example.com npm info prestart foo@ npm info start foo@ > foo@ start /usr/src/app > node server.js Server running at http://127.0.0.1:8000/
In the docker image, I am exposing port 8000, but I don’t know how would I browse into the node server inside container from the host?
if I just browse to http://127.0.0.1:8000/ I get:
This site can’t be reached 127.0.0.1 refused to connect.
I am on ubuntu 14.04, so, how would I view that deckorized server?
2 Solutions collect form web for “How to run local server in host?”
First of all try putting all the container start up commands to Dockerfile (where you build you docker image), i.e. you need to
npm start and to expose port 8000, you should add to Dockerfile:
EXPOSE 8000 CMD [ "npm", "start" ]
You need to start you instance of the application with port mapping:
docker run -p public_port:8000 -d my-running-app
It will create a communication tunnel from “outside” world (public_port, could be the same – 8000, or whichever you need) to docker image
You need to map your container Port to your Host port:
docker exec -p 8000:3000
for example maps your 8000 container port to your 3000 host port.
By default Docker containers can make connections to the outside world, but the outside world cannot connect to containers. Each outgoing connection will appear to originate from one of the host machine’s own IP addresses thanks to an iptables masquerading rule on the host machine that the Docker server creates when it starts..
Docker doc for binding Ports