深度阅读

How to map a port from the Docker container to the host machine?

作者
作者
2023年08月22日
更新时间
10.51 分钟
阅读时间
0
阅读量

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.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。