In Docker, network communication between a container and the host system is managed through port mapping. This allows external traffic to reach specific services running inside a containerized environment.\n\n### Port Mapping Fundamentals\nThe primary way to map ports is during the execution of a container using the -p (or --publish) flag. This flag follows the syntax host_port:container_port. When a container provides multiple services or requires separate channels for traffic and management, multiple mapping flags can be used in a single command.\n\n### Declaring Ports in a Dockerfile\nWhile the EXPOSE instruction does not actually publish the ports, it serves as documentation and an instruction for the runtime environment about wich ports the application intends to use.\n\ndockerfile\n# Using a lightweight Nginx image\nFROM nginx:alpine\n\n# Define the primary web port and an auxiliary service port\nEXPOSE 80\nEXPOSE 8080\n\n# Start the Nginx server\nCMD [\"nginx\", \"-g\", \"daemon off;\"]\n\n\n### Executing Multi-Port Containers\nTo instantiate a container with these mappings, chain multiple -p arguments. In the example below, the host directs traffic from port 8081 to the container's port 80, and from port 9090 to the container's port 8080.\n\nbash\n# Build the image from the local Dockerfile\ndocker build -t multi-port-service .\n\n# Run the container with two separate port mappings\ndocker run -d \\\n --name my-running-app \\\n -p 8081:80 \\\n -p 9090:8080 \\\n multi-port-service\n\n\n### Verifying Port Configuration\nOnce the container is active, the mapping status can be inspected using the following command:\n\nbash\ndocker ps\n# Or specifically check the port mappings for a specific container\ndocker port my-running-app\n\n\nThe output will confirm the binding between the host interfaces and the container's internal ports.
Exposing and Mapping Multiple Ports in Docker
Posted on Tue, 01 Sep 2026 16:50:26 +0000 by Ambush Commander