Docker port mapping¶
Docker port mapping is a mechanism that allows services running inside a Docker container to be accessed via ports on the host machine.^[600-developer-docker-docker-compose-docker-compouse-02.md] This creates a connection between a specific port on the host and a specific port inside the container.
Syntax and Configuration¶
Port mappings are typically defined within a service's configuration using the ports section.^[600-developer-docker-docker-compose-docker-compouse-02.md] The syntax follows the format "HOST_PORT:CONTAINER_PORT"^[600-developer-docker-docker-compose-docker-compouse-02.md]:
- Host Port: The port number on the local machine where the service will be accessed.
- Container Port: The port number on which the application inside the container is listening.
Example¶
In the following docker-compose.yml configuration for a MySQL database, the ports attribute 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"
With this configuration, the database can be accessed by connecting to localhost:33060, even though the MySQL process inside the container is running on its default port 3306^[600-developer-docker-docker-compose-docker-compouse-02.md].
Related Concepts¶
- Docker Compose
- [[Docker]]
Sources¶
^[600-developer-docker-docker-compose-docker-compouse-02.md]