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

本文翻译自:How do I assign a port mapping to an existing Docker container?

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? 是否可以将端口映射分配给现有Docker容器?


#1楼

参考:https://stackoom.com/question/1J820/如何为现有Docker容器分配端口映射


#2楼

Not sure if you can apply port mapping a running container. 不确定是否可以应用端口映射正在运行的容器。 You can apply port forwarding while running a container which is different than creating a new container. 您可以在运行与创建新容器不同的容器时应用端口转发。

$ docker run -p <public_port>:<private_port> -d <image>  

will start running container. 将开始运行容器。 This tutorial explains port redirection. 本教程介绍了端口重定向。


#3楼

If by "existing" you mean "running", then it's not (currently) possible to add a port mapping. 如果通过“现有”来表示“正在运行”,那么(当前)无法添加端口映射。

You can, however, dynamically add a new network interface with eg Pipework , if you need to expose a service in a running container without stopping/restarting it. 但是,如果需要在不停止/重新启动服务的情况下将服务公开到正在运行的容器中,则可以使用Pipework动态添加新的网络接口。


#4楼

I'm also interested in this problem. 我对这个问题也很感兴趣。

As @Thasmo mentioned, port forwardings can be specified ONLY with docker run command. @Thasmo所述,只能使用docker run命令指定端口转发。
Other commands, docker start does not have -p option and docker port only displays current forwardings. 其他命令, docker start没有-p选项, docker port仅显示当前转发。

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. 注意:上面的test02是我从test01容器构造的新映像。

  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. 其中第一个8080是本地端口,第二个8080是容器端口。


#5楼

In Fujimoto Youichi's example test01 is a container, whereas test02 is an image. 在Fujimoto test01的示例中, test01是一个容器,而test02是一个图像。

Before doing docker run you can remove the original container and then assign the container the same name again: 在执行docker run之前,您可以删除原始容器,然后再次为该容器分配相同的名称:

$ docker stop container01
$ docker commit container01 image01
$ docker rm container01
$ docker run -d -P --name container01 image01

(Using -P to expose ports to random ports rather than manually assigning). (使用-P将端口暴露给随机端口,而不是手动分配)。


#6楼

If you simply want to change the port of the running container, you do: 如果只想更改正在运行的容器的端口,则可以执行以下操作:

  1. stop existing container 停止现有容器

    sudo docker stop NAME sudo docker停止NAME

  2. now restart with the new port mapping 现在使用新的端口映射重新启动

    sudo docker run -d -p 81:80 NAME sudo docker run -d -p 81:80名称

where as: 其中:

"-d" to background / deamon the docker “ -d”到后台/守护docker

"-p" enable port mapping “ -p”启用端口映射

"81" external (exposed) port you use to access with your browser 用于通过浏览器访问的“ 81”个外部(公开)端口

"80" internal docker container listen port “ 80”内部Docker容器侦听端口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值