Skip to content

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 (EXPOSE in 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 (-p or --publish flag in docker 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.

The -p (or --publish) flag is used with docker run to map ports.

The most common formats are:

  • -p hostPort:containerPort : Maps containerPort inside the container to hostPort on all interfaces of the Docker host.
  • -p ip:hostPort:containerPort : Maps containerPort to hostPort specifically on the given ip address of the Docker host.
  • -p containerPort : Maps containerPort to 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.

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:lts

After running this, you can access the Jenkins UI by navigating to http://localhost:8080 or http://<docker-host-ip>:8080 in your browser.

If you run multiple instances or don’t need a fixed host port:

# Run Nginx, mapping container port 80 to a random host port
docker 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 port
docker run -d --name specific-random-nginx -p 80 nginx:stable-alpine

To find out which random host port was assigned, use docker ps:

docker ps

Look 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.

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 image
docker inspect jenkins/jenkins:lts

Look 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 mappings
docker inspect myjenkins

Look 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.