本文全部内容来自尚硅谷周阳老师
视频地址:www.bilibili.com/video/BV1gr…
本篇文章仅用于个人学习,如有侵权联系删除。
一、docker的简介
docker官网:www.docker.com
docker仓库:Docker Hub官网: hub.docker.com/
docker的概念
-
Docker是基于Go语言实现的云开源项目。
-
将应用打成镜像,通过镜像成为运行在Docker容器上面的实例,而 Docker容器在任何操作系统上都是一致的,这就实现了跨平台、跨服务器。
-
解决了运行环境和配置问题的软件容器, 方便做持续集成并有助于整体发布的容器虚拟化技术。
Docker和传统虚拟化方式的不同
-
传统虚拟机技术是虚拟出一套硬件后,在其上运行一个完整操作系统,在该系统上再运行所需应用进程
-
容器内的应用进程直接运行于宿主的内核,容器内没有自己的内核且也没有进行硬件虚拟。因此容器要比传统虚拟机更为轻便。
-
每个容器之间互相隔离,每个容器有自己的文件系统 ,容器之间进程不会相互影响,能区分计算资源。
Docker容器与虚拟机(VM)对比
| Docker容器 | 虚拟机(VM) | |
|---|---|---|
| 操作系统 | 与宿主机共享OS | 宿主机OS上运行虚拟机OS |
| 存储大小 | 镜像小,便于存储与运输 | 镜像庞大(vmdk、vdi等) |
| 运行性能 | 几乎无额外性能损失 | 操作系统额外的CPU、内存消耗 |
| 移植性 | 轻便、灵活、适用于Linux | 笨重,与虚拟化技术耦合度高 |
| 硬件亲和性 | 面向软件开发者 | 面向硬件运维者 |
| 部署速度 | 快速,秒级 | 较慢,10s以上 |
二、docker的基本组成
1. 镜像
-
Docker 镜像(Image)就是一个只读的模板。镜像可以用来创建 Docker 容器,一个镜像可以创建很多容器。
-
它也相当于是一个root文件系统。比如官方镜像 centos:7 就包含了完整的一套 centos:7 最小系统的 root 文件系统。
-
相当于容器的“源代码”,docker镜像文件类似于Java的类模板,而docker容器实例类似于java中new出来的实例对象。
2. 容器
-
容器就类似于一个虚拟化的运行环境,容器是用镜像创建的运行实例。
-
就像是Java中的类和实例对象一样,镜像 = 类 ,容器 = 对象
-
可以把容器看做是一个简易版的 Linux 环境(包括root用户权限、进程空间、用户空间和网络空间等)和运行在其中的应用程序。
3. 仓库
-
仓库(Repository)是集中存放镜像文件的场所。
-
仓库分为公开仓库(Public)和私有仓库(Private)两种形式。
-
类似于 Maven仓库、github仓库,Docker公司提供的官方registry被称为Docker Hub,存放各种镜像模板的地方。
三、docker的安装
1. 安装前提
-
目前,CentOS 仅发行版本中的内核支持 Docker。
-
docker必须部署在linux内核的系统上
-
Docker 运行在CentOS 7 (64-bit)上,要求
(1) 系统为64位
(2) Linux系统内核版本为3.8以上,这里选用Centos7.x
-
查看自己的内核命令,uname命令用于打印当前系统相关信息(内核版本号、硬件架构、主机名称和操作系统类型等)。
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
[root@localhost ~]# uname -r
3.10.0-1127.el7.x86_64
2. 安装步骤
安装文档:docs.docker.com/engine/inst…
- 确定你是cnetos7及以上版本
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)
- 卸载旧版本
[root@localhost ~]# sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
- yum安装gcc相关
[root@localhost ~]# ping www.baidu.com
[root@localhost ~]# yum -y install gcc
[root@localhost ~]# yum -y install gcc-c++
- 安装需要的软件包
[root@localhost ~]# yum install -y yum-utils
- 设置stable镜像仓库
# 阿里云下载,速度快,推荐
[root@localhost ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 官网下载,速度慢,不推荐
[root@localhost ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 官网下载可能出现的问题
1. [Errno 14] curl#35 - TCP connection reset by peer
2. [Errno 12] curl#35 - Timeout
- 更新yum软件包索引
[root@localhost ~]# yum makecache fast
- 安装docker ce
[root@localhost ~]# yum -y install docker-ce docker-ce-cli containerd.io
- 启动docker
[root@localhost ~]# systemctl start docker
- 测试
[root@localhost ~]# docker version
[root@localhost ~]# docker run hello-world
- 卸载
[root@localhost ~]# systemctl stop docker
[root@localhost ~]# yum remove docker-ce docker-ce-cli containerd.io
[root@localhost ~]# rm -rf /var/lib/docker
[root@localhost ~]# rm -rf /var/lib/containerd
四、配置阿里云镜像加速
描述:promotion.aliyun.com/ntms/act/ku…
登录->控制台->产品与服务->弹性计算->容器镜像服务->镜像工具->镜像加速器
加速器地址:https://sbdj03yt.mirror.aliyuncs.com
[root@localhost ~]# sudo mkdir -p /etc/docker
[root@localhost ~]# sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://sbdj03yt.mirror.aliyuncs.com"]
}
EOF
[root@localhost ~]# sudo systemctl daemon-reload
[root@localhost ~]# sudo systemctl restart docker
五、docker的常用命令
1. 帮助启动类命令
启动docker: systemctl start docker
停止docker: systemctl stop docker
重启docker: systemctl restart docker
查看docker状态: systemctl status docker
开机启动: systemctl enable docker
查看docker概要信息: docker info
查看docker总体帮助文档: docker --help
查看docker命令帮助文档: docker 具体命令 --help
2. 镜像命令
2.1 查看本地主机上的镜像
(1) REPOSITORY:表示镜像的仓库源
(2) TAG:镜像的标签版本号
(3) IMAGE ID:镜像ID
(4) CREATED:镜像创建时间
(5) SIZE:镜像大小
同一仓库源可以有多个TAG版本,代表这个仓库源的不同个版本,我们使用 REPOSITORY:TAG 来定义不同的镜像。
如果你不指定一个镜像的版本标签,例如你只使用 ubuntu,docker 将默认使用 ubuntu:latest 镜像
//列出本地主机上的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 13 months ago 13.3kB
//-a :列出本地所有的镜像(含历史映像层)
[root@localhost ~]# docker images -a
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 13 months ago 13.3kB
//-q :只显示镜像ID。
[root@localhost ~]# docker images -q
feb5d9fea6a5
2.2 查看远程仓库上的镜像
docker search 镜像名称
docker search [OPTIONS] 镜像名字
//--limit : 只列出N个镜像,默认25个
[root@localhost ~]# docker search redis
[root@localhost ~]# docker search --limit 5 redis
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
redis Redis is an open source key-value store that… 11555 [OK]
redislabs/redisinsight RedisInsight - The GUI for Redis 71
redislabs/redisearch Redis With the RedisSearch module pre-loaded… 55
redislabs/redis Clustered in-memory database engine compatib… 35
redislabs/rebloom A probablistic datatypes module for Redis 21 [OK]
(1) NAME: 镜像名称
(2) DESCRIPTION: 镜像说明
(3) STARS: 点赞数量
(4) OFFICIAL: 是否是官方的
(5) AUTOMATED: 是否是自动构建的
2.3 拉取远程仓库上的镜像
docker pull 镜像名称
docker pull 镜像名字[:TAG]
没有TAG就是最新版 等价于 docker pull 镜像名字:latest
//拉取最新版本ubuntu
[root@localhost ~]# docker pull ubuntu
//拉取5.7版本mysql
[root@localhost ~]# docker pull mysql:5.7
//拉取最新版本mysql
[root@localhost ~]# docker pull mysql:latest
2.4 查看镜像/容器/数据卷所占的空间
查看镜像/容器/数据卷所占的空间 docker system df
[root@localhost ~]# docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 4 1 901.4MB 901.4MB (99%)
Containers 1 0 0B 0B
Local Volumes 0 0 0B 0B
Build Cache 0 0 0B 0B
2.5 删除镜像
删除单个 docker rmi -f 镜像ID
删除多个 docker rmi -f 镜像ID1 镜像ID2 。。。
删除多个 docker rmi -f 镜像名1:TAG 镜像名2:TAG
删除全部 docker rmi -f $(docker images -qa)
//删除单个
[root@localhost ~]# docker rmi -f c20987f18b13
//删除多个
[root@localhost ~]# docker rmi -f 3218b38490ce feb5d9fea6a5
[root@localhost ~]# docker rmi -f mysql:5.7 redis:latest
//这里要注意 如果存在两个镜像repository都是mysql 但是版本不同 那么执行下面删除代码的时候只会删除最新版本的。 第二次执行会提示Error: No such image: mysql
[root@localhost ~]# docker rmi -f mysql
//删除全部
[root@localhost ~]# docker rmi -f $(docker images -qa)
2.6 提交镜像
docker commit提交容器副本使之成为一个新的镜像
docker commit -m="提交的描述信息" -a="作者" 容器ID 要创建的目标镜像名:[标签名]
//默认的ubuntu是没有vim命令的,现在我们安装一个vim并提交成一个新的镜像
//1.首先确认原来ubuntu容器中没有vim命令
[root@localhost ~]# docker exec -it 3a52e57d0120 /bin/bash
root@3a52e57d0120:/# vim a.txt
bash: vim: command not found
//2.安装vim命令相关包
root@3a52e57d0120:/# apt-get update
root@3a52e57d0120:/# apt-get -y install vim
//3.提交新的镜像
[root@localhost ~]# docker commit -m="add ubuntu vim" -a="tianmeng" 3a52e57d0120 vimubuntu:1.1
sha256:06d9850fbcd6b66d417bd4a0974014854888f319ab642b62d3e8f04535e0b4aa
//4.本地可以查看到刚提交的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
vimubuntu 1.1 06d9850fbcd6 About a minute ago 180MB
test/redis 2.1 32a63ddca92a 37 minutes ago 101MB
mysql latest 3218b38490ce 10 months ago 516MB
ubuntu latest ba6acccedd29 13 months ago 72.8MB
centos latest 5d0da3dc9764 14 months ago 231MB
elasticsearch latest 5acf0e8da90b 4 years ago 486MB
//5.启动新镜像,测试vim命令
[root@localhost ~]# docker run -it 06d9850fbcd6 /bin/bash
root@34e690ecdf91:/# vim aa.txt
2.7 本地镜像推送到阿里云
百度搜索阿里云并登录。搜索容器镜像服务
创建命名空间并填写其他信息
选择本地仓库
完成后进入管理页面可以看到如下脚本
将镜像推送到阿里云registry。这里是我的仓库,需要更换成自己的仓库
docker login --username=zy1432286313tm registry.cn-hangzhou.aliyuncs.com
docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/tianmeng_docker/myubuntu:[镜像版本号]
docker push registry.cn-hangzhou.aliyuncs.com/tianmeng_docker/myubuntu:[镜像版本号]
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
vimubuntu 1.1 4125ac613a99 5 seconds ago 180MB
ubuntu latest ba6acccedd29 13 months ago 72.8MB
[root@localhost ~]# docker login --username=zy1432286313tm registry.cn-hangzhou.aliyuncs.com
Password: # 这里输入密码
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[root@localhost ~]# docker tag 4125ac613a99 registry.cn-hangzhou.aliyuncs.com/tianmeng_docker/myubuntu:1.0
[root@localhost ~]# docker push registry.cn-hangzhou.aliyuncs.com/tianmeng_docker/myubuntu:1.0
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/tianmeng_docker/myubuntu]
96b195f9f0f7: Pushed
9f54eef41275: Pushed
1.0: digest: sha256:c5f8f38b86a7f3b5790c0f55d5c7827858bd5761751fd4d05463cafda04fbac6 size: 741
2.8 将阿里云上的镜像下载到本地
docker pull registry.cn-hangzhou.aliyuncs.com/tianmeng_docker/myubuntu:[镜像版本号]
[root@localhost ~]# docker pull registry.cn-hangzhou.aliyuncs.com/tianmeng_docker/myubuntu:1.0
1.0: Pulling from tianmeng_docker/myubuntu
7b1a6ab2e44d: Pull complete
ec1d83c3e291: Pull complete
Digest: sha256:c5f8f38b86a7f3b5790c0f55d5c7827858bd5761751fd4d05463cafda04fbac6
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/tianmeng_docker/myubuntu:1.0
registry.cn-hangzhou.aliyuncs.com/tianmeng_docker/myubuntu:1.0
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/tianmeng_docker/myubuntu 1.0 4125ac613a99 12 minutes ago 180MB
[root@localhost ~]# docker run -it registry.cn-hangzhou.aliyuncs.com/tianmeng_docker/myubuntu:1.0 /bin/bash
root@c060384f1dd4:/# vim a.txt
root@c060384f1dd4:/# [root@localhost ~]#
[root@localhost ~]#
2.9 本地镜像推送到私有库
(1) 下载镜像Docker Registry
docker pull registry
(2) 运行私有库Registry
docker run -d -p 5000:5000 -v /root/myregistry/:/tmp/registry --privileged=true registry
(3) 安装net-tools包
apt-get update、apt-get install net-tools
(4) 提交镜像
docker commit -m="add ubuntu net-tools" -a="tianmeng" 6f410eaca8ba netubuntu:1.1
(5) curl验证私服库上有什么镜像
curl -XGET http://192.168.198.100:5000/v2/_catalog
(6) 将新镜像修改符合私服规范的Tag
docker tag netubuntu:1.1 192.168.198.100:5000/netubuntu:1.1
(7) 修改配置文件使之支持http。cat /etc/docker/daemon.json
{ "registry-mirrors": ["sbdj03yt.mirror.aliyuncs.com"], "insecure-registries": ["192.168.198.100:5000"] }
(8) push推送到私服库
docker push 192.168.198.100:5000/netubuntu:1.1
(9) pull到本地并运行
docker pull 192.168.198.100:5000/netubuntu:1.1
//下载镜像Docker Registry
[root@localhost ~]# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
79e9f2f55bf5: Pull complete
0d96da54f60b: Pull complete
5b27040df4a2: Pull complete
e2ead8259a04: Pull complete
3790aef225b9: Pull complete
Digest: sha256:169211e20e2f2d5d115674681eb79d21a217b296b43374b8e39f97fcf866b375
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-hangzhou.aliyuncs.com/tianmeng_docker/myubuntu 1.0 4125ac613a99 39 minutes ago 180MB
registry latest b8604a3fe854 12 months ago 26.2MB
ubuntu latest ba6acccedd29 13 months ago 72.8MB
//运行私有库Registry,相当于本地有个私有Docker hub
//默认情况,仓库被创建在容器的/var/lib/registry目录下,建议自行用容器卷映射,方便于宿主机联调
[root@localhost ~]# docker run -d -p 5000:5000 -v /root/myregistry/:/tmp/registry --privileged=true registry
b4e85a4a3fc09ce6f4651a7d04bd82677b81085279689034c9ebd6f858358291
//ubuntu镜像里默认没有ifconfig命令
[root@localhost ~]# docker run -it ubuntu /bin/bash
root@6f410eaca8ba:/# ifconfig
bash: ifconfig: command not found
//安装net-tools包
root@6f410eaca8ba:/# apt-get update
root@6f410eaca8ba:/# apt-get install net-tools
//提交镜像
[root@localhost ~]# docker commit -m="add ubuntu net-tools" -a="tianmeng" 6f410eaca8ba netubuntu:1.1
sha256:6a54e34539380057609eea6891e81a18edc57809370da2b5853ca0db19ba7d92
//curl验证私服库上有什么镜像 可以看到此刻仓库是空的
[root@localhost ~]# curl -XGET http://192.168.198.100:5000/v2/_catalog
{"repositories":[]}
//将新镜像netubuntu:1.1修改符合私服规范的Tag
//公式:docker tag 镜像:Tag Host:Port/Repository:Tag
[root@localhost ~]# docker tag netubuntu:1.1 192.168.198.100:5000/netubuntu:1.1
//修改配置文件使之支持http
[root@localhost docker]# vim /etc/docker/daemon.json
//修改完成内容如下
//docker默认不允许http方式推送镜像,通过配置选项来取消这个限制。====> 修改完后如果不生效,建议重启docker
[root@localhost docker]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://sbdj03yt.mirror.aliyuncs.com"],
"insecure-registries": ["192.168.198.100:5000"]
}
//push推送到私服库
[root@localhost docker]# docker push 192.168.198.100:5000/netubuntu:1.1
失败情况 使用systemctl restart docker重启一下就可以
The push refers to repository [192.168.198.100:5000/netubuntu]
Get "https://192.168.198.100:5000/v2/": http: server gave HTTP response to HTTPS client
成功情况
The push refers to repository [192.168.198.100:5000/netubuntu]
f272dbf4e54c: Pushed
9f54eef41275: Pushed
1.1: digest: sha256:8877b36a91a9909eeac1d33a81cfe99a6ef8b50e4a148e0e1e551f6e0ea3922c size: 741
//私服库里已经可以看到了
[root@localhost docker]# curl -XGET http://192.168.198.100:5000/v2/_catalog
{"repositories":["netubuntu"]}
//pull到本地并运行
[root@localhost docker]# docker pull 192.168.198.100:5000/netubuntu:1.1
1.1: Pulling from netubuntu
7b1a6ab2e44d: Already exists
3ad91251d028: Pull complete
Digest: sha256:8877b36a91a9909eeac1d33a81cfe99a6ef8b50e4a148e0e1e551f6e0ea3922c
Status: Downloaded newer image for 192.168.198.100:5000/netubuntu:1.1
192.168.198.100:5000/netubuntu:1.1
//启动 验证ifconfig命令
[root@localhost docker]# docker run -it 6a54e3453938 /bin/bash
root@56fba77a2055:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 8 bytes 656 (656.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
3. 容器命令
3.1 新建 + 启动容器
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
(1) --name="容器新名字" 为容器指定一个名称;
(2) -d: 后台运行容器并返回容器ID,也即启动守护式容器(后台运行);
(3) -i:以交互模式运行容器,通常与 -t 同时使用;
(4) -t:为容器重新分配一个伪输入终端,通常与 -i 同时使用;也即启动交互式容器(前台有伪终端,等待交互);
(5) -P: 随机端口映射,大写P
(6) -p: 指定端口映射,小写p
(7) 退出终端 直接输入exit
| 参数 | 说明 |
|---|---|
| -p hostPort:containerPort | 端口映射 -p 8080:80 |
| -p ip:hostPort:containerPort | 配置监听地址 -p 10.0.0.100:8080:80 |
| -p ip::containerPort | 随机分配端口 -p 10.0.0.100::80 |
| -p hostPort:containerPort:udp | 指定协议 -p 8080:80:tcp |
| -p 81:80 -p 443:443 | 指定多个 |
[root@localhost ~]# docker run -it centos /bin/bash
[root@54c7f1da2a82 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@localhost ~]# docker run -d centos
c34eaa721946d85ff4b1b44cc72e1b7a39fa100e9eb60e9b5b133e0209fe236e
3.2 查询当前正在运行的容器
docker ps [OPTIONS]
OPTIONS说明:
(1) -a :列出当前所有正在运行的容器+历史上运行过的
(2) -l :显示最近创建的容器
(3) -n:显示最近n个创建的容器
(4) -q :静默模式,只显示容器编号。
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da7aef26f394 centos "/bin/bash" 24 seconds ago Up 23 seconds brave_maxwell
c34eaa721946 centos "/bin/bash" 6 minutes ago Exited (0) 6 minutes ago frosty_zhukovsky
54c7f1da2a82 centos "/bin/bash" 12 minutes ago Exited (0) 12 minutes ago naughty_curie
f5273e1cd124 feb5d9fea6a5 "/hello" 2 hours ago Exited (0) 2 hours ago condescending_jackson
[root@localhost ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da7aef26f394 centos "/bin/bash" 47 seconds ago Up 47 seconds brave_maxwell
[root@localhost ~]# docker ps -n
flag needs an argument: 'n' in -n
See 'docker ps --help'.
[root@localhost ~]# docker ps -q
da7aef26f394
3.3 退出容器
两种退出方式
(1) exit :run进去容器,exit退出,容器停止
(2) ctrl + p + q :run进去容器,ctrl + p + q退出,容器不停止
[root@localhost ~]# docker run -it centos /bin/bash
[root@da7aef26f394 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@da7aef26f394 /]# [root@localhost ~]#
3.4 启动已停止运行的容器
docker start 容器ID或者容器名
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da7aef26f394 centos "/bin/bash" 11 minutes ago Up 11 minutes brave_maxwell
c34eaa721946 centos "/bin/bash" 17 minutes ago Exited (0) 3 minutes ago frosty_zhukovsky
54c7f1da2a82 centos "/bin/bash" 23 minutes ago Exited (0) 22 minutes ago naughty_curie
f5273e1cd124 feb5d9fea6a5 "/hello" 2 hours ago Exited (0) 2 hours ago condescending_jackson
[root@localhost ~]# docker start 54c7f1da2a82
54c7f1da2a82
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da7aef26f394 centos "/bin/bash" 11 minutes ago Up 11 minutes brave_maxwell
c34eaa721946 centos "/bin/bash" 17 minutes ago Exited (0) 4 minutes ago frosty_zhukovsky
54c7f1da2a82 centos "/bin/bash" 23 minutes ago Up 3 seconds naughty_curie
f5273e1cd124 feb5d9fea6a5 "/hello" 2 hours ago Exited (0) 2 hours ago condescending_jackson
3.5 重启容器
docker restart 容器ID或者容器名
3.6 停止容器
docker stop 容器ID或者容器名
3.7 强制停止容器
docker kill 容器ID或容器名
3.8 删除已停止的容器
docker rm 容器ID
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da7aef26f394 centos "/bin/bash" 15 minutes ago Up About a minute brave_maxwell
c34eaa721946 centos "/bin/bash" 21 minutes ago Exited (0) 2 minutes ago frosty_zhukovsky
54c7f1da2a82 centos "/bin/bash" 27 minutes ago Up 4 minutes naughty_curie
f5273e1cd124 feb5d9fea6a5 "/hello" 2 hours ago Exited (0) 2 minutes ago condescending_jackson
[root@localhost ~]# docker rm c34eaa721946
c34eaa721946
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da7aef26f394 centos "/bin/bash" 16 minutes ago Up 2 minutes brave_maxwell
54c7f1da2a82 centos "/bin/bash" 28 minutes ago Up 4 minutes naughty_curie
f5273e1cd124 feb5d9fea6a5 "/hello" 2 hours ago Exited (0) 2 minutes ago condescending_jackson
//运行容器删除会报错
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
da7aef26f394 centos "/bin/bash" 16 minutes ago Up 2 minutes brave_maxwell
54c7f1da2a82 centos "/bin/bash" 28 minutes ago Up 4 minutes naughty_curie
f5273e1cd124 feb5d9fea6a5 "/hello" 2 hours ago Exited (0) 2 minutes ago condescending_jackson
[root@localhost ~]# docker rm da7aef26f394
Error response from daemon: You cannot remove a running container da7aef26f39495c93f53a4f5c1745f17414813ad6b0fac51d2ea4a2de78439ae. Stop the container before attempting removal or force remove
3.9 强制删除未停止的容器
docker rm -f 容器ID
[root@localhost ~]# docker rm -f da7aef26f394
da7aef26f394
3.10 一次性删除多个容器实例
(1) docker rm -f $(docker ps -a -q)
(2) docker ps -a -q | xargs docker rm
[root@localhost ~]# docker rm -f $(docker ps -a -q)
54c7f1da2a82
f5273e1cd124
3.11 启动守护式容器
前台交互式启动:docker run -it redis:6.0.8
后台守护式启动:docker run -d redis:6.0.8
问题
以后台模式启动centos和redis
(1) docker run -d centos
(2) docker run -d redis:6.0.8
然后使用docker ps -a进行查看, 会发现centos容器已经退出,redis继续运行。这里很重要的要说明的一点: Docker容器后台运行,就必须有一个前台进程。容器运行的命令如果不是那些一直挂起的命令(比如运行top,tail),就是会自动退出的。这个是docker的机制问题,比如你的web容器,我们以nginx为例,正常情况下,我们配置启动服务只需要启动响应的service即可。例如service nginx start。但是,这样做,nginx为后台进程模式运行,就导致docker前台没有运行的应用,这样的容器后台启动后,会立即自杀因为他觉得他没事可做了。所以,最佳的解决方案是,将你要运行的程序以前台进程的形式运行,常见就是命令行模式,表示我还有交互操作,别中断。
[root@localhost ~]# docker run -d redis:6.0.8
d2ef87ffe8211602f247c1a4c0bf7c6b11afa1dca1e3a6a8ac628b247c420142
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d2ef87ffe821 redis:6.0.8 "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 6379/tcp determined_wiles
[root@localhost ~]# docker run -d centos
d7d7650d31eb3c2179ad59e0a880b151b573f62cc1dba54d116b1acde27ae4b7
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d7d7650d31eb centos "/bin/bash" 2 seconds ago Exited (0) 2 seconds ago priceless_driscoll
d2ef87ffe821 redis:6.0.8 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 6379/tcp determined_wiles
3.12 查看容器日志
docker logs 容器ID
[root@localhost ~]# docker logs d2ef87ffe821
1:C 14 Nov 2022 08:20:11.773 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 14 Nov 2022 08:20:11.774 # Redis version=6.0.8, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 14 Nov 2022 08:20:11.774 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 14 Nov 2022 08:20:11.779 * Running mode=standalone, port=6379.
1:M 14 Nov 2022 08:20:11.780 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 14 Nov 2022 08:20:11.780 # Server initialized
1:M 14 Nov 2022 08:20:11.780 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 14 Nov 2022 08:20:11.780 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo madvise > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled (set to 'madvise' or 'never').
1:M 14 Nov 2022 08:20:11.781 * Ready to accept connections
3.13 查看容器内运行的进程
docker top 容器ID
[root@localhost ~]# docker top d2ef87ffe821
UID PID PPID C STIME TTY TIME CMD
polkitd 10117 10096 0 00:20 ? 00:00:03 redis-server *:6379
3.13 查看容器内部细节
docker inspect 容器ID
3.14 进入正在运行的容器并以命令行交互
docker run -it ubuntu /bin/bash
docker exec -it d2ef87ffe821 redis-cli
docker exec -it d2ef87ffe821 /bin/bash
docker attach 5f83fac9d892
(1) attach直接进入容器启动命令的终端,不会启动新的进程 用exit退出,会导致容器的停止。
(2) exec是在容器中打开新的终端,并且可以启动新的进程 用exit退出,不会导致容器的停止。(推荐)
(3) 如果使用ctrl + p + q退出,那么attach和exec都不会导致容器的停止。
[root@localhost ~]# docker exec -it d2ef87ffe821 redis-cli
[root@localhost ~]# docker exec -it d2ef87ffe821 /bin/bash
[root@localhost ~]# docker attach 5f83fac9d892
3.15 从容器内拷贝文件到主机上
容器 -> 主机
docker cp 容器ID:容器内路径 目的主机路径
3.16 导入和导出容器
(1) docker export 容器ID > 文件名.tar
(2) cat 文件名.tar | docker import - 镜像用户/镜像名:镜像版本号
export 导出容器的内容留作为一个tar归档文件[对应import命令]
import 从tar包中的内容创建一个新的文件系统再导入为镜像[对应export]
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7c4ccb473b9e centos "/bin/bash" 20 minutes ago Up 20 minutes musing_mcnulty
d2ef87ffe821 redis:6.0.8 "docker-entrypoint.s…" 17 hours ago Up 17 hours 6379/tcp determined_wiles
[root@localhost ~]# docker export d2ef87ffe821 > redis.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg Desktop Documents Downloads Music original-ks.cfg Pictures Public redis.tar.gz Templates Videos
3.17 容器数据卷
(1) 卷就是目录或文件,存在于一个或多个容器中,由docker挂载到容器,但不属于联合文件系统,因此能够绕过Union File System提供一些用于持续存储或共享数据的特性。
(2) 卷的设计目的就是数据的持久化,完全独立于容器的生存周期,因此Docker不会在容器删除时删除其挂载的数据卷。
(3) 有点类似我们Redis里面的rdb和aof文件。其实就是将docker容器内的数据保存进宿主机的磁盘中。
(4) 运行一个带有容器卷存储功能的容器实例:
docker run -it --privileged=true -v /宿主机绝对路径目录:/容器内目录 镜像名
如: docker run -d -p 5000:5000 -v /root/myregistry/:/tmp/registry --privileged=true registry
(5) 将运用与运行的环境打包镜像,run后形成容器实例运行,但是我们对数据的要求希望是持久化的。Docker容器产生的数据,如果不备份,那么当容器实例删除后,容器内的数据自然也就没有了。为了能保存数据在docker中我们使用卷。
(6) 特点:
数据卷可在容器之间共享或重用数据、卷中的更改可以直接实时生效、数据卷中的更改不会包含在镜像的更新中、数据卷的生命周期一直持续到没有容器使用它为止。
3.17.1 宿主vs容器之间映射添加容器卷
# 宿主vs容器之间映射添加容器卷
[root@localhost ~]# docker run -it --name myubuntu --privileged=true -v /tmp/myHostData:/tmp/myDockerData ubuntu /bin/bash
root@9a32e157bf8c:/#
# 查看容器卷是否挂载成功
[root@localhost ~]# docker inspect 9a32e157bf8c
[
....
"Mounts": [
{
"Type": "bind",
"Source": "/tmp/myHostData",
"Destination": "/tmp/myDockerData",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
]
....
]
# 容器和宿主机之间数据共享
# 1.docker修改,主机同步获得
# 2.主机修改,docker同步获得
# 3.docker容器stop,主机修改,docker容器重启看数据是否同步。
[root@localhost ~]# cd /tmp/myHostData
[root@localhost myHostData]# ls
[root@localhost myHostData]# mkdir a.txt
[root@localhost myHostData]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a32e157bf8c ubuntu "/bin/bash" 12 minutes ago Up 12 minutes myubuntu
[root@localhost myHostData]# docker exec -it 9a32e157bf8c /bin/bash
root@9a32e157bf8c:/# cd /tmp/myDockerData/
root@9a32e157bf8c:/tmp/myDockerData# ls
a.txt
root@9a32e157bf8c:/tmp/myDockerData# mkdir b.txt
root@9a32e157bf8c:/tmp/myDockerData# read escape sequence
[root@localhost myHostData]# ls
a.txt b.txt
[root@localhost myHostData]# docker stop 9a32e157bf8c
9a32e157bf8c
[root@localhost myHostData]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost myHostData]# mkdir c.txt
[root@localhost myHostData]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a32e157bf8c ubuntu "/bin/bash" 15 minutes ago Exited (130) About a minute ago myubuntu
56fba77a2055 6a54e3453938 "/bin/bash" 19 hours ago Exited (137) 16 minutes ago cool_satoshi
82b62ab803d2 registry "/entrypoint.sh /etc…" 19 hours ago Exited (137) 16 minutes ago laughing_dijkstra
[root@localhost myHostData]# docker restart 9a32e157bf8c
9a32e157bf8c
[root@localhost myHostData]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a32e157bf8c ubuntu "/bin/bash" 16 minutes ago Up 3 seconds myubuntu
[root@localhost myHostData]# docker exec -it 9a32e157bf8c /bin/bash
root@9a32e157bf8c:/# cd /tmp/myDockerData/
root@9a32e157bf8c:/tmp/myDockerData# ls
a.txt b.txt c.txt
3.17.2 读写规则映射添加说明
(1) 读写(默认):rw = read + write
docker run -it --privileged=true -v /宿主机绝对路径目录:/容器内目录:rw 镜像名
(2) 只读:此时如果宿主机写入内容,可以同步给容器内,容器可以读取到。容器自己只能读取不能写 ro = read only
docker run -it --privileged=true -v /宿主机绝对路径目录:/容器内目录:ro 镜像名
3.17.3 卷的继承与共享
(1) 容器1完成和宿主机的映射
docker run -it --privileged=true -v /mydocker/u:/tmp --name u1 ubuntu
(2) 容器2继承容器1的卷规则
docker run -it --privileged=true --volumes-from 父类 --name u2 ubuntu
docker run -it --privileged=true --volumes-from u1 --name u2 ubuntu
3.18 常用命令总结
attach Attach to a running container # 当前 shell 下 attach 连接指定运行镜像
build Build an image from a Dockerfile # 通过 Dockerfile 定制镜像
commit Create a new image from a container changes # 提交当前容器为新的镜像
cp Copy files/folders from the containers filesystem to the host path #从容器中拷贝指定文件或者目录到宿主机中
create Create a new container # 创建一个新的容器,同 run,但不启动容器
diff Inspect changes on a container's filesystem # 查看 docker 容器变化
events Get real time events from the server # 从 docker 服务获取容器实时事件
exec Run a command in an existing container # 在已存在的容器上运行命令
export Stream the contents of a container as a tar archive # 导出容器的内容流作为一个 tar 归档文件[对应 import ]
history Show the history of an image # 展示一个镜像形成历史
images List images # 列出系统当前镜像
import Create a new filesystem image from the contents of a tarball # 从tar包中的内容创建一个新的文件系统映像[对应export]
info Display system-wide information # 显示系统相关信息
inspect Return low-level information on a container # 查看容器详细信息
kill Kill a running container # kill 指定 docker 容器
load Load an image from a tar archive # 从一个 tar 包中加载一个镜像[对应 save]
login Register or Login to the docker registry server # 注册或者登陆一个 docker 源服务器
logout Log out from a Docker registry server # 从当前 Docker registry 退出
logs Fetch the logs of a container # 输出当前容器日志信息
port Lookup the public-facing port which is NAT-ed to PRIVATE_PORT # 查看映射端口对应的容器内部源端口
pause Pause all processes within a container # 暂停容器
ps List containers # 列出容器列表
pull Pull an image or a repository from the docker registry server # 从docker镜像源服务器拉取指定镜像或者库镜像
push Push an image or a repository to the docker registry server # 推送指定镜像或者库镜像至docker源服务器
restart Restart a running container # 重启运行的容器
rm Remove one or more containers # 移除一个或者多个容器
rmi Remove one or more images # 移除一个或多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]
run Run a command in a new container # 创建一个新的容器并运行一个命令
save Save an image to a tar archive # 保存一个镜像为一个 tar 包[对应 load]
search Search for an image on the Docker Hub # 在 docker hub 中搜索镜像
start Start a stopped containers # 启动容器
stop Stop a running containers # 停止容器
tag Tag an image into a repository # 给源中镜像打标签
top Lookup the running processes of a container # 查看容器中运行的进程信息
unpause Unpause a paused container # 取消暂停容器
version Show the docker version information # 查看 docker 版本号
wait Block until a container stops, then print its exit code # 截取容器停止时的退出状态值
六、docker的镜像
1. 概念
是一种轻量级、可执行的独立软件包,它包含运行某个软件所需的所有内容,我们把应用程序和配置依赖打包好形成一个可交付的运行环境(包括代码、运行时需要的库、环境变量和配置文件等),这个打包好的运行环境就是image镜像文件。只有通过这个镜像文件才能生成Docker容器实例(类似Java中new出来一个对象)。
2. 镜像的分层
Docker中的镜像分层,支持通过扩展现有镜像,创建新的镜像。类似Java继承于一个Base基础类,自己再按需扩展。
新镜像是从 base 镜像一层一层叠加生成的。每安装一个软件,就在现有镜像的基础上增加一层
镜像是分层的,在使用pull命令拉取镜像的时候可以看出
[root@localhost ~]# docker pull elasticsearch
Using default tag: latest
latest: Pulling from library/elasticsearch
05d1a5232b46: Pull complete
5cee356eda6b: Pull complete
89d3385f0fd3: Pull complete
65dd87f6620b: Pull complete
78a183a01190: Pull complete
1a4499c85f97: Pull complete
2c9d39b4bfc1: Pull complete
1b1cec2222c9: Pull complete
59ff4ce9df68: Pull complete
1976bc3ee432: Pull complete
5af49e8af381: Pull complete
42c8b75ff7af: Pull complete
7e6902915254: Pull complete
99853874fa54: Pull complete
596fbad6fcff: Pull complete
Digest: sha256:a8081d995ef3443dc6d077093172a5931e02cdb8ffddbf05c67e01d348a9770e
Status: Downloaded newer image for elasticsearch:latest
docker.io/library/elasticsearch:latest
3. 联合文件系统
UnionFS(联合文件系统):Union文件系统(UnionFS)是一种分层、轻量级并且高性能的文件系统,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual filesystem)。Union 文件系统是 Docker 镜像的基础。镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。 特性:一次同时加载多个文件系统,但从外面看起来,只能看到一个文件系统,联合加载会把各层文件系统叠加起来,这样最终的文件系统会包含所有底层的文件和目录
4. 镜像加载原理
(1) docker的镜像实际上由一层一层的文件系统组成,这种层级的文件系统UnionFS(联合文件系统)。
(2) bootfs(boot file system)主要包含bootloader和kernel, bootloader主要是引导加载kernel, Linux刚启动时会加载bootfs文件系统,在Docker镜像的最底层是引导文件系统bootfs。这一层与我们典型的Linux/Unix系统是一样的,包含boot加载器和内核。当boot加载完成之后整个内核就都在内存中了,此时内存的使用权已由bootfs转交给内核,此时系统也会卸载bootfs。
(3) rootfs (root file system) ,在bootfs之上。包含的就是典型 Linux 系统中的 /dev, /proc, /bin, /etc 等标准目录和文件。rootfs就是各种不同的操作系统发行版,比如Ubuntu,Centos等等。
(4) 平时我们安装进虚拟机的CentOS都是好几个G,为什么docker这里才200M??
对于一个精简的OS,rootfs可以很小,只需要包括最基本的命令、工具和程序库就可以了,因为底层直接用Host的kernel,自己只需要提供 rootfs 就行了。由此可见对于不同的linux发行版, bootfs基本是一致的, rootfs会有差别, 因此不同的发行版可以公用bootfs。
5. 镜像分层的好处
镜像分层最大的一个好处就是共享资源,方便复制迁移,就是为了复用。
比如说有多个镜像都从相同的 base 镜像构建而来,那么 Docker Host 只需在磁盘上保存一份 base 镜像;
同时内存中也只需加载一份 base 镜像,就可以为所有容器服务了。而且镜像的每一层都可以被共享。
6. 容器层与镜像层
当容器启动时,一个新的可写层被加载到镜像的顶部。这一层通常被称作“容器层”,“容器层”之下的都叫“镜像层”。
所有对容器的改动 - 无论添加、删除、还是修改文件都只会发生在容器层中。只有容器层是可写的,容器层下面的所有镜像层都是只读的。