一.安装docker服务
1.Docker.tar.gz上传
2.将Docker.tar.gz 解压到/opt目录下
命令为 docker -zxf Docker.tar.gz -C /opt
3.配置yum源(yum源为本地yum源和网络yum源)
本地yum源的形式为
[docker]
name=docker
baseurl=file:///opt/Docker
gpgcheck=0
enabled=1
4.关闭所有防火墙
setenforce 0
systemctl stop firewalld
systemctl disable firewalld
iptables -F
iptables -x
iptables -z
iptables -save
5.安装docker
yum install docker-ce -y
6.开启docker并设置开机自启
systemctl start docker
systemctl enable docker
7.查看是否成功
docker info
二.Docker 镜像管理
拉取镜像的知识
1.配置加速器
[root@localhost ~]# vi /etc/docker/daemon.json
{
"registry-mirrors":["https://dhp9bx4f.mirror.aliyuncs.com"]
}
【配置完加速器后要重启】不要忽略这一点
拉取centos镜像
[root@localhost ~]# docker pull centos
此时是从网络上拉取的
2.给镜像打标签
docker tag 300e315adb2f (此处写镜像的名字或者id) wangruxin(此处是你给镜像起的名字可以随意)
3.把镜像启动为容器
docker run -itd centos
4.查看所有容器
docker ps -a
查看本地镜像:docker images
搜索镜像:docker search 需要的镜像的名字
删除指定镜像:docker rmi 镜像名称
三.Docker通过容器创建镜像
1.docker run 容器启动后
2.docker exec -it d1e22ca7dc61(这里是容器的id) bash 进入容器
3.进入容器后可以安装一些工具[root@d1e22ca7dc61 /]# yum install net-tools -y
4.ctrl+d退出容器,此处需要两次,才能完全退出
5.[root@localhost ~]# docker commit -m"install net-tools" (为你安装的工具,或者作出的改变)-a "wangruxin"(作者) d1e22ca7dc61(容器id) heiheiya(为镜像取得新名字)
6.查看是否有heiheiya镜像
[root@localhost ~]# docker images
四.通过模板创建镜像
1.先下载模板 centos-7-x86_64-minimal.tar.g
2.将tar包导为镜像
[root@localhost ~]# cat centos-7-x86_64-minimal.tar.gz |docker import - centos7
3.查看[root@localhost ~]# docker images
此时多了一个centos7的镜像
4.把现有镜像导为一个文件
[root@localhost ~]# docker save -o ytl-wangruxin.tar(导为文件后显示) wangruxin(已有镜像的名字)
5.查看[root@localhost ~]# ll
此时多了一个
-rw-------. 1 root root 216535040 Dec 16 02:07 ytl-wangruxin.tar
6.tar包恢复镜像
[root@localhost ~]# docker load < ytl-wangruxin.tar
或者[root@localhost ~]# docker load --input ytl-centos.tar
五.Docker容器管理
1删除正在运行的容器加上-f
[root@localhost ~]# docker rm -f d1e22ca7dc61
2容器导出tar包
[root@localhost ~]# docker export a9c3cf95cd5c(容器id) > wang.tar(新取的名字)
查看ll
-rw-r--r--. 1 root root 238086656 Dec 16 02:23 wang.tar
tar包导为镜像
[root@localhost ~]# cat wang.tar |docker import - wang
六.Docker仓库管理
1.从网络拉取registry镜像
[root@localhost ~]# docker pull registry
2.把它启动作为私有仓库
[root@localhost ~]# docker run -itd -p 5000:5000 registry
3.检查是否拥有这个私有仓库
[root@localhost ~]# curl http://192.168.200.69:5000/v2/_catalog
{"repositories":[]}
4.把想要上传到私有仓库的镜像打标签
[root@localhost ~]# docker tag centos 192.168.200.69:5000/wangruxin
5.更改配置文件
[root@localhost ~]# vi /etc/docker/daemon.json
添加
"insecure-registries
":["192.168.200.69:5000"]
6.重启docker
[root@localhost ~]# systemctl restart docker
7.重启registry容器
[root@localhost ~]# docker start 23018bacd684
8,上传镜像
[root@localhost ~]# docker push 192.168.200.69:5000/wangruxin
9.再次查看私有仓库会有一个wangruxin的镜像
[root@localhost ~]# curl http://192.168.200.69:5000/v2/_catalog
{"repositories":["wangruxin"]}
10.相同网段的虚拟机可以从私有仓库拉取wangruxin这个镜像
这是96节点
两个节点之间要可以相互ping通
[root@localhost ~]# docker pull 192.168.200.69:5000/wangruxin
11.查看镜像信息
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.200.69:5000/wangruxin latest 300e315adb2f 9 days ago 209MB
七 Docker 数据管理
挂载本地的目录到容器里
在宿主机创建一个目录
[root@localhost ~]# mkdir /abc
然后挂载在容器里面
[root@localhost ~]# docker run -itd -v /abc/:/data centos bash (--name 给容器取得名字可加可不加)
使用上一个容器的数据卷,192.168.200.69:5000/wangruxin的镜像创建了一个新的容器
[root@localhost ~]# docker run -itd --volumes-from quirky_blackburn (上一个容器的数据卷)300e315adb2f(镜像id) /bin/bash
7780ab2dab9294ba6889fcb69c4a9cfcb02e706085228f38c8247b74892b9371
查看容器全部信息
[root@localhost ~]# docker inspect 7780ab2dab92
查看容器详细信息
[root@localhost ~]# docker inspect -f {{.Mounts}} 7780ab2dab92
[{bind /abc /data true rprivate}]
[root@localhost ~]# docker run -itd -v /data/ --name heiheipo centos bash
不进入容器查看
[root@localhost ~]# docker exec -it 214fc121b7dd (容器id) ls(想要的操作
)
八.Docker数据卷的备份和恢复