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.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github