如何将端口映射分配给现有的 Docker 容器? [英] How do I assign a port mapping to an existing Docker container?

查看:36
本文介绍了如何将端口映射分配给现有的 Docker 容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我是否误解了这里的某些内容,但似乎只能通过从图像创建新容器来设置端口映射.有没有办法将端口映射分配给现有的 Docker 容器?

I'm not sure if I've misunderstood something here, but it seems like it's only possible to set port mappings by creating a new container from an image. Is there a way to assign a port mapping to an existing Docker container?

推荐答案

您可以通过直接编辑位于以下位置的 hostconfig.json 文件来更改端口映射/var/lib/docker/containers/[hash_of_the_container]/hostconfig.json/var/snap/docker/common/var-lib-docker/containers/[hash_of_the_container]/hostconfig.json,我相信,如果你安装了 Docker.

You can change the port mapping by directly editing the hostconfig.json file at /var/lib/docker/containers/[hash_of_the_container]/hostconfig.json or /var/snap/docker/common/var-lib-docker/containers/[hash_of_the_container]/hostconfig.json, I believe, if You installed Docker as a snap.

您可以通过 docker inspect 命令和Id"的值来确定 [hash_of_the_container]字段是哈希值.

You can determine the [hash_of_the_container] via the docker inspect <container_name> command and the value of the "Id" field is the hash.

  1. 停止容器(docker stop ).
  2. 停止 docker 服务(根据 Tacsiazuma 的评论)
  3. 更改文件.
  4. 重新启动您的 docker 引擎(以刷新/清除配置缓存).
  5. 启动容器(docker start ).

因此您不需要使用这种方法创建图像.您也可以在此处更改重启标志.

So you don't need to create an image with this approach. You can also change the restart flag here.

P.S.您可以访问 https://docs.docker.com/engine/admin/ 了解如何根据您的主机正确重新启动您的 docker 引擎.我使用 sudo systemctl restart docker 重新启动在 Ubuntu 16.04 上运行的 docker 引擎.

P.S. You may visit https://docs.docker.com/engine/admin/ to learn how to correctly restart your docker engine as per your host machine. I used sudo systemctl restart docker to restart my docker engine that is running on Ubuntu 16.04.

这篇关于如何将端口映射分配给现有的 Docker 容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆