美文网首页
在Debian8.5系统中安装docker配置网络与宿主机同一网

在Debian8.5系统中安装docker配置网络与宿主机同一网

作者: Lei观澜 | 来源:发表于2018-06-26 10:43 被阅读31次

1、安装Debian系统。这个就不写过程了,和普通的linux系统安装过程相似,不会的可以google。

2、安装完成后配置apt源。

先将原来的源保存备份。

cp /etc/apt/sources.list /etc/apt/sources.list.bak

然后vim /etc/apt/sources.list。进入文件将文件删除,输入以下内容(亲测可用)。

deb http://mirrors.aliyun.com/debian stable main contrib non-free

deb http://mirrors.aliyun.com/debian stable-proposed-updates main contrib non-free

deb http://mirrors.aliyun.com/debian stable-updates main contrib non-free

deb-src http://mirrors.aliyun.com/debian stable main contrib non-free

deb-src http://mirrors.aliyun.com/debian stable-proposed-updates main contrib non-free

deb-src http://mirrors.aliyun.com/debian stable-updates main contrib non-free

deb http://http.debian.net/debian jessie-backports main

deb http://mirrors.ustc.edu.cn/debian stable main contrib non-free

deb-src http://mirrors.ustc.edu.cn/debian stable main contrib non-free

deb http://mirrors.ustc.edu.cn/debian stable-proposed-updates main contrib non-free

然后运行

apt-get update    

3、接下按照官方文档开始安装docker

https://docs.docker.com/install/linux/docker-ce/debian/#uninstall-old-versions

如果没有安装成功的话,一般都是apt源的问题。或者有什么依赖没有装上。

安装完成后输入docker version 可以看到server 和client 两个类信息显示。说明安装成功。

之后就可以进行docker 的操作了。

4、配置docker镜像。

推荐使用国内镜像。http://www.docker-cn.com/registry-mirror

vim /etc/docker/daemon.json 

输入信息:

{

"registry-mirrors": ["https://registry.docker-cn.com"]

}

之后就可以进行镜像的拉取了。

这里有一些基础教程

https://yeasy.gitbooks.io/docker_practice/content/

https://zhuanlan.zhihu.com/p/23599229

5、接下来就是配置网络了。

由于我这里生产环境要求运行的容器需要与宿主机处于同一网段。方便外部设备访问容器。

所以就进行了一些配置修改。找了好久最后在B站上搜到了一个极客学院的视频。

里面介绍了三种配置网络的访问。我这里只用了第一种。

下面是配置方法。

配置方法是基于类ubuntu,Debian系统进行的。

首先配置网卡 。记得保存原有配置。

直接新建vim /etc/network/interfaces 

auto lo

iface lo inet loopback

auto br0 

iface br0 inet static

address 172.16.1.113

netmask 255.255.0.0

gateway 172.16.0.1

bridge_ports eth0

eth0需要改成实际的主网卡名称, 通过ifconfig 可以看到。

保存文件,reboot 重启系统。

进入系统后可以看到

ifconfig

然后修改docker配置文件启用br0网桥。

还是先保存原来的配置文件。

mv /lib/systemd/system/docker.service /lib/systemd/system/docker.service.bak

然后

vim  /lib/systemd/system/docker.service

在文件中添加

EnvironmentFile=-/etc/default/docker

ExecStart=/usr/bin/dockerd -H fd:// $DOCKER_OPTS

/lib/systemd/system/docker.service

启用/etc/default/docker 文件。没有这个文件的话可以直接新建就好了。

在文件中写入

DOCKER_OPTS= " -b=br0 --fixed-cidr=172.16.1.113/29"

-b指定使用的网桥名称

fixed 指定启动容器后为容器分配的IP地址

计算ip地址网站:

http://help.bitscn.com/ip/

重启docker服务

systemctl daemon-reload

 systemctl restart docker

运行docker 启动容器。docker 将按照fixed 参数为容器分配地址。

这样非宿主机就可以访问容器内部了。

相关文章

网友评论

      本文标题:在Debian8.5系统中安装docker配置网络与宿主机同一网

      本文链接:https://www.haomeiwen.com/subject/gljpyftx.html