目录
一 Docker概述
1.1 Docker是什么
●是一种轻量级的“虚拟机”
●在Linux容器里运行应用的开源工具
1.2 Docker与虚拟机的区别
区别 | 虚拟机 | 容器 |
---|---|---|
资源占用 | 多 | 少 |
开启时间 | 30-60秒 | 毫秒级别 |
安全性 | 系统隔离 | 内核共享 |
弹性扩展 | 执行时间长 | 执行时间短 |
1.3 Docker的使用场景
●打包应用程序简化部署
●可脱离底层硬件任意迁移
●例:服务器从腾讯云移到阿里云
二 Docker的核心概念及安装方式
2.1 Docker核心概念
●镜像:
一个面向Docker容器引擎的只读模板
●容器:
从镜像创建的运行实例
●仓库:
集中保存镜像的地方
2.2 CentOS安装Docker的两种方式
●使用CURL获得Docker的安装脚本进行安装
●使用YUM仓库来安装Docker
三 Docker部署安装
1.安装依赖包
[root@localhost ~]# yum -y install yum-utils device-mapper-persistent-data lvm2
2.设置阿里云镜像源
[root@localhost ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3.安装Docker-CE
[root@localhost ~]# yum -y install docker-ce ##注意这边要把本地源改为线网源
4.关闭防火墙以及核心防护
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
setenforce: SELinux is disabled
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled
5.开启docker服务
[root@localhost ~]# systemctl start docker.service
[root@localhost ~]# systemctl enable docker.service
6.镜像加速
[root@localhost ~]# cd /etc/docker/
[root@localhost docker]# ls
key.json
[root@localhost docker]# tee /etc/docker/daemon.json <<-'EOF'
> {
> "registry-mirrors": ["https://p3d56qc5.mirror.aliyuncs.com"]
> }
> EOF
{
"registry-mirrors": ["https://p3d56qc5.mirror.aliyuncs.com"]
}
[root@localhost docker]# ls
daemon.json key.json
[root@localhost docker]# vim daemon.json ·
[root@localhost docker]# systemctl daemon-reload
[root@localhost docker]# systemctl restart docker
7.网络优化
[root@localhost docker]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1
[root@localhost docker]# sysctl -p
net.ipv4.ip_forward = 1
[root@localhost docker]# service network restart
Restarting network (via systemctl): [ 确定 ]
[root@localhost docker]# systemctl restart docker
四 Docker镜像创建与操作
[root@localhost docker]# docker version ##查看docker版本信息
[root@localhost docker]# docker search nginx ##查找指定镜像
[root@localhost docker]# docker pull nginx ##下载镜像(分布式存放,分层下载)
[root@localhost docker]# docker images ##查看下载镜像信息
[root@localhost docker]# cd /var/lib/docker/image/overlay2/ ##下载文件的信息位置
[root@localhost overlay2]# vi repositories.json ##镜像信息
[root@localhost overlay2]# docker inspect c39a868aad02 ##获取镜像信息
[root@localhost overlay2]# docker tag nginx:latest nginx:web ##添加新标签
[root@localhost overlay2]# docker images | grep nginx ##查看指定镜像信息
[root@localhost overlay2]# docker rmi nginx:latest ##删除镜像
[root@localhost overlay2]# docker save -o /opt/nginx nginx:web ##存出镜像命名为nginx存在opt目录下
[root@localhost overlay2]# docker load < /opt/nginx ##载入镜像
[root@localhost overlay2]# docker tag nginx:web cwj/nginx:web ##改标签
[root@localhost overlay2]# docker push cwj/nginx:web ##上传镜像
五 Docker容器操作
[root@localhost overlay2]# docker create -it nginx:web /bin/bash ##创建容器(i:让容器的标准输入保持打开;t:让Docker分配一个伪终端)
[root@localhost overlay2]# docker ps -a ##查看容器(a:列出所有的容器,包括未运行的容器,不加-a列出运行的容器)
[root@localhost overlay2]# docker start f9eaf968d810 ##启动容器
[root@localhost overlay2]# docker exec -it dd8130492672 /bin/bash ##进入已经在运行的容器中(一定是运行状态)
[root@dd8130492672 /]# exit ##退出容器
[root@localhost overlay2]# docker run paigeeworld/centos7 /usr/bin/bash -c ls / ##启动执行命令查看系统根目录,-c:执行完释放掉容器,状态是Exited
相当于: docker pull paigeeworld/centos7
docker create -it paigeeworld/centos7 /bin/bash
docker start 0c568b6fc0fd
[root@localhost overlay2]# docker run -d paigeeworld/centos7 /bin/bash -c "while true;do echo hello;done" ##持续后台运行,-d:表示在后台运行
[root@localhost ~]# docker stop 31cdd35ce6e4 ##终止运行
[root@localhost overlay2]# docker export f9eaf968d810 > nginx_c ##容器导出
[root@localhost overlay2]# cat nginx_c | docker import - nginx:web ##容器导入,只会生成镜像,而不会创建容器
[root@localhost overlay2]# docker rm f9eaf968d810 ##删除容器(正在运行的容器不能被删除)
[root@localhost overlay2]# docker ps -a | awk '{print "docker rm "$1}' | bash ##批量删除未运行的容器