Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Cloud Native 6 min read

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

Container 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.json

Or change to the container directory first:

cd /var/lib/docker/containers/{hash_of_the_container}*
vim hostconfig.json

Modify 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
--rm

removes 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).

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

dockermacOSContainer ConfigurationHostconfig

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion