服务器 
首页 > 服务器 > 浏览文章

Docker为网络bridge模式指定容器ip的方法

(编辑:jimmy 日期: 2024/11/26 浏览:3 次 )

前言

众所周知bridge模式是Docker默认的网络设置,此模式会为每一个容器分配Network Namespace、设置IP等,并将一个主机上的Docker容器连接到一个虚拟网桥上。下面来看看Docker为网络bridge模式指定容器ip的方法。

实现方法

如果只是简单创建一个bridge模式的网络是无法给容器指定ip的

[root@vultrvpn conf.d]# docker network create --driver bridge wordpress_net
ad1ff3d972e8bfe6d992a8403a98f46784aa75d5514adbf35ee6ae4528513be7
[root@vultrvpn conf.d]# docker network ls
NETWORK ID   NAME    DRIVER    SCOPE
38fab5c74b87  bridge    bridge    local    
f0cf94dd0c5e  host    host    local    
465a31c55aa1  none    null    local    
ad1ff3d972e8  wordpress_net  bridge    local 

已创建一个wordpress_net的网络,在这个网络模块上创建一个容器,并指定ip

[root@vultrvpn conf.d]# docker run -d --name db_mysql -m 220m --memory-swap 220m -v /data/docker_project/db_mysql/data:/var/lib/mysql -v /data/docker_project/db_mysql/conf:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=qwe123 --network=wordpress_net --ip=172.18.0.2 mysql:5.7
da497e56bd4b0f3434dd4f81534096574d167354dc508b657ceb390ba8e63771
docker: Error response from daemon: User specified IP address is supported only when connecting to networks with user configured subnets.

提示了一个错误,若要配置ip,则需要给这个网络指定一个subnet。所以重新搞一个网络。

docker network create --driver bridge --subnet 172.25.0.0/16 wordpress_net


[root@vultrvpn conf.d]# docker network inspect wordpress_net 
[
 {
  "Name": "wordpress_net",
  "Id": "925e64493df9b674ef9bdadaae380c8a335aa6353e48b447cb5d910df19c3a3e",
  "Scope": "local",
  "Driver": "bridge",
  "EnableIPv6": false,
  "IPAM": {
   "Driver": "default",
   "Options": {},
   "Config": [
    {
     "Subnet": "172.25.0.0/16"
    }
   ]
  },
  "Internal": false,
  "Containers": {},
  "Options": {},
  "Labels": {}
 }
]

在给容器指定一个ip

[root@vultrvpn conf.d]# docker run -d --name my_vpn -m 100m --memory-swap 100m -e WORDPRESS_DB_HOST=172.25.0.2 -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=qwe123 -e WORDPRESS_DB_NAME=myvpn -p 8001:80 --network=wordpress_net wordpress:4.6
096518d84419476f71647a8c70c91c3f336ef8772af50672b88a9c1fd7b3a719
[root@vultrvpn conf.d]# docker ps -a
CONTAINER ID  IMAGE     COMMAND     CREATED    STATUS      PORTS     NAMES
096518d84419  wordpress:4.6   "/entrypoint.sh apach" 47 seconds ago  Up 47 seconds    0.0.0.0:8001->80/tcp my_vpn
6d23ed99db20  wordpress:4.6   "/entrypoint.sh apach" About a minute ago Up About a minute   0.0.0.0:8000->80/tcp my_wordpress
ade42edf5a93  mysql:5.7    "docker-entrypoint.sh" 3 minutes ago  Up 3 minutes    3306/tcp    db_mysql

正常创建

查看网卡信息

[root@vultrvpn conf.d]# docker network inspect wordpress_net 
[
 {
  "Name": "wordpress_net",
  "Id": "925e64493df9b674ef9bdadaae380c8a335aa6353e48b447cb5d910df19c3a3e",
  "Scope": "local",
  "Driver": "bridge",
  "EnableIPv6": false,
  "IPAM": {
   "Driver": "default",
   "Options": {},
   "Config": [
    {
     "Subnet": "172.25.0.0/16"
    }
   ]
  },
  "Internal": false,
  "Containers": {
   "096518d84419476f71647a8c70c91c3f336ef8772af50672b88a9c1fd7b3a719": {
    "Name": "my_vpn",
    "EndpointID": "e44636c2b3a49d4aedaad68cd6ca17aea3a9fe4f71afecb27219a6412701536a",
    "MacAddress": "02:42:ac:19:00:04",
    "IPv4Address": "172.25.0.4/16",
    "IPv6Address": ""
   },
   "6d23ed99db20f17d99dd5dcf6cc65f5ced8cb936917dee044914cf278179150c": {
    "Name": "my_wordpress",
    "EndpointID": "3c765d5953428c1d66be584eb0d34b58f6eee3f598e3a37263d54d77e2b87263",
    "MacAddress": "02:42:ac:19:00:03",
    "IPv4Address": "172.25.0.3/16",
    "IPv6Address": ""
   },
   "ade42edf5a934b02f7bb741bd5a45a282a50d3a49e9f1866579e8f93fef22a0e": {
    "Name": "db_mysql",
    "EndpointID": "2322bfb9da72b3b6db68d59cea97172e66b960ca5b782bd2fa7d09f9c93f0288",
    "MacAddress": "02:42:ac:19:00:02",
    "IPv4Address": "172.25.0.2/16",
    "IPv6Address": ""
   }
  },
  "Options": {},
  "Labels": {}
 }
]

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家能有所帮助,如果有疑问大家可以留言交流。

上一篇:Tomcat容器管理安全的验证方式汇总
下一篇:3分钟用Docker搭建一个Minecraft服务器
一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap