●是一种轻量级的“虚拟机”
●在Linux容器里运行应用的开源工具
●打包应用程序简化部署
●可脱离底层硬件任意迁移
●例:服务器从腾讯云迁移到阿里云
虚拟机 容器
资源 占用资源多 占用资源少
开启时间 30-60秒 毫秒级别
安全 系统隔离,安全性高 内核共享,安全性较
弹性扩展 执行时间长 执行时间短
●·镜像
◆—个面向Docker容器引擎的只读模板
●容器
◆从镜像创建的运行实例
●仓库
◆集中保存镜像的地方
●使用CURL获得Docker的安装脚本进行安装
●使用YUM仓库来安装Docker
●应用发布的标准格式
●支撑一个Docker容器的运行
●基于已有镜像创建
●基于本地模板创建
●基于Dockerfile创建
1∶安装依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
#yum-utils提供了yum-config-manager
#device mapper存储驱动程序需要device-mapper-persistent-data和lvm2#Device Mapper是 Linux2.6内核中支持逻辑卷管理的通用设备映射机制,
它为实现用于存储资源管理的块设备驱动提供了一个高度模块化的内核架构。
2:设置阿里云镜像源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3:安装Docker-CE
yum install -y docker-ce
systemctl stop firewalld.service
setenforce 0
vim /etc/selinux/config
SELINUX=disabled
systemctl start docker.service
systemctl enable docker.service
docker images 查看镜像
docker ps -a 查看容器
镜像加速
cd /etc/docker/
tee /etc/docker/daemon.json <<-'EOF'
{
>registry-mirrors":["https://u8w1de1s.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker
网络优化
[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1 #开启路由转发
[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1
docker镜像操作
[root@localhost ~]# docker version #查看版本
[root@localhost ~]# docker search nginx #搜索镜像
[root@localhost ~]# docker pull nginx #下载镜像,下载后存放在/var/lib/docker
[root@localhost ~]# docker images #查看镜像列表
[root@localhost yum.repos.d]# docker inspect c59a758aad03 #查看镜像详细信息,c39a868aad02为镜像id号
镜像重命名和标签
[root@localhost yum.repos.d]# docker tag nginx:latest newnginx:newtag
[root@localhost yum.repos.d]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
newnginx newtag c59a758aad03 3 days ago 133MB
nginx latest c59a758aad03 3 days ago 133MB
删除镜像
[root@localhost ~]# docker rmi newnginx:newtag #一个镜像有多个标签时,删除其中一个不会删除镜像文件
Untagged: newnginx:newtag
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c59a758aad03 3 days ago 133MB
[root@localhost ~]# docker rmi nginx:latest #将镜像文件的所有标签都删除后,镜像文件删除
Untagged: nginx:latest
Untagged: nginx@sha256:aeade65e99e5d5e7ce162833636f692354c227ff438556e5f3ed0335b7cc2f1b
Deleted: sha256:c39a868aad02a383c7e490e0fc4a5b0217f667f2de764bc2755e315a5adf64a1
Deleted: sha256:1af47386c12cb24b362bd785e71058ab675fbdcbda8e7a7ba0dd10c3cff75a2a
Deleted: sha256:25ee02f9e42f8cdca7ca8bc522cb69ac4a86a55401818a13c7a01e52a81894fe
Deleted: sha256:b1d2bc3292aa3d64794f99d2885d35b454993b4af24ad350a969d27925db7ebb
Deleted: sha256:4216e20d59f9fc90bccdd14493452532806d4803a25514366de2a9c5560624d0
Deleted: sha256:d0fe97fa8b8cefdffcef1d62b65aba51a6c87b6679628a2b50fc6a7a579f764c
导入导出镜像
[root@localhost ~]# docker save -o /opt/nginx nginx:latest #导出镜像到/opt/nginx
[root@localhost ~]# ls -h /opt
containerd nginx
[root@localhost ~]# docker load < /opt/nginx #导入镜像
d0fe97fa8b8c: Loading layer 72.49MB/72.49MB
2baf69a23d7a: Loading layer 64.54MB/64.54MB
2f57e21e4365: Loading layer 3.072kB/3.072kB
aee208b6ccfb: Loading layer 4.096kB/4.096kB
7b5417cae114: Loading layer 3.584kB/3.584kB
Loaded image: nginx:latest
docker create [选项] 镜像 运行的程序
-i 让容器的输入保持打开
-t 让docker 分配一个伪终端
[root@localhost ~]# docker create -it nginx /bin/bash
b271e91455e14779650368db982fc56789f2b82bce9ac465879820a2387b08c2
查看所有运行的容器状态
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b260e91665e1 nginx "/docker-entrypoint.…" 4 minutes ago Created nifty_joliot
启动容器
[root@localhost ~]# docker start b271e91455e1
b271e91455e1
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b271e91455e1 nginx "/docker-entrypoint.…" 5 minutes ago Up 11 seconds 80/tcp nifty_joliot
如果想创建并启动容器,可以执行docker run
[root@localhost ~]# docker run centos /usr/bin/bash -c ls
Unable to find image 'centos:latest' locally #若本地有镜像,则直接创建容器,没有镜像就会自动去下载
latest: Pulling from library/centos
3c72a8ed6814: Pull complete
Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd
Status: Downloaded newer image for centos:latest
bin
dev
...
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6219fcdbeede centos "/usr/bin/bash -c ls" 6 minutes ago Exited (0) 6 minutes ago upbeat_hopper
b271e91455e1 nginx "/docker-entrypoint.…" 14 minutes ago Up 9 minutes 80/tcp nifty_joliot
正常容器任务结束后,系统会自动释放资源,容器状态为Exited (0)
若非正常自动释放而停止的容器,状态码为非0
[root@localhost ~]# docker run -d centos /usr/bin/bash -c "while true;do echo hello;done"
72dd38ac6e6b12ecbacd9f7f030a83fae5282ed241d6966e69271e97a0e9037a
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
72dd38ac6e6b centos "/usr/bin/bash -c 'w…" 42 seconds ago Up 41 seconds heuristic_matsumoto
b271e91455e1 nginx "/docker-entrypoint.…" 25 minutes ago Exited (0) 8 minutes ago nifty_joliot
[root@localhost ~]# docker stop 74dd59ac7e7b
72dd38ac6e6b
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
72dd38ac6e6b centos "/usr/bin/bash -c 'w…" 2 minutes ago Exited (137) 58 seconds ago heuristic_matsumoto
b271e91455e1 nginx "/docker-entrypoint.…" 27 minutes ago Exited (0) 10 minutes ago nifty_joliot
若容器内有命令正在运行,手动stop,容器状态码为非0.若容器内空闲状态而手动stop,状态码为0
进入正在运行的容器
docker exec -it 容器id /bin/bash
[root@localhost ~]# docker exec -it 72dd38ac6e6b /bin/bash
[root@72dd38ac6e6b /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
容器的导入和导出
docker export 容器id > 文件名
cat 文件名 | docker import
docker export 74dd59ac7e7b > /opt/centos_docker #导出
docker rm 72dd38ac6e6b #删除容器
docker rm -f b271e91455e1 #强制删除正在运行的容器
cat /opt/centos_docker | docker import - centos:test #导入,只能导入成镜像,不能成容器
批量删除停止状态的容器
docker ps -a | awk '{print "docker rm "$1}' | bash