小编典典

如何为现有Docker容器分配端口映射?

docker

我不确定在这里是否误解了一些东西,但是似乎只能通过从映像创建新容器来设置端口映射。是否可以将端口映射分配给现有Docker容器?


阅读 510

收藏
2020-06-17

共1个答案

小编典典

您可以通过直接在以下位置编辑hostconfig.json文件
来更改端口映射/var/lib/docker/containers/[hash_of_the_container]/hostconfig.json

您可以通过docker inspect <container_name>命令确定[hash_of_the_container] ,“
Id”字段的值为哈希。

1) stop the container 
2) stop docker service (per Tacsiazuma's comment)
3) change the file
4) restart your docker engine (to flush/clear config caches)
5) start the container

因此,您无需使用这种方法来创建图像。您也可以在此处更改重新启动标志。

PS:您可以访问
https://docs.docker.com/engine/admin/,以了解如何根据主机正确重新启动Docker引擎。我曾经sudo systemctl restart docker重启过在Ubuntu 16.04上运行的Docker引擎

2020-06-17