如何在Docker容器中设置静态IP地址? [英] How can I set a static IP address in a Docker container?

查看:1905
本文介绍了如何在Docker容器中设置静态IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Docker在默认情况下给我的IP范围(176.17.xx)感到非常满意,所以我不需要创建一个新的桥,我只想给我的容器一个静态地址范围内,所以我可以直接将客户端浏览器指向它。
我尝试使用

  RUN echoauto eth0>> / etc / network / interfaces 

RUN echoiface eth0 inet static>> / etc / network / interfaces

RUN echoaddress 176.17.0.250>> / etc / network / interfaces

RUN echonetmask 255.255.0.0>> / etc / network / interfaces

运行ifdown eth0

运行ifup eth0

从一个Docker文件,并正确地填充了interfaces文件,但接口本身没有改变。事实上,在容器内运行ifup eth0可以得到这个错误:



RTNETLINK答案:不允许操作

无法启动eth0




解决方案

我已经在这里回答了
https://stackoverflow.com/a/35359185 / 4094678
但是我现在看到,这个问题实际上比上述的更旧,所以我将复制答案:



首先您需要创建自己的码头网络(mynet123)

  docker network create --subnet = 172.18.0.0 / 16 mynet123 

不仅仅是运行图像(我以ubuntu为例)

  docker run --net mynet123  - ip 172.18.0.22 -it ubuntu bash 

然后在ubuntu shell

  ip addr 
pre>

此外,您可以使用
--hostname来指定一个主机名
--add-host来添加更多条目到/ etc / hosts <在

rel =nofollow noreferrer> https://docs.docker.com/engine/reference/commandline/network_create/


I'm perfectly happy with the IP range that docker is giving me by default (176.17.x.x), so I don't need to create a new bridge, I just want to give my containers a static address within that range so I can point client browsers to it directly. I tried using

RUN echo "auto eth0" >> /etc/network/interfaces

RUN echo "iface eth0 inet static" >> /etc/network/interfaces

RUN echo "address 176.17.0.250" >> /etc/network/interfaces

RUN echo "netmask 255.255.0.0" >> /etc/network/interfaces

RUN ifdown eth0

RUN ifup eth0

from a Dockerfile, and it properly populated the interfaces file, but the interface itself didn't change. In fact, running ifup eth0 within the container gets this error:

RTNETLINK answers: Operation not permitted

Failed to bring up eth0

解决方案

I have already answered this here https://stackoverflow.com/a/35359185/4094678 but I see now that this question is actually older then the aforementioned one, so I'll copy the answer as well:

Easy with Docker version 1.10.1, build 9e83765.

First you need to create you own docker network (mynet123)

docker network create --subnet=172.18.0.0/16 mynet123

than simply run the image (I'll take ubuntu as example)

docker run --net mynet123 --ip 172.18.0.22 -it ubuntu bash

then in ubuntu shell

ip addr

Additionally you could use --hostname to specify a hostname --add-host to add more entries to /etc/hosts

Docs (and why you need to create a network) at https://docs.docker.com/engine/reference/commandline/network_create/

这篇关于如何在Docker容器中设置静态IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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