Operations 6 min read

How to Add or Modify Docker Container Port Mappings After Creation

This guide explains step‑by‑step how to add, change, or remove port mappings for Docker containers by stopping the container, editing hostconfig.json (and optionally config.v2.json), and restarting the container, with commands for both Linux and Docker Desktop for Mac.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Add or Modify Docker Container Port Mappings After Creation

If a container hasn't been built yet, you can add port mappings during creation by using multiple -p options in the docker run command, for example:

docker run --name api_dfo_hyperf_ws \
  -v /Users/pudongping/glory/codes/dfo/api_dfo_hyperf:/api_dfo_hyperf \
  -p 9502:9502 \
  -p 9503:9503 \
  -p 9504:9504 \
  -p 9505:9505 \
  -it \
  --entrypoint /bin/sh \
  alex/alex_api_dfo:v1.0

To modify or add ports on an already running container, first stop it: docker stop <container_id_or_name> Find the full container hash with:

docker inspect <container_id_or_name> | grep Id

Open the container's hostconfig.json (path: /var/lib/docker/containers/<hash>/hostconfig.json) using vim or vi. If you prefer, cd into the container directory first: cd /var/lib/docker/containers/<hash>* In hostconfig.json, locate the "PortBindings":{} object and add the desired mappings, e.g.:

{
  "PortBindings": {
    "8502/tcp": [{ "HostIp": "", "HostPort": "8502" }],
    "8505/tcp": [{ "HostIp": "", "HostPort": "8505" }]
  }
}

Also update "ExposedPorts" in config.v2.json or config.json if those files contain port entries:

{
  "Config": {
    "ExposedPorts": {
      "8502/tcp": {},
      "8505/tcp": {}
    },
    "Entrypoint": ["/bin/sh"]
  }
}

After editing, restart Docker and verify the changes:

# Restart Docker
service docker restart
# or
systemctl restart docker

# Inspect the container to confirm
docker inspect <container_id_or_name>

Finally, start the container again:

docker start <container_id_or_name>

Docker Desktop for Mac

On macOS, containers run inside a VM, so you need to enter that VM to edit the files. Use the tiny justincormack/nsenter1 image:

docker run -it --rm --privileged --pid=host justincormack/nsenter1
--rm

removes the helper container on exit. --privileged grants access to host devices. --pid=host shares the host's PID namespace, allowing you to see host processes.

Once inside the VM, navigate to /var/lib/docker/containers and edit hostconfig.json and, if needed, config.v2.json using vi (the image lacks vim).

vi /var/lib/docker/containers/<hash>/hostconfig.json
vi /var/lib/docker/containers/<hash>/config.v2.json

After modifications, restart Docker on macOS and verify the container configuration as described above.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerDevOpsLinuxmacOSport mappingContainer Configuration
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.