Skip to content

Latest commit

 

History

History
96 lines (60 loc) · 1.59 KB

File metadata and controls

96 lines (60 loc) · 1.59 KB

Port Mapping

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

Build the example container

From this location look at the Dockerfile.

Note: We use ADD here to copy in the source files

docker build -t hello-node .

Run it

Now run it. This time let's run it in the background

docker run -d hello-node

Let's confirm it is running

docker ps

Note: We should see our running container. Note the id

Exec into a container

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/bash

Now we are inside the container, let's see what is running

ps -A

Note: we have a node process. Is it serving http traffic?

curl localhost:80

Yes!

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 :'(

Map the Port

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:8080

Yay!

If you are in Cloud9 then go to the Preview menu and select Preview Running Application