Home >Operation and Maintenance >Docker >How to change docker container port
We know that docker run can specify port mapping, but once the container is generated, there is no command that can be modified directly. Usually the indirect method is to save the image, create a new container, and specify the new port mapping when creating it.
(Recommended tutorial: docker tutorial)
So is there any way to directly modify the existing container without saving the image?
The specific method is as follows:
1) Stop the container
2) Stop the docker service (systemctl stop docker)
3) Modify the hostconfig of this container. The port in the json file (someone mentioned in the original post that if the port is also recorded in config.v2.json, it must also be modified)
cd /var/lib/docker/3b6ef264a040* #这里是CONTAINER ID vi hostconfig.json #如果之前没有端口映射, 应该有这样的一段: "PortBindings":{} #增加一个映射, 这样写: "PortBindings":{"3306/tcp":[{"HostIp":"","HostPort":"3307"}]} #前一个数字是容器端口, 后一个是宿主机端口. #而修改现有端口映射更简单, 把端口号改掉就行.
4) Start the docker service (systemctl start docker)
5) Start container
The above is the detailed content of How to change docker container port. For more information, please follow other related articles on the PHP Chinese website!