Port mapping allows us to expose some network service inside our container as a port on the host.
The idea is that there is a port inside the container, let's say 8080 that is running our service.
We then add a port on the host that maps through to the port in the container, let's say 8888
From this location look at the Dockerfile.
Note: We use ADD here to copy in the source files
docker build -t hello-node .Now run it. This time let's run it in the background
docker run -d hello-nodeLet's confirm it is running
docker psNote: We should see our running container. Note the id
This time we are going to exec into the container to start a shell inside.
Note: You can just use the first few characters of the container id if you are lazy
docker exec -it <container id> /bin/bashNow we are inside the container, let's see what is running
ps -ANote: we have a node process. Is it serving http traffic?
curl localhost:80Yes!
Let's exit and see if the host can see that traffic
exit
# Still running? Do you know why?
docker ps
curl localhost:80
Awww :'(
Let's kill our previous container
docker kill <container id>Let's map a port on the host to a port in the container when we run it
docker run -d -p 8080:80 hello-node Let's hit the host port
curl localhost:8080Yay!
If you are in Cloud9 then go to the Preview menu and select Preview Running Application