How to map a port from the Docker container to the host machine?
Published on Aug. 22, 2023, 12:19 p.m.
You can map a port from the Docker container to the host machine by using the -p
or --publish
option in the docker run
command, followed by the mapping between the container port and the host port.
For example, to map port 8080 in the container to port 80 on the host machine, you can run the following command:
docker run -p 80:8080 <image>
Replace <image>
with the name of the Docker image you want to run.
This will publish the container’s port 8080 to port 80 of the host machine. You can replace 80 with any other available port on the host machine. If you want to map the container port to a specific IP address of the host, you can use the syntax IP:HOST_PORT:CONTAINER_PORT
instead. For example:
docker run -p 192.168.1.100:80:8080 <image>
This will publish the container’s port 8080 to port 80 of the host machine with the IP address 192.168.1.100.
Keep in mind that you should not use ports that are already in use on the host machine, and that some ports may require elevated privileges to bind to.