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:
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.
Those speicifications explained:
- target: the port inside the container
- published: the publicly exposed port
- protocol: the port protocol (tcp or udp)
- mode: host for publishing a host port on each node, or ingress for a swarm mode port to be load balanced.
Read more about it here