扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
1.Docker核心概念
2.Docker镜像操作
3.Docker容器操作
4.Docker资源控制
5.Docker数据卷管理创新互联是一家专业提供应县企业网站建设,专注与成都网站设计、成都网站建设、外贸网站建设、H5响应式网站、小程序制作等业务。10年已为应县众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。
1.是一种轻量级的“虚拟机”
2.在Linux容器里运行应用的开源工具
参考案例指导文档表1-1
1.打包应用程序简化部署
2.可脱离底层硬件任意迁移
3.例:服务器从腾讯云迁移到阿里云
1.镜像
2.容器
3.仓库(共有,私有)
1.使用CURL获得Docker的安装脚本进行安装
2.使用YUM仓库来安装Docker
[root@localhost ~]# yum install yum-utils device-mapper-persistent-data lvm2 -y
[root@localhost ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@localhost ~]# ls /etc/yum.repos.d/
CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo docker-ce.repo
#此时会显示docker的镜像源
[root@localhost ~]# yum install -y docker-ce
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl start docker.service
[root@localhost ~]# systemctl enable docker.service
[root@localhost ~]# docker search nginx #搜索nginx镜像
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
nginx Official build of Nginx. 12375 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1704 [OK]
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 749 [OK]
linuxserver/nginx An Nginx container, brought to you by LinuxS… 84
......以下省略多项
#显示出的仓库中的镜像按星级进行排名,星级越高,下载人数越多,自然就越靠谱
[root@localhost ~]# docker pull nginx
#此时我们不使用加速进行下载
Using default tag: latest
latest: Pulling from library/nginx
000eee12ec04: Downloading 3.083MB/27.09MB
eb22865337de: Downloading 1.703MB/23.74MB
bee5d581ef8b: Download complete
#此时速度会非常慢,有时候甚至会出现卡顿
#此时我们可以在此处做一个优化操作就是:镜像加速
`此命令可以在阿里云的官网上搜索“官方镜像加速”从而获取到自己的命令代码(需要先注册阿里云平台的账号)`
`找到控制台,登陆后找到左侧的“产品与服务”,再找到“容器镜像服务”,再找到左侧的“镜像加速器”,在“操作文档”中选择自己的系统,此处我们需要选择“CentOS”,就可以找到自己账号生成的镜像加速配置文件`
[root@localhost ~]# tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://w1ogxqvl.mirror.aliyuncs.com"]
}
EOF
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
000eee12ec04: Pull complete
eb22865337de: Pull complete
bee5d581ef8b: Pull complete
Digest: sha256:50cf965a6e08ec5784009d0fccb380fc479826b6e0e65684d9879170a9df8566
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
#此时很快就可以下载完成了
[root@localhost ~]# docker images #查看下载下来的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 231d40e811cd 4 weeks ago 126MB
docker create [选项]镜像运行的程序
-i让容器的标准输入保持打开
-t 让Docker分配一个伪终端
docker ps [选项]
-a列出最近一次启动的容器
docker start容器的ID/名称
docker run [选项]镜像运行的程序执行的命令
-d在后台进行运行
docker stop容器的ID/名称
docker exec [选项]容器的ID/名称/bin/bash
docker export容器的ID/名称>文件名
cat文件名| docker import -生成的镜像名称:标签
docker rm [选项]容器的ID/名称
[root@localhost ~]# docker inspect 231d40e811cd #查看镜像信息
[
{
"Id": "sha256:231d40e811cd970168fb0c4770f2161aa30b9ba6fe8e68527504df69643aa145",
"RepoTags": [
"nginx:latest"
],
"RepoDigests": [
......以下省略多行
[root@localhost ~]# docker tag nginx:latest nginx:web #重新定义标签
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 231d40e811cd 4 weeks ago 126MB
nginx web 231d40e811cd 4 weeks ago 126MB
[root@localhost ~]# docker images | grep nginx
nginx latest 231d40e811cd 4 weeks ago 126MB
nginx web 231d40e811cd 4 weeks ago 126MB
#删除镜像
[root@localhost ~]# docker rmi nginx:web
Untagged: nginx:web
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 231d40e811cd 4 weeks ago 126MB
#存出镜像
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
containerd rh
[root@localhost opt]# docker save -o nginx nginx:latest
[root@localhost opt]# ls
containerd nginx rh
[root@localhost opt]# ls -lh
总用量 125M
drwx--x--x. 4 root root 28 12月 24 11:01 containerd
-rw-------. 1 root root 125M 12月 24 11:17 nginx
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
[root@localhost ~]# docker images #此时Docker镜像是空的
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost opt]# scp /opt/nginx root@192.168.18.145:/opt/
The authenticity of host '192.168.18.145 (192.168.18.145)' can't be established.
ECDSA key fingerprint is SHA256:mTT+FEtzAu4X3D5srZlz93S3gye8MzbqVZFDzfJd4Gk.
ECDSA key fingerprint is MD5:fa:5a:88:23:49:60:9b:b8:7e:4b:14:4b:3f:cd:96:a0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.18.145' (ECDSA) to the list of known hosts.
root@192.168.18.145's password:
nginx 100% 124MB 124.0MB/s 00:01
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
containerd nginx rh
[root@localhost opt]# docker load < nginx #导入过程
831c5620387f: Loading layer 72.48MB/72.48MB
5fb987d2e54d: Loading layer 57.67MB/57.67MB
4fc1aa8003a3: Loading layer 3.584kB/3.584kB
Loaded image: nginx:latest
[root@localhost opt]# docker images #查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 231d40e811cd 4 weeks ago 126MB
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost opt]# docker create -it nginx:latest /bin/bash #容器创建
82013604d57b7e0cbad870b367ca241579581a3c34d977aec58157a89fe9188e
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
82013604d57b nginx:latest "/bin/bash" 18 seconds ago Created laughing_robinson
[root@localhost opt]# docker start 82013604d57b #启动
82013604d57b
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
82013604d57b nginx:latest "/bin/bash" 2 minutes ago Up 20 seconds 80/tcp laughing_robinson
#此时状态为up,端口为80
[root@localhost opt]# docker search centos7
[root@localhost opt]# docker pull paigeeworld/centos7
[root@localhost opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 231d40e811cd 4 weeks ago 126MB
paigeeworld/centos7 latest 4cbe7aa905e7 5 years ago 382MB
[root@localhost opt]# docker create -it paigeeworld/centos7 /bin/bash
fe74e5e0e354e855f76b2edc043358c71f9a2e99d10ac3875c610e1fd9dac4d7
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe74e5e0e354 paigeeworld/centos7 "/bin/bash" 8 seconds ago Created trusting_satoshi
#此时只是Created创建好的状态
[root@localhost opt]# docker start fe74e5e0e354 #启动
fe74e5e0e354
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe74e5e0e354 paigeeworld/centos7 "/bin/bash" About a minute ago Up 11 seconds trusting_satoshi
82013604d57b nginx:latest "/bin/bash" 8 minutes ago Up 6 minutes 80/tcp laughing_robinson
#此时是Up开启的状态
[root@localhost opt]# docker run paigeeworld/centos7 /usr/bin/bash -c ls /
#查看镜像根目录下有哪些文件
bin
boot
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d04961e9862f paigeeworld/centos7 "/usr/bin/bash -c ls…" About a minute ago Exited (0) About a minute ago frosty_pare
#docker run = 镜像做了create然后start,再进入到容器中执行了ls,最后执行了stop
#进入容器
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d04961e9862f paigeeworld/centos7 "/usr/bin/bash -c ls…" 15 minutes ago Exited (0) 15 minutes ago frosty_pare
fe74e5e0e354 paigeeworld/centos7 "/bin/bash" 22 minutes ago Up 21 minutes trusting_satoshi
82013604d57b nginx:latest "/bin/bash" 30 minutes ago Up 28 minutes 80/tcp laughing_robinson
[root@localhost opt]# docker exec -it fe74e5e0e354 /bin/bash #选择运行的容器进入环境
bash-4.2# ls /
bin dev home lib64 media opt root sbin sys usr
boot etc lib lost+found mnt proc run srv tmp var
bash-4.2# exit
exit
#终止运行
[root@localhost opt]# docker stop fe74e5e0e354 #停止
fe74e5e0e354
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe74e5e0e354 paigeeworld/centos7 "/bin/bash" 25 minutes ago Exited (137) 4 seconds ago trusting_satoshi
#此时状态为退出
1.通过--cpu-quota选项来限制CPU的使用率
2.通过修改配置文件cpu.cfs_quota_us实现
docker run --cpu-shares 1024容器A
docker run --cpu-shares 1024容器B
docker run --cpu-shares 2048容器C
[root@localhost ~]# docker run --cpu-quota 20000 nginx:latest
#该容器在电脑中所占资源只有20%,如果没有设置它就会对别的资源进行抢占
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e1e8a4aef629 nginx:latest "nginx -g 'daemon of…" 5 seconds ago Up 4 seconds 80/tcp strange_dubinsky
#此时会显示一个新的进程(需要新开一个终端进行查看)
[root@localhost ~]# cd /sys/fs/cgroup/cpu/docker/
[root@localhost docker]# ls
cgroup.procs cpu.shares
cpuacct.stat cpu.stat
cpuacct.usage e1e8a4aef6293bb2f116fe219ef0e2e190d9e094d79bb583925b49d0af3e9e67
#此时会在配置文件的目录下找到以’e1e8a4aef629’开头的配置文件
[root@localhost docker]# cd e1e8a4aef6293bb2f116fe219ef0e2e190d9e094d79bb583925b49d0af3e9e67
[root@localhost e1e8a4aef6293bb2f116fe219ef0e2e190d9e094d79bb583925b49d0af3e9e67]# ls
cgroup.clone_children cpuacct.usage cpu.rt_period_us notify_on_release
cgroup.event_control cpuacct.usage_percpu cpu.rt_runtime_us tasks
cgroup.procs cpu.cfs_period_us cpu.shares
cpuacct.stat cpu.cfs_quota_us cpu.stat
[root@localhost e1e8a4aef6293bb2f116fe219ef0e2e190d9e094d79bb583925b49d0af3e9e67]# cat cpu.cfs_quota_us #此quota文件可以设置上限值
20000
[root@localhost docker]# docker pull paigeeworld/centos7
#需要再下载一个cento7的镜像
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 231d40e811cd 4 weeks ago 126MB
paigeeworld/centos7 latest 4cbe7aa905e7 5 years ago 382MB
`创建两个容器为c1和c2,若只有这两个容器,设置容器的权重,使得c1和c2的CPU资源占比为33.3%和66.7%`
[root@localhost docker]# docker run -itd --name cl --cpu-shares 512 paigeeworld/centos7
ab596821561386ec6519652606b79b363e548f29ad2c289480f7a577e8f5803b
[root@localhost docker]# docker run -itd --name c2 --cpu-shares 1024 paigeeworld/centos7
462562fbf8ba7614328c3afde41bcc466dd329fbfe79317b5ec0e6e5cefde71d
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
462562fbf8ba paigeeworld/centos7 "/bin/bash" 35 seconds ago Up 34 seconds c2
ab5968215613 paigeeworld/centos7 "/bin/bash" 36 seconds ago Up 36 seconds cl
#此时就会产生cl和c2两个新的容器了,分别占用1/3和2/3
[root@localhost docker]# docker run --name c3 --cpuset-cpus 0,1 paigeeworld/centos7
#此时可以使用到两个核心数资源
[root@localhost docker]# docker run --name c4 -m 512m paigeeworld/centos7
[root@localhost docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
580c4e066308 paigeeworld/centos7 "/bin/bash" 3 seconds ago Exited (0) 2 seconds ago c4
--device-read-bps:限制读某个设备的bps(数据量)
[root@localhost dev]# docker run -d --device-read-bps /dev/sda:30mb paigeeworld/centos7
627b05085a2305f5e2ef5a3725458e25b85441c8e400402f72f562fb71813ab1
--device-write-bps:限制写入某个设备的bps(数据量)
例:docker run -d --device-write-bps /dev/sda:30mb paigeeworld/centos7
--device-read-iops:限制读某个设备的iops (次数)
--device-write-iops:限制写入某个设备的iops (次数)
1.方便查看容器内产生的数据
2.多容器间实现数据共享
1.数据卷
2.数据卷容器
数据卷:直接在centos上添加,可实现数据共享,实现的是宿主和容器之间的数据共享
数据卷容器:容器之间实现共享,和宿主机没有关系,A容器中的a目录与B容器上的d目录实现数据共享
docker run -d -v /data1 -v /data2 --name web
httpd:centos
docker run -d -v /var/www:/data1 --name web-1
httpd:centos
docker run -it --volumes-from web --name db1
httpd:centos /bin/bash
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流