dla*_*ced 43 linux networking docker
我非常满意Docker默认给我的IP范围176.17.x.x,所以我不需要创建一个新桥,我只想给我的容器一个静态地址在这个范围内,所以我可以指向客户端浏览器直.我试过用
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
Run Code Online (Sandbox Code Playgroud)
从Dockerfile,它正确填充interfaces文件,但接口本身没有改变.实际上,在容器中运行ifup eth0会出现此错误:
RTNETLINK答案:不允许操作无法启动eth0
can*_*Now 30
我已经在这里回答了这个问题 /sf/answers/2475142981/ 但我现在看到这个问题实际上比上面提到的要早,所以我也会复制答案:
使用Docker 1.10.1版,轻松构建9e83765.
首先,您需要创建自己的docker网络(mynet123)
docker network create --subnet=172.18.0.0/16 mynet123
Run Code Online (Sandbox Code Playgroud)
而不是简单地运行图像(我将以ubuntu为例)
docker run --net mynet123 --ip 172.18.0.22 -it ubuntu bash
Run Code Online (Sandbox Code Playgroud)
然后在ubuntu shell中
ip addr
Run Code Online (Sandbox Code Playgroud)
此外,您可以使用
--hostname指定主机名
--add-host来添加更多条目/etc/hosts
文档(以及您需要创建网络的原因),请访问https://docs.docker.com/engine/reference/commandline/network_create/
小智 13
我用写的方法在这里从官方泊坞窗文件和我已经证实了它的工作原理:
# At one shell, start a container and
# leave its shell idle and running
$ sudo docker run -i -t --rm --net=none base /bin/bash
root@63f36fc01b5f:/#
# At another shell, learn the container process ID
# and create its namespace entry in /var/run/netns/
# for the "ip netns" command we will be using below
$ sudo docker inspect -f '{{.State.Pid}}' 63f36fc01b5f
2778
$ pid=2778
$ sudo mkdir -p /var/run/netns
$ sudo ln -s /proc/$pid/ns/net /var/run/netns/$pid
# Check the bridge's IP address and netmask
$ ip addr show docker0
21: docker0: ...
inet 172.17.42.1/16 scope global docker0
...
# Create a pair of "peer" interfaces A and B,
# bind the A end to the bridge, and bring it up
$ sudo ip link add A type veth peer name B
$ sudo brctl addif docker0 A
$ sudo ip link set A up
# Place B inside the container's network namespace,
# rename to eth0, and activate it with a free IP
$ sudo ip link set B netns $pid
$ sudo ip netns exec $pid ip link set dev B name eth0
$ sudo ip netns exec $pid ip link set eth0 up
$ sudo ip netns exec $pid ip addr add 172.17.42.99/16 dev eth0
$ sudo ip netns exec $pid ip route add default via 172.17.42.1
Run Code Online (Sandbox Code Playgroud)
使用这种方法,我总是使用net = none运行我的容器,并使用外部脚本设置IP地址.
实际上,尽管我最初的失败,@ MarkO'Connor的回答是正确的.我在我的主机/ etc/network/interfaces文件中创建了一个新接口(docker0),在主机上运行了sudo ifup docker0,然后运行
docker run --net=host -i -t ...
Run Code Online (Sandbox Code Playgroud)
它获取静态IP并将其分配给容器中的docker0.
谢谢!
这对我有用:
docker run --cap-add=NET_ADMIN -d -it myimages/image1 /bin/sh -c "/sbin/ip addr add 172.17.0.8 dev eth0; bash"
Run Code Online (Sandbox Code Playgroud)
解释:
--cap-add=NET_ADMIN拥有管理网络的权限(即命令的权限/sbin/ip)
myimages/image1容器的图像
/bin/sh -c "/sbin/ip addr add 172.17.0.8 dev eth0 ; bash"
在容器内运行,ip addr add 172.17.0.8 dev eth0向该容器添加一个新的 IP 地址 172.17.0.8(注意:现在和将来一定要使用免费的 IP 地址)。然后运行 bash,只是为了不让容器自动停止。
奖金:
我的目标场景:设置一个分布式应用程序,其中容器在 dist-app 中扮演不同的角色。“指挥容器”能够自行(在内部)运行 docker 命令,以便根据需要启动和停止容器。每个容器都被配置为知道从哪里连接以访问 dist-app 中的特定角色/容器(因此每个合作伙伴必须知道每个角色的 IP 集)。
去做这个:
使用此 Dockerfile 创建的图像
FROM pin3da/docker-zeromq-node
MAINTAINER Foobar
# install docker software
RUN apt-get -yqq update && apt-get -yqq install docker.io
# export /var/run/docker.sock so we can connect it in the host
VOLUME /var/run/docker.sock
Run Code Online (Sandbox Code Playgroud)
镜像构建命令:
docker build --tag=myimages/conductor --file=Dockerfile .
Run Code Online (Sandbox Code Playgroud)
容器运行命令:
docker run -v /var/run/docker.sock:/var/run/docker.sock --name=conductor1 -d -it myimages/conductor bash
Run Code Online (Sandbox Code Playgroud)
首先(不是绝对必要)添加条目以/etc/hosts通过 IP 或名称查找合作伙伴(选项--add-host)
第二(显然是必需的)为正在运行的容器分配一个ip(
/sbin/ip在其中使用)
docker run --cap-add=NET_ADMIN --add-host worker1:172.17.0.8 --add-host worker2:172.17.0.9 --name=worker1 -h worker1.example.com -d -it myimages/image1 /bin/sh -c "/sbin/ip addr add 172.17.0.8 dev eth0; bash"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
80768 次 |
| 最近记录: |