How to access host port from docker container?

Better Stack Team
Updated on April 7, 2023

To access a port on the host machine from inside a Docker container, you can use the -p option when running the container to map the host port to a port in the container. For example, if you want to access port 8080 on the host machine from a container, you can use the following command:

docker run -p 8080:8080 my-image

This will map port 8080 on the host machine to port 8080 in the container. From inside the container, you can then access the service running on port 8080 on the host machine using the host machine's IP address or hostname. Note that the service running on the host machine must be configured to listen on the host port.

For example, if you have a web server running on port 8080 on the host machine, you can access it from inside the container using the following URL:


Note that if you are running Docker in a virtual machine, you may need to configure port forwarding or network settings in the virtual machine to allow access to the host machine's ports. Check the documentation for your virtualization software for more information.

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