Docker Ports: Configuration and Usage

watch 3m, 2s
views 2

09:45, 01.06.2026

Article Content
arrow

  • Introduction
  • Why Port Mapping is Important
  • How to Add a New Port Mapping to a Running Container
  • Restarting the Container with Updated Settings
  • Steps:
  • Restarting Using Docker Commit
  • Steps:
  • How to Modify Docker Settings on the Fly
  • 1. Stopping the Container and Docker Service
  • 2. Locating the Configuration Files
  • 3. Making Changes to the Configuration
  • 4. Verifying the Updated Configuration
  • 5. Applying New Port Mapping to a Running Container
  • Conclusion

Introduction

Docker has revolutionized how developers build, deploy, and manage applications by using containerization. One critical component of working with Docker containers is managing ports, which are necessary for communication between your containerized services and the outside world. Whether you're exposing a web server on port 80 or a custom app on 8080, understanding Docker port configuration is vital for effective container orchestration.

This article walks you through port mapping in Docker — what it is, why it matters, and how to configure it, even for running containers.

Why Port Mapping is Important

Containers are isolated from the host system by default. When a containerized application listens on a specific port internally, that port must be explicitly mapped to a port on the host machine to be accessible externally.

Example: docker run -p 8080:80 nginx

This maps port 80 inside the container to port 8080 on the host, making the NGINX server accessible via localhost:8080.

Without port mapping:

  • Applications remain inaccessible from outside the Docker network.
  • Services can’t communicate across containers or systems.
  • Development and debugging become more difficult.

How to Add a New Port Mapping to a Running Container

Docker does not support adding new port mappings to an already running container directly. The -p flag only works during the container creation phase (docker run).

To expose a new port:

  • You must recreate the container with the new port configuration.
  • Alternatively, use a workaround like using Docker commit and restarting (covered below).

Restarting the Container with Updated Settings

If you want to update the port mapping:

Steps:

  1. Stop the container:
    docker stop <container_name_or_id>
  2. Remove the container (data remains intact if volumes are used):
    docker rm <container_name_or_id>
  3. Recreate it with new port mappings:
    docker run -d -p 8080:80 --name my-nginx nginx Make sure you preserve any necessary volumes, environment variables, or networks when recreating the container.

Restarting Using Docker Commit

If the running container has custom configurations you don’t want to lose:

Steps:

  1. Commit the container state to a new image:
    docker commit <container_id> myimage:withports
  2. Run a new container from the committed image with updated ports:
    docker run -d -p 8080:80 --name new_container myimage:withports This method lets you retain the current container’s filesystem while exposing new ports.

How to Modify Docker Settings on the Fly

While Docker doesn’t allow live port changes to containers, you can make broader Docker configuration changes for future container behavior or debugging purposes.

1. Stopping the Container and Docker Service

Stop the container and then the Docker daemon (may require sudo):

docker stop <container_name> sudo systemctl stop docker

2. Locating the Configuration Files

Docker settings are often located in:

  • Linux: /etc/docker/daemon.json
  • Windows/Mac (Docker Desktop): Through GUI or ~/.docker/config.json

3. Making Changes to the Configuration

Edit daemon.json to define custom port mappings or experimental features.

Example: 


"iptables": true, 
"default-address-pools": [ 

"base": "10.10.0.0/16", 
"size": 24


}

Note: Port mappings aren’t directly modified here, but other networking behaviors can be configured.

4. Verifying the Updated Configuration

Check for syntax issues:

sudo dockerd --config-file /etc/docker/daemon.json --debug

If successful, restart the Docker service:

sudo systemctl start docker

5. Applying New Port Mapping to a Running Container

After restarting Docker:

  • Create a new container with the desired port mapping.
  • Use docker exec, docker cp, or volume mounts to transfer data from the old container if needed.

Conclusion

Docker port mapping is foundational to deploying accessible, networked applications. While Docker doesn't allow modifying ports on the fly for active containers, there are reliable workflows to reconfigure containers with new settings, including committing containers and recreating them. Understanding these practices ensures you maintain uptime, accessibility, and scalability in containerized environments.

Share

Was this article helpful to you?

VPS popular offers

-10%

CPU
CPU
4 Epyc Cores
RAM
RAM
4 GB
Space
Space
50 GB NVMe
Bandwidth
Bandwidth
Unlimited
Keitaro KVM 4096
OS
CentOS
Software
Software
Keitaro

18.1 /mo

/mo

Billed annually

-10%

CPU
CPU
4 Xeon Cores
RAM
RAM
4 GB
Space
Space
100 GB SSD
Bandwidth
Bandwidth
Unlimited
wKVM-SSD 4096 Windows

18.65 /mo

/mo

Billed annually

-15%

CPU
CPU
6 Xeon Cores
RAM
RAM
8 GB
Space
Space
100 GB SSD
Bandwidth
Bandwidth
80 Mbps
DDoS Protected SSD-wKVM 8192 Windows

101 /mo

/mo

Billed annually

-10%

CPU
CPU
4 Xeon Cores
RAM
RAM
4 GB
Space
Space
100 GB HDD
Bandwidth
Bandwidth
Unlimited
KVM-HDD 4096 Linux

15 /mo

/mo

Billed annually

-9.9%

CPU
CPU
3 Xeon Cores
RAM
RAM
1 GB
Space
Space
40 GB HDD
Bandwidth
Bandwidth
300 Gb
KVM-HDD HK 1024 Linux

4.96 /mo

/mo

Billed annually

-10%

CPU
CPU
2 Epyc Cores
RAM
RAM
1 GB
Space
Space
10 GB NVMe
Bandwidth
Bandwidth
Unlimited
KVM-NVMe 1024 Linux

7.1 /mo

/mo

Billed annually

-15.4%

CPU
CPU
6 Xeon Cores
RAM
RAM
16 GB
Space
Space
150 GB SSD
Bandwidth
Bandwidth
100 Mbps
DDoS Protected SSD-wKVM 16384 Windows

130 /mo

/mo

Billed annually

-15.3%

CPU
CPU
4 Xeon Cores
RAM
RAM
2 GB
Space
Space
75 GB SSD
Bandwidth
Bandwidth
40 Mbps
DDoS Protected SSD-wKVM 2048 Windows

54 /mo

/mo

Billed annually

-10%

CPU
CPU
6 Xeon Cores
RAM
RAM
16 GB
Space
Space
400 GB HDD
Bandwidth
Bandwidth
Unlimited
KVM-HDD 16384 Linux

50 /mo

/mo

Billed annually

-10%

CPU
CPU
4 Xeon Cores
RAM
RAM
4 GB
Space
Space
100 GB HDD
Bandwidth
Bandwidth
300 Gb
KVM-HDD HK 4096 Linux

12.07 /mo

/mo

Billed annually

Other articles on this topic

cookie

Accept cookies & privacy policy?

We use cookies to ensure that we give you the best experience on our website. If you continue without changing your settings, we'll assume that you are happy to receive all cookies on the HostZealot website.