部署docker容器虚拟化平台
安装docker环境依赖
[root@niexj31 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
配置国内docker的yum源(阿里云)
[root@niexj31 ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安装docker-ce
[root@niexj31 ~]# yum install -y docker-ce docker-ce-cli containerd.io
启动docker设置docker开机自启动
[root@niexj31 ~]# systemctl start docker && systemctl enable docker
显示docker版本信息
[root@niexj31 ~]# docker version
查看docker信息
[root@niexj31 ~]# docker info
配置docker镜像加速器(清华、网易、科大)
[root@niexj31 ~]# vim /etc/docker/daemon.json
{
"registry-mirrors":["https://reg-mirror.qiniu.com/","https://hub-mirror.c.163.com/","https://docker.mirrors.ustc.edu.cn/"]
}
重载配置、重启docker服务
[root@niexj31 ~]# systemctl daemon-reload && systemctl restart docker
查看docker镜像加速器
[root@niexj31 ~]# docker info
查找镜像:docker search + 镜像名:标签
[root@niexj31 ~]# docker search centos
拉取镜像:docker pull + 镜像名:标签
查看本地镜像
[root@niexj31 ~]# docker images
本地镜像打包:docker save -o +包名 + 镜像名:标签
[root@niexj31 ~]# docker save -o centos-v1.tar centos:latest
删除本地镜像:docker rmi + 镜像名:标签(或镜像ID)
[root@niexj31 ~]# docker rmi centos:latest
上传本地镜像包:docker load -i +镜像包名
[root@niexj31 ~]# docker load -i centos-v1.tar
启动docker实例
[root@niexj31 ~]# docker run -it --name centos1 centos:latest bash
[root@niexj31 ~]# docker run -itd --name centos2 centos:latest /bin/bash -c "echo 123"
查看后台运行的所有镜像实例
[root@niexj31 ~]# docker ps -a
docker logs + 容器实例ID或实例的名称
[root@niexj31 ~]# docker logs 10981fd5c896
实例一个容器并在后台运行
[root@niexj31 ~]# docker run -itd --name centos3 centos:latest /bin/bash
查看后台正在运行的容器实例
[root@niexj31 ~]# docker ps
停止一个后台正在运行的实例容器:docker stop 容器名或容器ID
[root@niexj31 ~]# docker stop centos3
启动一个实例容器:docker start + 容器名或容器ID
[root@niexj31 ~]# docker start centos3
杀死一个正在运行的实例容器:docker kill + 容器名或容器ID
[root@niexj31 ~]# docker kill centos3
进入一个后台正在运行的实例容器:docker exec -it + 容器名或容器ID /bin/bash
[root@niexj31 ~]# docker exec -it centos3 /bin/bash
删除实例容器:docker rm + 容器实例名或容器实例ID
如果实例容器正在后台运行需要先停止才能删除
[root@niexj31 ~]# docker rm e1c7bac24d63
Error response from daemon: You cannot remove a running container e1c7bac24d63ab6106e4b8c4cddb1a6bfdf92f205e3eb21bd5ee2d6b80ea97a9. Stop the container before attempting removal or force remove
[root@niexj31 ~]# docker stop e1c7bac24d63
e1c7bac24d63
[root@niexj31 ~]# docker rm e1c7bac24d63
e1c7bac24d63
镜像制作的方法
创建一个安装好Apache web服务器的容器镜像

[root@niexj31 ~]# docker run -it --name apache1 centos:7.6.1810 /bin/bash
[root@6364bd2edf8f /]# yum install -y httpd
[root@6364bd2edf8f /]# exit
[root@niexj31 ~]# docker ps -a
根据容器当前状态做一个image镜像:docker commit + containerID + 新的image_ID
[root@niexj31 ~]# docker commit 6364bd2edf8f centos:apache
[root@niexj31 ~]# docker images
[root@niexj31 ~]# docker run -it --name centps-apache1 centos:apache /bin/bash
[root@5043fb2dd731 /]# rpm -qa httpd
httpd-2.4.6-97.el7.centos.1.x86_64
[root@5043fb2dd731 /]#
把生成的centos:apache保存到本地系统
[root@niexj31 ~]# docker save -o centos:apache1.tar centos:apache
删除镜像,删除前需要把以该镜像为实例的容器删掉,才能删除镜像成功
[root@niexj31 ~]# docker rmi centos:apache
Error response from daemon: conflict: unable to remove repository reference "centos:apache" (must force) - container 5043fb2dd731 is using its referenced image e0ab66d9e4a8
[root@niexj31 ~]# docker ps -a
[root@niexj31 ~]# docker rm centps-apache1
centps-apache1
[root@niexj31 ~]# docker rmi centos:apache
Untagged: centos:apache
Deleted: sha256:e0ab66d9e4a8fbf698439b0cc52611ae2a9e6ee7b1990c04128be636c95658f1
Deleted: sha256:cdc9af08ae7c4adeb78a0136cbb935588af5f6ae13dd38f5694d26a63b0fb0e5
[root@niexj31 ~]# docker images
在容器实例中需要使用systemctl管理服务命令的方法
[root@niexj31 ~]# docker run -itd --name apache5 --privileged=true centos:apache /usr/sbin/init
[root@niexj31 ~]# docker exec -it apache5 /bin/bash
[root@fcb0885ed5e5 /]# systemctl status httpd
若不然会报错:Failed to get D-Bus connection: Operation not permitted
方法二:使用Dockerfile创建镜像
[root@niexj31 ~]# mkdir /etc/docker/dockerfile
[root@niexj31 ~]# cd !$
cd /etc/docker/dockerfile
[root@niexj31 dockerfile]# vim Dockerfile #写入一下内容
FROM centos:latest
MAINTAINER <niexj>
RUN yum install -y httpd
ADD index.html /var/www/html/
ADD index.html /var/www/html/ #复制物理机上dockerfile目录下的index.html文件,到实例中/var/www/html/的目录下
[root@niexj31 dockerfile]# echo "docker hello" > index.html
构建centos:apache2
[root@niexj31 dockerfile]# docker build -t centos:apache2 .
[root@niexj31 dockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos apache2 3c0822954ba9 10 seconds ago 286MB
centos apache e0ab66d9e4a8 51 minutes ago 377MB
centos latest 5d0da3dc9764 5 weeks ago 231MB
centos 7.6.1810 f1cb7c7d58b7 2 years ago 202MB
以centos:apache2镜像,创建容器实例
[root@niexj31 dockerfile]# docker run -itd --name apache6 --privileged=true centos:apache2 /usr/sbin/init
1a501a1a748364ac4eb31a09e61450706a67a5fb29bd94eefd31e83c348fdb78
[root@niexj31 dockerfile]# docker exec -it apache6 /bin/bash
[root@1a501a1a7483 /]# rpm -qa httpd
httpd-2.4.37-39.module_el8.4.0+950+0577e6ac.1.x86_64
[root@1a501a1a7483 /]# cat /var/www/html/index.html
docker hello
容器端口映射
[root@niexj31 dockerfile]# docker run -itd --name apache7 --privileged=true -p 80:80 centos:apache2 /usr/sbin/init
在物理机上查看
[root@niexj31 ~]# netstat -antup | grep "80"
查看容器中的测试页
[root@51730f4ce5b2 /]# cat /var/www/html/index.html
docker hello
测试
[root@niexj31 ~]# curl localhost
docker hello