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

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

问题描述

我不知道我是否误解了这里的东西,但似乎只能通过从图像创建一个新的容器来设置端口映射。有没有办法将端口映射分配给现有的Docker容器?

解决方案

我也对这个问题感兴趣。 / p>

如@Thasmo所述,端口转发只能使用'docker run'命令指定。

其他命令'docker start'没有 - p选项和'docker端口'仅显示当前转发。



要添加端口转发,我总是按照这些步骤,


  1. 停止运行容器

      docker stop test01 


  2. 提交容器

      docker commit test01 test02 

    注意: strong>以上, test02 是从 test01 容器中构建的新图像。 / p>


  3. 运行从被提交的图像

      docker run -p 8080:8080 -td test02 
    pre>

第一个8080是本地端口,第二个8080是容器端口。


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?

解决方案

I'm also interested in this problem.

As @Thasmo mentioned, port forwardings can be specified ONLY with 'docker run' command.
Other commands, 'docker start' does not have -p option and 'docker port' only displays current forwardings.

To add port forwardings, I always follow these steps,

  1. stop running container

    docker stop test01
    

  2. commit the container

    docker commit test01 test02
    

    NOTE: The above, test02 is a new image that I'm constructing from the test01 container.

  3. re-run from the commited image

    docker run -p 8080:8080 -td test02
    

Where the first 8080 is the local port and the second 8080 is the container port.

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

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