Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Export Services with Docker Port Binding


    By default, Docker is locked down and no ports are exposed. We’ll cover how to expose ports when building your image with a Dockerfile, and how to export that port as a service when running containers.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: Let's take a look at a simple Node.js app. All we are doing is responding, "Hello, World!" to every request, and then starting the web server on port 8000.

    For Docker file t's as simple as well, just copying over the index.js file and starting Node. Let's build this into a Docker image named foo, and then run it with dockerrun-dfoo.

    If we try to access this address in port for curl, we will get a connection refused failure response. This is because Docker locks down all ports by default, just like a firewall. We need to open the container to port 8000 and make it publicly accessible.

    Let's start up another container, but this time with also defining a -p flag. This is how we tell Docker which port we want to access on the container, and where on the host we want to access it. The host definition comes first, followed by the container port. If we try to access this again with curl, we will get our "Hello, World!" response.

    Note that we can also map the container port to a different port on the host. This is useful when running multiple Docker images at the same time that all run on the same port.

    If using compose, you could do the same thing by defining the ports option. It accepts multiple values following the same format as the -p flag, with the host port coming first, followed by the container port. Let's test this out by starting our app with compose, then testing this new 8010 port with curl.