How to expose a port on a live Docker container?

Better Stack Team
Updated on April 14, 2023

To expose a port on a live Docker container, you can use the docker container port command. Here are the steps:

  1. To expose a port on a live Docker container, you need to first find the container ID or name. You can use the docker container ls command to list all running containers and find the ID or name of the container you want to expose the port on.

     
    docker container ls
    
  2. Use the docker container port command to expose the port on the container. The syntax is as follows:

     
    docker container port <container> <port>
    

Where <container> is the container ID or name, and <port> is the port you want to expose. For example, to expose port 8080 on a container named mycontainer, you would run:

 
docker container port mycontainer 8080

This command will output the host IP address and port that maps to the container port you specified.

Note: Exposing a port on a live container does not automatically publish it to the host. To publish the port to the host, you need to stop and restart the container with the -p flag, specifying the host port and container port mappings. For example:

 
docker stop mycontainer
docker run -p 8080:8080 myimage

This will stop the container mycontainer, and start a new container based on myimage with the port mapping of 8080:8080, exposing port 8080 to the host.

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