Easily Add or Modify Docker Container Port Mappings on macOS
Learn step‑by‑step how to expose new ports or modify existing ones for Docker containers on macOS, including using the -p flag during container creation, editing hostconfig.json and config files inside the Docker VM, and restarting Docker to apply changes.
Container Not Started?
If your container hasn't been built yet and you want to add port mappings, include the -p option when creating the container; add one -p for each port you wish to map. 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.0Container Already Running?
To modify or add ports you must first stop the running container.
All commands below use container id cbe26510c276 ; replace it with your own container id.
docker stop {container_name_or_id}Example: docker stop cbe26510c276 Find the full container hash: docker inspect {container_name_or_id} | grep Id Then edit the hostconfig.json file located at:
vim /var/lib/docker/containers/{hash_of_the_container}/hostconfig.jsonOr change to the container directory first:
cd /var/lib/docker/containers/{hash_of_the_container}* vim hostconfig.jsonModify hostconfig.json
In hostconfig.json locate the "PortBindings":{} entry and add the desired mappings. For example, map host ports 8502 and 8505 to container ports 8502 and 8505:
{
"PortBindings": {
"8502/tcp": [
{
"HostIp": "",
"HostPort": "8502"
}
],
"8505/tcp": [
{
"HostIp": "",
"HostPort": "8505"
}
]
}
}Usually the host port should match the container port for easier management.
If config.v2.json or config.json also contain port definitions, edit the "ExposedPorts" section accordingly:
{
"Config": {
"ExposedPorts": {
"8502/tcp": {},
"8505/tcp": {}
},
"Entrypoint": ["/bin/sh"]
}
}Restart Docker and verify the changes:
# Restart Docker
service docker restart
# or
systemctl restart docker docker inspect {container_name_or_id}Finally start the container again:
docker start {container_name_or_id}Docker Desktop for Mac
How to log into the VM of Docker Desktop for Mac
Docker Desktop runs containers inside a Linux VM. The VM path can be found in the Docker Desktop settings under “Disk image location”. The simplest way to enter the VM is to run the tiny justincormack/nsenter1 image:
docker run -it --rm --privileged --pid=host justincormack/nsenter1 --rmremoves the container on exit. --privileged grants access to host devices. --pid=host shares the host’s process namespace.
Then navigate to /var/lib/docker/containers and edit config.v2.json and hostconfig.json with vi (the image does not include vim).
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
