Docker创建容器时默认采用bridge网络,自行分配ip,不允许自己指定。在实际部署中,我们需要指定容器ip,不允许其自行分配ip,尤其是搭建集群时,固定ip是必须的。我们可以创建自己的bridge网络 : mynet,创建容器的时候指定网络为mynet并指定ip即可。

1.查看网络模式

docker network ls

2.创建一个新的bridge网络

docker network create --driver bridge --subnet=172.18.12.0/16 --gateway=172.18.1.1 mynet

3.查看网络信息

docker network inspect mynet

4.创建容器并指定容器ip

docker run -e TZ="Asia/Shanghai" --privileged -itd -h hadoop01.com --name hadoop01 --network=mynet --ip 172.18.12.1 centos /bin/bash

解释说明:--privileged (可以有很多权限)

-e TZ="Asia/Shanghai" (时区)

-h hadoop01.com (主机名)

--name hadoop01 (容器名字)

-i :开启标准输入

-it :合起来实现和容器交互的作用,运行一个交互式会话 shell

-d : 后台运行。

解释说明2 :指定端口

docker run -e TZ="Asia/Shanghai" -p 6001:22 --privileged -itd -h hadoop3 --name hadoop3 --network=mynet --ip 172.18.12.3 centos /usr/sbin/init

5.运行容器

docker exec -it hadoop3 /bin/bash

6.centos最小化安装没有ifconfig命令,可通过yum进行安装

yum install -y net-tools

7.安装ssh服务

yum install -y openssh-server

yum install -y openssh-clients

systemctl start sshd

8.新增非root用户

yum install passwd.x86_64

useradd brock

passwd brock

9.xshell连接指定端口的容器

docker 获取容器ip

Docker provides the ability to package and run an application in a loosely isolated environment called a container.

Docker提供了在松散隔离的环境(称为容器)中打包和运行应用程序的功能。

I know what you might be thinking – come on, not another post explaining what Docker is, it's everywhere these days!

我知道您可能在想什么-来吧,这是无处不在的另一篇解释Docker是什么的文章!

But don't worry, we are skipping that basic introduction. The target audience for this article should already have a basic understanding of what Docker and Containers are.

但是不用担心,我们将跳过该基本介绍。 本文的目标读者应该已经对Docker和Containers有基本的了解。

But have you ever wondered how to get a Docker Container IP Address?

但是您是否想知道如何获取Docker容器IP地址?

Docker网络解释 (Docker network explained)

First, let's understand how the Docker network works. For that we are going to focus on the default bridge network. When you are using Docker, if you don’t specify a driver this is the type of network you are using.

首先,让我们了解Docker网络的工作方式。 为此,我们将重点关注默认bridge网络。 使用Docker时,如果未指定驱动程序,则这是您使用的网络类型。

The bridge network works as a private network internal to the host so containers on it can communicate. External access is granted by exposing ports to containers.

bridge网络充当主机内部的专用网络,因此其上的容器可以进行通信。 通过将端口暴露给容器来授予外部访问权限。

Bridge networks are used when your applications run in standalone containers that need to communicate.

当您的应用程序在需要通信的独立容器中运行时,将使用网桥网络。

In the picture above db and web can communicate with each other on a user created bridge network called mybridge.

在上图中, dbweb可以在用户创建的名为mybridge桥接网络上相互通信。

If you’ve never added a network in Docker you should see something similar to this:

如果您从未在Docker中添加网络,则应该看到类似以下内容:


  1. $ docker network ls

  2. NETWORK ID NAME DRIVER SCOPE

  3. c3cd46f397ce bridge bridge local

  4. ad4e4c24568e host host local

  5. 1c69593fc6ac none null local

The default bridge network is listed, along with host and none.  We will ignore the other two, and use the bridge network when we get to the examples.

列出了默认的bridge网络,以及hostnone 。 我们将忽略其他两个,并在获得示例时使用bridge网络。

Docker容器IP地址 (Docker Container IP Address)

By default, the container is assigned an IP address for every Docker network it connects to. And each network is created with a default subnet mask, using it as a pool later on to give away the IP addresses.

默认情况下,为容器连接到的每个Docker网络分配一个IP地址。 而且,每个网络都是使用默认子网掩码创建的,以后将其用作池来分配IP地址。

Usually Docker uses the default 172.17. 0.0/16 subnet for container networking.

通常,Docker使用默认值172.17。 容器网络的0.0 / 16子网。

Now to better understand it, we will execute a real use case.

现在,为了更好地理解它,我们将执行一个实际的用例。

Docker示例 (Docker Example)

To illustrate this, we will use a Hive and Hadoop environment, containing 5 Docker Containers.

为了说明这一点,我们将使用包含5个Docker容器的Hive和Hadoop环境。

Check out the docker-compose.yml file we are about to execute:

docker-compose.yml出我们将要执行docker-compose.yml文件:


  1. version: "3"

  2. services:

  3. namenode:

  4. image: bde2020/hadoop-namenode:2.0.0-hadoop2.7.4-java8

  5. volumes:

  6. - namenode:/hadoop/dfs/name

  7. environment:

  8. - CLUSTER_NAME=test

  9. env_file:

  10. - ./hadoop-hive.env

  11. ports:

  12. - "50070:50070"

  13. datanode:

  14. image: bde2020/hadoop-datanode:2.0.0-hadoop2.7.4-java8

  15. volumes:

  16. - datanode:/hadoop/dfs/data

  17. env_file:

  18. - ./hadoop-hive.env

  19. environment:

  20. SERVICE_PRECONDITION: "namenode:50070"

  21. ports:

  22. - "50075:50075"

  23. hive-server:

  24. image: bde2020/hive:2.3.2-postgresql-metastore

  25. env_file:

  26. - ./hadoop-hive.env

  27. environment:

  28. HIVE_CORE_CONF_javax_jdo_option_ConnectionURL: "jdbc:postgresql://hive-metastore/metastore"

  29. SERVICE_PRECONDITION: "hive-metastore:9083"

  30. ports:

  31. - "10000:10000"

  32. hive-metastore:

  33. image: bde2020/hive:2.3.2-postgresql-metastore

  34. env_file:

  35. - ./hadoop-hive.env

  36. command: /opt/hive/bin/hive --service metastore

  37. environment:

  38. SERVICE_PRECONDITION: "namenode:50070 datanode:50075 hive-metastore-postgresql:5432"

  39. ports:

  40. - "9083:9083"

  41. hive-metastore-postgresql:

  42. image: bde2020/hive-metastore-postgresql:2.3.0

  43. volumes:

  44. namenode:

  45. datanode:

From docker-hive GitHub

来自docker-hive GitHub

No one wants to read a HUGE config file, right? So here's a picture:

没有人想要读取巨大的配置文件,对吗? 所以这是一张照片:

Much better! Now let's start up those containers:

好多了! 现在让我们启动这些容器:

docker-compose up -d

We can see 5 containers:

我们可以看到5个容器:


  1. $ docker ps --format \

  2. "table {{.ID}}\t{{.Status}}\t{{.Names}}"

  3. CONTAINER ID STATUS NAMES

  4. 158741ba0339 Up 1 minutes dockerhive_hive-metastore-postgresql

  5. 607b00c25f29 Up 1 minutes dockerhive_namenode

  6. 2a2247e49046 Up 1 minutes dockerhive_hive-metastore

  7. 7f653d83f5d0 Up 1 minutes (healthy) dockerhive_hive-server

  8. 75000c343eb7 Up 1 minutes (healthy) dockerhive_datanode

Next let's check our Docker networks:

接下来,让我们检查一下Docker网络:


  1. $ docker network ls

  2. NETWORK ID NAME DRIVER SCOPE

  3. c3cd46f397ce bridge bridge local

  4. 9f6bc3c15568 docker-hive_default bridge local

  5. ad4e4c24568e host host local

  6. 1c69593fc6ac none null local

Wait a minute... there's a new network called docker-hive_default!

等等...有一个名为docker-hive_default的新网络!

By default docker compose sets up a single network for your app. And your app’s network is given a name based on the “project name”, originated from the name of the directory it lives in.

默认情况下,docker compose为您的应用设置一个网络。 应用的网络将基于“项目名称”获得一个名称,该名称源自其所在目录的名称。

So since our directory is named docker-hive, this explains the new network.

因此,由于我们的目录名为docker-hive ,这说明了新网络。

Next some examples on how to get the Docker IP Address.

接下来是有关如何获取Docker IP地址的一些示例。

如何获取Docker容器IP地址-示例
(How to Get A Docker Container IP Address - examples)

And now that I have your attention, we are going to unveil the mystery.

现在,请注意,我们将揭开这个谜团。

1.使用Docker Inspect (1. Using Docker Inspect)

Docker inspect is a great way to retrieve low-level information on Docker objects. You can pick out any field from the returned JSON in a fairly straightforward manner.

Docker inspect是检索有关Docker对象的低级信息的好方法。 您可以以相当简单的方式从返回的JSON中选择任何字段。

So shall we use it to get the IP Address from the dockerhive_datanode?

那么我们是否应该使用它从dockerhive_datanode获取IP地址?


  1. $ docker inspect -f \

  2. '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' \

  3. 75000c343eb7

  4. 172.18.0.5

Didn't you say that Docker uses the default 172.17. 0.0/16 subnet for container networking? Why is the returned IP Address: 172.18.0.5  outside it?

您不是说Docker使用默认的172.17。 用于容器网络的0.0 / 16子网? 为什么返回的IP地址: 172.18.0.5在外面?

To answer that we have to look at our network settings:

要回答这个问题,我们必须查看我们的网络设置:


  1. $ docker network inspect -f \

  2. '{{range .IPAM.Config}}{{.Subnet}}{{end}}' 9f6bc3c15568

  3. 172.18.0.0/16

We executed this example in a Compute Engine VM, and in this test, the docker network was assigned a different subnet: 172.18.0.0/16. That explains it!

我们在Compute Engine VM中执行了此示例,在此测试中,为docker网络分配了另一个子网: 172.18.0.0/16 。 这就解释了!

Furthermore, we can also lookup all IP Addresses inside the docker-hive_default network.

此外,我们还可以在docker-hive_default网络中查找所有IP地址。

So we don't need to look up each Container's IP individually:

因此,我们不需要单独查找每个容器的IP:


  1. $ docker network inspect -f \

  2. '{{json .Containers}}' 9f6bc3c15568 | \

  3. jq '.[] | .Name + ":" + .IPv4Address'

  4. "dockerhive_hive-metastore-postgresql:172.18.0.6/16"

  5. "dockerhive_hive-metastore:172.18.0.2/16"

  6. "dockerhive_namenode:172.18.0.3/16"

  7. "dockerhive_datanode:172.18.0.5/16"

  8. "dockerhive_hive-server:172.18.0.4/16"

If you didn't notice, we used jq help to parse the Containers map object.

如果您没有注意到,我们使用jq帮助来解析Containers地图对象。

2.使用Docker exec (2. Using Docker exec)

In the following example we will work with the dockerhive_namenode.

在以下示例中,我们将使用dockerhive_namenode 。


  1. $ docker exec dockerhive_namenode cat /etc/hosts

  2. 127.0.0.1 localhost

  3. ::1 localhost ip6-localhost ip6-loopback

  4. fe00::0 ip6-localnet

  5. ff00::0 ip6-mcastprefix

  6. ff02::1 ip6-allnodes

  7. ff02::2 ip6-allrouters

  8. 172.18.0.3 607b00c25f29

3.在Docker容器内部 (3. Inside the Docker Container)


  1. $ docker exec -it dockerhive_namenode /bin/bash

  2. # running inside the dockerhive_namenode container

  3. ip -4 -o address

  4. 7: eth0 inet 172.18.0.3/16 brd 172.18.255.255 scope global eth0

We can even find other containers' IP Addresses that are inside a container in the same network:

我们甚至可以找到同一网络中某个容器内其他容器的IP地址:

Data node

数据节点


  1. # running inside the dockerhive_namenode container

  2. ping dockerhive_datanode

  3. PING dockerhive_datanode (172.18.0.5): 56 data bytes

  4. 64 bytes from 172.18.0.5: icmp_seq=0 ttl=64 time=0.092 ms

Hive mestastore

蜂巢mestastore


  1. # running inside the dockerhive_namenode container

  2. ping dockerhive_hive-metastore

  3. PING dockerhive_hive-metastore_1 (172.18.0.2): 56 data bytes

  4. 64 bytes from 172.18.0.2: icmp_seq=0 ttl=64 time=0.087 ms

Hive server

蜂巢服务器


  1. # running inside the container

  2. ping dockerhive_hive-server

  3. PING dockerhive_hive-server (172.18.0.4): 56 data bytes

  4. 64 bytes from 172.18.0.4: icmp_seq=0 ttl=64 time=0.172 ms

结语 (Wrap up)

All examples were executed in a linux distribution Compute Engine VM. If you execute them in macOS or Windows environments the sample commands might change a bit.

所有示例均在linux发行版Compute Engine VM中执行。 如果您在macOS或Windows环境中执行它们,示例命令可能会有所变化。

Also bear in mind that those IP Addresses in the examples given are internal to the sample docker-hive_default network. So if you have a use case to connect to those containers externally, you would need to use the host machine's external IP (assuming that you are exposing the containers ports correctly). Or if you are using kubernetes, for instance, to manage your Docker containers, let it handle the IP Addresses for you kubernetes-expose-external-ip-address 

docker容器IP的设置相关推荐

  1. 共享内存简介及docker容器的shm设置与修改

    共享内存简介及docker容器的shm设置与修改 共享内存简介 共享内存指 (shared memory)在多处理器的计算机系统中,可以被不同中央处理器(CPU)访问的大容量内存.由于多个CPU需要快 ...

  2. centos查看docker容器ip

    1.安装net-tools 命令:yum install net-tools -y 2.查看 docker0 inet addr后面的即为docker容器ip 命令:ifconfig

  3. docker容器的网络配置,允许docker可以被宿主机以外的其它主机访问以及局域网内可以直接访问docker容器ip

    自从Docker容器出现以来,容器的网络通信就一直是被关注的焦点,也是生产环境的迫切需求.容器的网络通信又可以分为两大方面:单主机容器上的相互通信,和跨主机的容器相互通信. 一.端口映射(局域网,外网 ...

  4. Docker容器网络代理设置

    之前已经讲过如何设置Docker守护进程如何设置网络代理,那么如何设置运行的Docker容器的网络代理呢? 设置环境变量 设置容器环境变量,这也是最直接的一种方式.启动容器时,通过设置–env的fla ...

  5. 固定docker容器IP方法

    docker 默认有4种网络模式,详细可查看我曾经写的 "docker几种网络模式" docker run 新建并启动容器时,不指定网络,默认是使用桥接模式,容器内部IP是通过DH ...

  6. docker容器ssh自启动设置

    最近经常遇到服务器docker容器异常关闭的情况,导致VSCode无法通过remote-ssh连接到docker,在此记录一下解决方案. 参考链接: docker容器内服务开机自启动实现方案(以ssh ...

  7. docker容器ip分配问题

    docker容器是默认绑定docker0,然后动态分配IP的,但是这种默认方式存在几个问题: 问题1:docker容器的docker0网段IP在各服务器在路由上不是能够互相通信的,那么会导致在各服务器 ...

  8. 【docker】修改docker容器配置,设置/修改端口映射

    目录 前言 方法1:将容器转换成镜像,用新的镜像创建新的容器 方法2:修改容器配置 方法3:创建新的容器 方法4:nginx stream代理 参考 前言 docker 创建容器时可指定端口映射.但容 ...

  9. 使Docker容器拥有可被宿主机以外的机器直接访问的独立IP

    我们常用的docker容器都是将ip端口映射到宿主机,通过宿主机IP进行访问.外部无法直接访问容器IP,下面简单介绍下怎么做到局域网内直接访问docker容器IP. 自动化脚本见 https://gi ...

最新文章

  1. 基于OpenCV与tensorflow实现实时手势识别
  2. 排球赛程序(个人作业)
  3. FTP 主动、被动工作模式
  4. Linux循环链表删除节点,删除循环单链表开头元素
  5. HTML a 标签的正则表达式
  6. 2021肿瘤学,所有SCI期刊都在这里
  7. DEPENDS工具和DUMPBIN工具使用
  8. 安卓设置Activity切换动画无效的问题
  9. java md2_java中加密的实现方法(MD5,MD2,SHA)
  10. win10删除开机密码_win10系统,电脑密码和微软密码都忘记了,怎么办? Day22
  11. 蓝桥杯真题-单词分析
  12. SharePoint下载服务器资源
  13. 桌面虚拟化(VDI)的概述
  14. 基于android手机实时监控ipcam视频之三:H.264的RTP打包解析
  15. 【算法学习4】树与二叉树基础
  16. 如何做番茄炖牛腩——hadoop理解
  17. 敏涵控股集团董事长刘敏:感恩奉献 一路向前
  18. Part6---Java创建Hbase表
  19. sm2电子印章结构体
  20. Windows操作系统优化

热门文章

  1. Arduino I2C任意更换SDA SCL GPIO引脚
  2. Classical Algorithm--Mobius反演
  3. 【ARM 嵌入式 C 入门及渐进 4-- Linux 位图 bitmap】
  4. PHP连接操作sqlserver
  5. 他山之石 | 丁香园 医疗领域图谱的构建与应用
  6. Discuz论坛怎么样防止被人恶意灌水
  7. 记录解决windows XP启动加载个人设置慢的问题
  8. 调制与解调(1)——初认识
  9. 【allegro 17.4软件操作保姆级教程三】布局操作基础二
  10. JDBC Statement RETURN_GENERATED_KEYS返回自动生成的ID