Docker - Managing Ports
Docker - Managing Ports
Section titled “Docker - Managing Ports”Applications running inside Docker containers often need to expose services on specific network ports (e.g., a web server on port 80, a database on port 3306). To access these services from the Docker host or external networks, you need to map ports from the host machine to the ports inside the container.
Exposing vs. Publishing Ports
Section titled “Exposing vs. Publishing Ports”- Exposing (
EXPOSEin Dockerfile): This instruction acts as documentation. It informs users of the image which ports the application intends to listen on. It does not make the port accessible from the host. - Publishing (
-por--publishflag indocker run): This actively maps a port on the Docker host to a port inside the container. This is what makes the container’s service accessible externally.
Publishing Ports with docker run
Section titled “Publishing Ports with docker run”The -p (or --publish) flag is used with docker run to map ports.
Syntax
Section titled “Syntax”The most common formats are:
-p hostPort:containerPort: MapscontainerPortinside the container tohostPorton all interfaces of the Docker host.-p ip:hostPort:containerPort: MapscontainerPorttohostPortspecifically on the givenipaddress of the Docker host.-p containerPort: MapscontainerPortto a randomly chosen available high port on the Docker host. Useful when you don’t care about the specific host port.-p hostPort:containerPort/udp: Specifies UDP protocol instead of the default TCP.
Example: Running Jenkins
Section titled “Example: Running Jenkins”The official Jenkins image (jenkins/jenkins:lts) typically exposes port 8080 for the web UI and port 50000 for agent communication.
# Run Jenkins, mapping host 8080 to container 8080 (TCP)# and host 50000 to container 50000 (TCP)docker run -d --name myjenkins \ -p 8080:8080 \ -p 50000:50000 \ jenkins/jenkins:ltsAfter running this, you can access the Jenkins UI by navigating to http://localhost:8080 or http://<docker-host-ip>:8080 in your browser.
Example: Random Host Port
Section titled “Example: Random Host Port”If you run multiple instances or don’t need a fixed host port:
# Run Nginx, mapping container port 80 to a random host portdocker run -d --name random-port-nginx -P nginx:stable-alpine# Note the capital -P publishes ALL exposed ports to random host ports
# Or map a specific container port to a random host portdocker run -d --name specific-random-nginx -p 80 nginx:stable-alpineTo find out which random host port was assigned, use docker ps:
docker psLook at the PORTS column for the container (e.g., 0.0.0.0:32768->80/tcp). You would then access the service via http://localhost:32768.
Finding Exposed Ports (docker inspect)
Section titled “Finding Exposed Ports (docker inspect)”While the image documentation (e.g., on Docker Hub) usually lists exposed ports, you can also find them by inspecting the image or a running container.
# Inspect the Jenkins imagedocker inspect jenkins/jenkins:ltsLook for the "ExposedPorts" key within the "ContainerConfig" or "Config" sections of the JSON output. This shows ports defined by EXPOSE in the Dockerfile.
# Inspect a running container to see actual mappingsdocker inspect myjenkinsLook for the "Ports" key within the "NetworkSettings" section to see the actual host port mappings.
Properly managing port mappings is essential for making containerized services accessible.