Understanding Docker Port Mappings

Docker allows you to map ports to what is available (exposed) to the host and what is available to the container. These handy mappings can give you control over what is exposed and allow specify specific ports to allow to be accessed. If you’re using docker compose the pattern is HOST:CONTAINER, for example “3000:80”

The host is the operating system in which the Docker client is running. So, if you ssh into a server, you entered the host. The container is what is running on top of your host, for example if you run a docker run command, the thing that will run as a result of that command is the container. Here are some examples of port mappings:

ports:
 - "3000"
 - "3000-3005"
 - "8000:8000"
 - "9090-9091:8080-8081"
 - "49100:22"
 - "127.0.0.1:8001:8001"
 - "127.0.0.1:5000-5010:5000-5010"
 - "6060:6060/udp"

Docker recommends to always explicily specify your port mappings as strings because of the way YAML parses numbers.

There is also the long format syntax which allows additional fields to be specified that can’t be expressed in the short form. This is avaialble in docker compose 3.2 version and up.

ports:
  - target: 80
    published: 8080
    protocol: tcp
    mode: host

Those speicifications explained:

Read more about it here

Instagram Post