How to assign a port mapping to an existing Docker container?

Better Stack Team
Updated on April 7, 2023

To assign a port mapping to an existing Docker container, you can use the "docker container update" command with the "--publish" or "-p" option.

The syntax for the command is as follows:

 
docker container update --publish <host_port>:<container_port> <container_name_or_id>

For example, to map port 8080 on the host system to port 80 in a container named "web-server", you can run the following command:

 
docker container update --publish 8080:80 web-server

This will create a new network mapping between the specified host port and the container's port, and the container will start forwarding traffic from the host system to the container.

Note that if the container is already running, you will need to stop and start it again for the new port mapping to take effect:

 
docker container stop web-server
docker container start web-server

Alternatively, you can use the "docker container restart" command to stop and start the container in a single step:

 
docker container restart web-server

After the container is restarted, it will be accessible on the new host port specified in the port mapping.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

Reliability is the
ultimate feature

Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.

Explore Better Stack