Skip to content

docker-compose-port-mapping-syntax

The docker-compose-port-mapping-syntax defines how container ports are exposed to the host machine within a docker-compose.yml file.^[600-developer__docker__docker-compose__docker-compouse-02.md]

Syntax Format

The port mapping is configured under the ports section of a service definition^[600-developer__docker__docker-compose__docker-compouse-02.md]. The syntax uses the format "HOST_PORT:CONTAINER_PORT", where the host port number is specified first, followed by a colon, and then the container port number^[600-developer__docker__docker-compose__docker-compouse-02.md].

Example

In the following configuration, the MySQL service maps port 33060 on the host to port 3306 inside the container^[600-developer__docker__docker-compose__docker-compouse-02.md]:

services:
  db:
    image: mysql:5.7
    ports:
      - "33060:3306"

This setup allows external applications to connect to the database service via the host's port 33060.

Sources