0
点赞
收藏
分享

微信扫一扫

|NO.Z.00001|——|NavigationLog|——|Docker&制作镜像&批量启动容器|

禾木瞎写 2022-03-23 阅读 111

一、docker的安装配置

### --- dockercentos7.x下安装教程:
### --- yum方式安装docker并配置桥接网络
[root@localhost ~]#  yum install -y docker                              // yum方式安装docker
[root@localhost ~]#  systemctl start docker                   
[root@localhost ~]#  ipaddr                                             // 查看是否有docker0网卡
[root@localhost ~]#  systemctl stop                                     // 停止docker
[root@localhost ~]#  ip link set dev docker0 down                       // down掉docker0网卡         
[root@localhost ~]#  brctl delbr docker0                                // 删除docker0网卡
### --- 配置桥接网络:
[root@localhost ~]#  brctl addbr br0                                    // 新建桥接网卡br0
[root@localhost ~]#  ip link set dev br0 up                             // 设置为启动状态
[root@localhost ~]#  ip addr add 192.168.1.61/24 dev br0                // 为br0设置IP,这一步会中断网络。
### --- 为br0分配物理网络中的IP地址
[root@localhost ~]#   ip addr  del 192.168.1.61/24 dev ens33            // 将宿主机网卡上的IP清空
[root@localhost ~]#   brctl addif br0 ens33                             // 将宿主机IP挂到br0上
[root@localhost ~]#   ip route del default                              // 删除原路由
[root@localhost ~]#   ip route add default via 192.168.1.1 dev br0      // 为br0设置路由。
[root@localhost ~]#   route -n                                          // 查看网关
### --- 设置docker服务启动参数:
[root@localhost ~]#  vim /etc/sysconfig/docker-network                   // 其他系统可能在:/etc/sysconfig/docker下。
DOCKER_NETWORK_OPTIONS="-b=br0"                                          // 设置为此内容即可
[root@localhost ~]#  systemctl restart docker                            // 启动docker服务
### --- 安装pipework
[root@localhost ~]#     git clone https://github.com/jpetazzo/pipework  // 下载pipework服务
[root@localhost ~]#     cp ~/pipework/pipework /usr/local/bin/          // 复制到/usr/local/sbin        
### --- 启动容器
[root@localhost ~]# docker run -itd --net=none  --name=centos7-ssh \
 docker.io/yumin9822/centos7-sshd:latest /bin/bash                           // 启动一个容器
[root@localhost ~]#   pipework  br0 centos7-ssh 192.168.1.70/24@192.168.1.1  // 为容器指定ip地址
[root@localhost ~]#   docker attach centos7-ssh                              // 进入centos7-ssh容器内部
[root@c6915d22954d ~]# cp -r /etc/skel/.bash* /root/                         // 拷贝环境变量到root下。
### --- 启动容器并操作
[root@localhost ~]# docker attach centos7-ssh                               // 容器未启动,启动容器
You cannot attach to a stopped container, start it first
[root@localhost ~]# docker start centos7-ssh                                // 启动容器
[root@localhost ~]# docker ps                                               // 查看容器起来了没
[ root@localhost ~]#  docker attach centos7-ssh                             // 进去后发现docker没有ip地址,重启后ip地址失效。
[root@localhost ~]#  docker start centos7-ssh
[root@localhost ~]#  pipework  br0 centos7-ssh 192.168.1.70/24@192.168.1.1  // 指定ip地址
[root@c6915d22954d /]#  route -n                                            // 查看它的网关
[root@c6915d22954d /]#  service sshd restart                                // 启动ssh服务。
### --- 将.tar文件上传到docker仓库用来做镜像。
### --- 如何把封装好的.tar 文件上传到docker仓库:两种方案。
[root@localhost ~]# docker  import <centos7.tar
[root@localhost ~]# cat centos7.tar  |docker import -centos7
### --- docker常用操作
[root@localhost ~]# docker vison                                            // 查看docker版本
[root@localhost ~]# docker  search centos                                   // 搜索可用的镜像
[root@localhost ~]#  docker images                                          // 查看当前系统可用的镜像
[root@localhost ~]#  docker pull centos                                     // 下载镜像
[root@localhost ~]#  cat centos.tar |docker  import - centos6               // docker 导入镜像
[root@localhost ~]#  docker export id > centos6.tar                         // 导出镜像
[root@localhost ~]#  docker run centos echo “hello word”                    // 在容器中运行hello word!
[root@localhost ~]# docker run cnntos yum install ntpdate                   // 在容器中安装ntpdate程序
[root@localhost ~]#  docker ps -l                                           // 命令获得最后一个容器的ID,
[root@localhost ~]#  docker ps -a                                           // 查看所有的容器
[root@localhost ~]# docker commit 2313132 centos:v1                        // 例如运行docker commit 提交刚修改的容器
[root@localhost ~]# docker run centos:latest cat /etc/passwd                  // 查看容器里面的密码文件
[root@localhost ~]#  docker run -i -t centos:latest /bin/bash                 // 在容器里启动一个/bin/bash shell 环境,可以登录进入操作,其中-t表示打开一个终端的意思,-i表示可以交互输入
[root@5ca6ca0c184c /]# ip addr list                                            // 查看容器的ip地址
[root@localhost ~]# docker run -d -p 80:80 -p 8022:22 centos:v2 /bin/bash -D   // -p制定容器启动后docker上运行的端口号映射及容器里运行的端口,80:80 第一个80 表示docker系统上的80 ,第二个80表示docker虚拟机里面的端口,用户默认访问本机80 端口,自动映射到容器里面的80 端口。
[root@localhost ~]# docker rmi 镜像ID                                          // 删除镜像
### --- 安装ssh服务
### --- 安装常用的基础软件包:
[root@localhost ~]# yum install  vim iotop bc gcc gcc-c++ glibc \
glibc-devel pcre pcre-devel openssl  openssl-devel zip unzip zlib-devel \
net-tools lrzsz tree ntpdate telnet lsof tcpdump wget libevent \
libevent-devel bc  systemd-devel bash-completion traceroute -y
### --- 安装ssh:
 
[root@6cbc9ebfbc5f /]# yum -y install openssl openssh-server openssh-client  openssh-devel
[root@6cbc9ebfbc5f ~]# vim /etc/ssh/sshd_config                         // 修改配置文件
PermitRootLogin yes                                                     // 把no改为yes
UsePAM no                                                               // 改为no
Port 22
[root@6cbc9ebfbc5f ~]# systemctl restart sshd                           // 重启ssh服务
[root@6cbc9ebfbc5f ~]# passwd                                           // 更改root密码  //此时,就可以使用xshell去链接了。
### --- 将容器打包成镜像并导入导出:
~~~     # 将容器打包成镜像
[root@localhost ~]# docker commit -a "yanqi" -m "centos7-ssh" 6cbc9ebfbc5f  centos7-ssh:zabbix-agent 
~~~     # 从容器导出镜像。
[root@localhost ~]# docker save > centos7-ssh.tar   centos7-ssh:zabbix-agent
~~~     # 导入镜像
[root@localhost ~]# docker import centos7-ssh.tar centos7-ssh1:zabbix-agent
~~~     # 导入镜像
[root@localhost ~]# cat centos7-ssh.tar | docker import - centos7-ssh2:zabbix-agent
### --- 创建容器并运行
### --- 使用该命令去创建一个容器:具备systemctl启动权限
### --- 创建zabbix-docker-client10台设备推荐使用命令
~~~     # 创建容器并运行     
[root@localhost ~]# docker run -tid --net=none --name=centos7-ssh2 --privileged=true centos7-ssh:zabbix-agent /sbin/init
~~~     # 写入IP地址
[root@localhost ~]# pipework  br0 centos7-ssh 192.168.1.70/24@192.168.1.1
~~~     # 使用该命令进入容器
[root@localhost ~]# docker exec -it centos7-ssh1 /bin/bash  

附录一:下载docker源并安装测试

### --- 下载docker源并安装测试
[root@localhost ~]# wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost ~]# yum install -y docker-ce*
[root@localhost ~]# rpm -qa |grep -E "docker"
[root@localhost ~]#  systemctl start docker.service 
[root@localhost ~]#  systemctl enable docker.service 
~~~ 运行测试程序
[root@localhost ~]# docker run hello-world  

附录二:添加本地源

### ---- 添加本地源
[root@localhost ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": [
"https://kfwkfulq.mirror.aliyuncs.com",
"https://2lqq34jg.mirror.aliyuncs.com",
"https://pee6w651.mirror.aliyuncs.com",
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com"
],
"dns": ["8.8.8.8","8.8.4.4"]
}
[root@localhost docker]# systemctl restart docker.service

附录三:批量创建10台主机

### --- 批量创建10台主机:批量创建10台centos-ssh服务器用来做zabbix客户端:
[root@localhost ~]# docker ps -aq                                       // 查看docker安装的程序
[root@localhost ~]# docker ps -aq |xargs docker rm -f                   // 删除docker下的所有程序
~~~ 创建10台主机用来做client
[root@localhost ~]# for i in $(seq 1 10);do docker run -itd --privileged centos:centos7 ;done
[root@localhost ~]# docker ps                                           // 查看创建的十台主机
[root@localhost ~]# docker exec -it aa9b5d69de24 /bin/bash              // 进入到这个容器之下。

附录四:docker下创建自定义网络地址

### --- docker下创建自定义网络地址
[root@localhost ~]# docker network ls                                       // 查看网络模式
[root@localhost ~]# docker network create --subnet=10.0.1.1/24 extnetwork   // 创建自定义的网路类型:extnetwork形的bridge网络。
b92097ff8046        extnetwork          bridge              local
[root@localhost network-scripts]# docker network rm b92097ff8046            // 删除docker网络配置

附录五:查看容器Ip地址

### --- 查看容器Ip地址
~~~ 查看docker容器的IP地址
[root@localhost ~]# docker inspect 792dad250145
~~~ 查看容器的IP地址
[root@localhost ~]#   docker inspect --format='{{.NetworkSettings.IPAddress}}' 792dad250145

附录六:添加路由规则

### --- 添加路由规则:
br-b92097ff8046
ip route add 10.0.1.1/24 via 192.168.1.59 dev br-b92097ff8046

附录七:安装linux容器基础软件包

### --- 安装常用的基础软件包:
### --- 安装ssh:
[root@6cbc9ebfbc5f /]# yum -y install openssl openssh-server openssh-client  openssh-devel
[root@6cbc9ebfbc5f ~]# vim /etc/ssh/sshd_config                     //修改配置文件
PermitRootLogin yes                                                 //把no改为yes
UsePAM no                                                           //改为no
Port 22
[root@6cbc9ebfbc5f ~]# systemctl restart sshd                       //重启ssh服务
[root@6cbc9ebfbc5f ~]# passwd                                       //更改root密码  //此时,就可以使用xshell去链接了。

附录八:学习地址

### --- 学习地址:
https://ke.qq.com/course/348394?taid=2789714403086570
举报

相关推荐

0 条评论