In a Docker Compose file (docker-compose.yml
), the ports
section is used to expose network ports for services defined in the file. It allows containers to listen on specified ports and make them accessible from outside the Docker environment.
In the given example, the ports
section is using the following format:
ports:
- "${BACKEND_PORT:-80}:80"
This line maps the host machine’s port to the container’s port. Let’s break it down:
${BACKEND_PORT:-80}
: This is a variable substitution that allows flexibility in specifying the host machine’s port. It uses the value of the environment variableBACKEND_PORT
if it is defined, or falls back to the default value of80
if the variable is not set. It means that you can override the default port by setting theBACKEND_PORT
environment variable.80:80
: This represents the port mapping in the format<host_port>:<container_port>
. In this case, it maps port80
of the container to the same port80
of the host machine. So, any traffic that arrives at the host’s port80
will be forwarded to the container’s port80
.
Overall, this configuration allows you to access the backend service running inside the container via the host machine’s port 80
(or the custom port specified by the BACKEND_PORT
environment variable). For example, if the host machine’s IP address is 192.168.0.1
, you can access the backend service by visiting http://192.168.0.1
on your web browser.