>_ IzzyLab

Hands-on task

This task is step 4 of the course “Docker networking”

EXPOSE doesn't publish a port

easy~12 min

A colleague built the catalog:1 image from /root/catalog and started the catalog container with the -P flag: "the Dockerfile has EXPOSE, the port will be published by itself". Docker did hand out a port on the host (docker port catalog), but the page doesn't open on it.

Figure it out and fix it:

  • EXPOSE in /root/catalog/Dockerfile must describe the port the service actually listens on;
  • the catalog:1 image is rebuilt from the fixed Dockerfile;
  • the catalog container runs from the new image, and the service is published on the server's 127.0.0.1:8090.

The image builds without the internet: the base busybox:1.36 is already available locally. The legacy builder is deprecated warning during the build is not an error.

What you'll practise:
  • EXPOSE
  • docker build
  • docker run -p

Topic breakdown

EXPOSE in a Dockerfile is just image metadata: "this service listens on such-and-such port". By itself it opens nothing and doesn't make the application listen on that port. It's used by people (visible in docker image inspect), by docker run -P (publishes all declared ports on random free host ports) and by tools like compose. Only -p (ports: in compose) publishes a port, and it doesn't need EXPOSE: -p 127.0.0.1:8090:9000 forwards the container's port 9000 even if it's declared nowhere. A typical mistake is EXPOSE drifting away from the real port after a config change: -P then publishes a port nobody listens on inside.

What's checked

  1. EXPOSE in the catalog:1 image matches the port the service listens on
  2. The catalog container runs from the rebuilt catalog:1 image
  3. The service is published on 127.0.0.1:8090
  4. The page opens on 127.0.0.1:8090

What it looks like