赞
踩
Docker 核心概念
Docker 镜像操作
Docker 容器操作
Docker 资源控制
Docker 数据卷管理
官方网站:www.docker.com
官方文档:docs.docker.com
docker是一种轻量级的”虚拟机“
在linux容器里运行的开源工具
容器内的应用程序之间相互隔离、互不影响
便于应用拆分、解耦
例:LNMP——部署到同一台主机中
linux ——容器1
nginx——容器2
mysql——容器3
php-fpm——容器4
应用之间不受影响,可以方便迁移
daocker是一个开源的应用容器引擎(相当于vm 15就是虚拟机的引擎),让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中;然后发布到任何流行的linux或windows机器上,也可以实现虚拟化
容器是完全使用沙箱机制,相互之间不会有任何接口
docker不需要虚拟出来硬件,比如虚拟cpu、虚拟磁盘;docker是进程之间的隔离
提供简单的应用程序打包工具
开发人员和运维人员职责逻辑分离
多环境保持一致性
输入的指令交由客户端,客户端转发给守护进程处理
守护进程识别处理命令,比如说制造镜像images;也可以从仓库(regisstry,分为公有和私有)中拉取,从这中间需要一个加速器,后面去配置一个阿里云加速器
利用镜像images,直接制造容器containors
容器会开放端口,用以客户访问
彼此进程之间是相互隔离的,采用的就是解耦的思想
docker可以在秒级别的层面去扩展收缩节点
容器内的应用可以直接打包成镜像上传到仓库内
打包应用程序简化部署——做镜像文件
可脱离底层硬件任意迁移
例:服务器从腾讯云迁移到阿里云
应用程序打包和发布
应用程序隔离
持续集成
部署微服务
快速搭建测试环境
提供paas(平台即服务)
社区版(community edition,CE)
企业版(Enterprise edition,EE) 增值服务
openstack的云主机搭建是基于IAAS基础设施即服务的层面搭建
docker(k8s)的容器containors是基于PAAS平台即服务的层面搭建
区别于VMware Workstation Pro、kvm、exsi、hyper-v 微软、Virtual box的硬件虚拟化,docker是基于进程的隔离
app 应用
bins/libs 进程
guest OS 安装虚拟机系统(guest OS)
docker engine/安装虚拟化引擎 hypervisor/监控平台
openrating system 宿主系统
infrastructure 硬件层面
备注:
docker 没有虚拟系统层面
hypervisor会侵占5%的cpu资源
docker engine 几乎忽略,只损耗关于网络通信的资源
docker的安全性低,因为是在宿主系统直接安装
虚拟机因为有一个虚拟机系统,所以安全性要高
备注:
磁盘占用,docker占用空间少,因为他是个进程级别
docker主要支持linux
镜像
容器
仓库
[root@nginx ~]# systtemctl stop firewalld
-bash: systtemctl: command not found
[root@nginx ~]# systemctl stop firewalld
[root@nginx ~]# systemctl disable firewalld
[root@nginx ~]# setenforce 0
setenforce: SELinux is disabled
[root@nginx ~]# sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
[root@nginx ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
[root@nginx ~]# cd /etc/yum.repos.d/
[root@nginx yum.repos.d]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror
adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
[root@nginx yum.repos.d]# ls
docker-ce.repo //新增
[root@nginx yum.repos.d]# yum install -y docker-ce
[root@nginx yum.repos.d]# systemctl start docker
[root@nginx yum.repos.d]# systemctl enable docker
打开https://help.aliyun.com/document_detail/60750.html
登录,点击容器镜像服务控制台
前往开通
设置密码
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://fk2yrsh1.mirror.aliyuncs.com"]
}
EOF
[root@nginx yum.repos.d]# cd /etc/docker/ You have new mail in /var/spool/mail/root [root@nginx docker]# ls key.json [root@nginx docker]# tee /etc/docker/daemon.json <<-'EOF' > { > "registry-mirrors": ["https://fk2yrsh1.mirror.aliyuncs.com"] > } > EOF { "registry-mirrors": ["https://fk2yrsh1.mirror.aliyuncs.com"] } [root@nginx docker]# ls daemon.json key.json [root@nginx docker]# systemctl daemon-reload [root@nginx docker]# systemctl restart docker
[root@nginx docker]# echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
[root@nginx docker]# sysctl -p
net.ipv4.ip_forward = 1
[root@nginx docker]# systemctl restart network
[root@nginx docker]# systemctl restart docker
[root@nginx docker]# docker version Client: Docker Engine - Community Version: 19.03.8 API version: 1.40 Go version: go1.12.17 Git commit: afacb8b Built: Wed Mar 11 01:27:04 2020 OS/Arch: linux/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 19.03.8 API version: 1.40 (minimum version 1.12) Go version: go1.12.17 Git commit: afacb8b Built: Wed Mar 11 01:25:42 2020 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.2.13 GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429 runc: Version: 1.0.0-rc10 GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd docker-init: Version: 0.18.0 GitCommit: fec3683
[root@nginx docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
-a 列出最近一次启动的容器
[root@nginx docker]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
此时下载镜像默认是从公有仓库,从公有仓库下载需要使用镜像加速
STARS越高,代表该镜像被使用率越高
jwilder/nginx-proxy 含义是作者/软件名
[root@nginx docker]# which docker /usr/bin/docker [root@nginx docker]# docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 12946 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1767 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 764 [OK] linuxserver/nginx An Nginx container, brought to you by LinuxS… 102 bitnami/nginx Bitnami nginx Docker Image 81 [OK] tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 67 [OK] jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 50 nginxdemos/hello NGINX webserver that serves a simple page co… 47 [OK] jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 37 [OK] nginx/unit NGINX Unit is a dynamic web and application … 36 nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 28 privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 23 [OK] schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 18 [OK] centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13 nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 13 centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 13 blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 13 [OK] raulr/nginx-wordpress Nginx front-end for the official wordpress:f… 12 [OK] nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 10 sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a… 7 [OK] mailu/nginx Mailu nginx frontend 6 [OK] bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 4 [OK] ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 1 [OK] wodby/nginx Generic nginx 0 [OK] centos/nginx-110-centos7 Platform for running nginx 1.10 or building … 0
AUFS (联合文件系统)若干层下载
c499e6d256d6: Pull complete
74cda408e262: Pull complete
ffadbd415ab7: Pull complete
下载信息存放在/var/lib/docker/image/overlay2/下
[root@nginx docker]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
c499e6d256d6: Pull complete
74cda408e262: Pull complete
ffadbd415ab7: Pull complete
Digest: sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@nginx docker]# cd /var/lib/docker/image/overlay2/
[root@nginx overlay2]# ls
distribution imagedb layerdb repositories.json
[root@nginx overlay2]# cat repositories.json
{"Repositories":{"nginx":{"nginx:latest":"sha256:ed21b7a8aee9cc677df6d7f38a641fa0e3c05f65592c592c9f28c42b3dd89291","nginx@sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596":"sha256:ed21b7a8aee9cc677df6d7f38a641fa0e3c05f65592c592c9f28c42b3dd89291"}}}
此时再次查看images
[root@nginx overlay2]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ed21b7a8aee9 8 days ago 127MB
latest代表标签
其中包含大小,端口号
[root@nginx overlay2]# docker inspect ed21b7a8aee9
[
{
"Id": "sha256:ed21b7a8aee9cc677df6d7f38a641fa0e3c05f65592c592c9f28c42b3dd89291",
"RepoTags": [
"nginx:latest"
[root@nginx overlay2]# docker tag nginx:latest nginx:web
[root@nginx overlay2]# docker images //可以使用grep
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ed21b7a8aee9 8 days ago 127MB
nginx web ed21b7a8aee9 8 days ago 127MB
相当于重新复制一个别名,ID号一样的
[root@nginx overlay2]# docker rmi nginx:latest
Untagged: nginx:latest
[root@nginx overlay2]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx web ed21b7a8aee9 8 days ago 127MB
[root@nginx opt]# docker save -o nginx nginx:web
[root@nginx opt]# ls -lh nginx
-rw------- 1 root root 125M Apr 8 20:39 nginx
docker rmi ed21b7a8aee9
[root@nginx opt]# docker rmi nginx:web
Untagged: nginx:web
Untagged: nginx@sha256:282530fcb7cd19f3848c7b611043f82ae4be3781cb00105a1d593d7e6286b596
Deleted: sha256:ed21b7a8aee9cc677df6d7f38a641fa0e3c05f65592c592c9f28c42b3dd89291
Deleted: sha256:8a305f371a6c3c445a1dfc500c1364743868a269ab8cdaf95902692e82168352
Deleted: sha256:d079ef06ec1f10a8050887365f9a940b39547ba6bcc46b16a463e740984f3223
Deleted: sha256:c3a984abe8a88059915bb6c7a1d249fd1ccc16d931334ac8816540b0eb686b45
[root@nginx opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@nginx opt]# docker load < nginx
[root@nginx opt]# docker load < nginx
c3a984abe8a8: Loading layer [==================================================>] 72.48MB/72.48MB
99134ec7f247: Loading layer [==================================================>] 58.11MB/58.11MB
d37eecb5b769: Loading layer [==================================================>] 3.584kB/3.584kB
Loaded image: nginx:web
[root@nginx opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx web ed21b7a8aee9 8 days ago 127MB
上传镜像到仓库中
登录本地仓库
docker tag nginx:web registry.cn-hangzhou.aliyuncs.com/nginx_gsy/docker_gsy:web
docker tag 仓库名:标签 私有仓库地址/命名空间/仓库名:镜像版本号
[root@nginx opt]# docker login --username=压马路一起 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@nginx opt]# docker tag nginx:web registry.cn-hangzhou.aliyuncs.com/nginx_gsy/docker_gsy:web //上传镜像 [root@nginx opt]# docker push registry.cn-hangzhou.aliyuncs.com/nginx_gsy/docker_gsy:web The push refers to repository [registry.cn-hangzhou.aliyuncs.com/nginx_gsy/docker_gsy] d37eecb5b769: Pushed 99134ec7f247: Pushed c3a984abe8a8: Pushed web: digest: sha256:7ac7819e1523911399b798309025935a9968b277d86d50e5255465d6592c0266 size: 948
[root@nginx opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx web ed21b7a8aee9 8 days ago 127MB
registry.cn-hangzhou.aliyuncs.com/nginx_gsy/docker_gsy web ed21b7a8aee9 8 days ago 127MB
查看当前容器
[root@nginx opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
此时容器为created状态
-i 让容器的标准型输入保持打开
-t 让容器开启一个伪终端
[root@nginx opt]# docker create -it nginx:web /bin/bash
0baf68712aedaf52156085b6fc0567c840533a6b0061dcc658b7f5df11c17d7d
[root@nginx opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0baf68712aed nginx:web "/bin/bash" 15 seconds ago Created
此时容器为up状态,端口开启
[root@nginx opt]# docker start 0baf68712aed
0baf68712aed
You have new mail in /var/spool/mail/root
[root@nginx opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0baf68712aed nginx:web "/bin/bash" About a minute ago Up 3 seconds 80/tcp
[root@nginx opt]# docker exec -it 0baf68712aed /bin/bash
root@0baf68712aed:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
-c commad的意思
先去找镜像,如果没有,就去仓库下载,之后执行一次-c的命令
[root@nginx ~]# docker run centos:7 /usr/bin/bash -c ls / Unable to find image 'centos:7' locally 7: Pulling from library/centos ab5ef0e58194: Pull complete Digest: sha256:4a701376d03f6b39b8c2a8f4a8e499441b0d567f9ab9d58e4991de4472fb813c Status: Downloaded newer image for centos:7 anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
此时也多了一个镜像
[root@nginx ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest ed21b7a8aee9 8 days ago 127MB
centos 7 5e35e350aded 4 months ago 203MB
查看容器,发现centos:7的状态是exited(0)指退出状态,(0)为正常退出
-c的ls命令执行完了,成功执行就正常退出
[root@nginx ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a75d7c0e9f65 centos:7 "/usr/bin/bash -c ls…" 2 minutes ago Exited (0) 2 minutes ago hungry_moser
[root@nginx ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest ed21b7a8aee9 8 days ago 127MB centos 7 5e35e350aded 4 months ago 203MB [root@nginx ~]# docker create -it nginx:latest /bin/bash d080f27d8e0fb1269480a3bf7977e2104e4f580f5bfd68488a348e667b31d70e [root@nginx ~]# docker start d080f27d8e0f [root@nginx ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d080f27d8e0f nginx:latest "/bin/bash" About a minute ago Up 3 seconds 80/tcp jovial_cerf a75d7c0e9f65 centos:7 "/usr/bin/bash -c ls…" 7 minutes ago Exited (0) 6 minutes ago hungry_moser [root@nginx ~]# docker stop d080f27d8e0f d080f27d8e0f [root@nginx ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d080f27d8e0f nginx:latest "/bin/bash" About a minute ago Exited (0) 1 second ago jovial_cerf a75d7c0e9f65 centos:7 "/usr/bin/bash -c ls…" 7 minutes ago Exited (0) 7 minutes ago
写个死循环语句,让其保持UP状态
-d 给一个守护进程
[root@nginx ~]# docker run -d centos:7 /bin/bash -c "while true;do echo hello;done"
f9197f3ebbf773559e7dc653796f83847ca43ceb99aa130d5aa8068065cbf082
[root@nginx ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f9197f3ebbf7 centos:7 "/bin/bash -c 'while…" 11 seconds ago Up 10 seconds
这种会异常退出
[root@nginx ~]# docker stop f9197f3ebbf7
f9197f3ebbf7
[root@nginx ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f9197f3ebbf7 centos:7 "/bin/bash -c 'while…" About a minute ago Exited (137) 2 seconds ago
首先目标容器是一个up状态
然后进入docker exec it
[root@nginx ~]# docker start d080f27d8e0f
d080f27d8e0f
[root@nginx ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f9197f3ebbf7 centos:7 "/bin/bash -c 'while…" 3 minutes ago Exited (137) 2 minutes ago hopeful_blackburn
d080f27d8e0f nginx:latest "/bin/bash" 7 minutes ago Up 1 second 80/tcp
[root@nginx ~]# docker exec -it d080f27d8e0f /bin/bash
root@d080f27d8e0f:/# ls
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@d080f27d8e0f:/# exit
exit
[root@nginx ~]#
[root@nginx ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f9197f3ebbf7 centos:7 "/bin/bash -c 'while…" 7 minutes ago Exited (137) 6 minutes ago hopeful_blackburn
d080f27d8e0f nginx:latest "/bin/bash" 11 minutes ago Up 4 minutes 80/tcp jovial_cerf
a75d7c0e9f65 centos:7 "/usr/bin/bash -c ls…" 17 minutes ago Exited (0) 17 minutes ago
[root@nginx ~]# docker export a75d7c0e9f65 > nginx_c 导出,此时这个节点使exited状态
[root@nginx ~]# ls -lh nginx_c
-rw-r--r-- 1 root root 202M Apr 8 22:22 nginx_c
(会生成镜像,而不会创建容器)
[root@nginx ~]# cat nginx_c | docker import - nginx:c
sha256:525c40797065e97c8c9238a486de8109292a8016cc081283ed32c8d32d6a8c27
[root@nginx ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx c 525c40797065 33 seconds ago 203MB
nginx latest ed21b7a8aee9 8 days ago 127MB
centos 7 5e35e350aded 4 months ago 203MB
[root@nginx ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f9197f3ebbf7 centos:7 "/bin/bash -c 'while…" 12 minutes ago Exited (137) 11 minutes ago hopeful_blackburn
d080f27d8e0f nginx:latest "/bin/bash" 16 minutes ago Up 9 minutes 80/tcp jovial_cerf
a75d7c0e9f65 centos:7 "/usr/bin/bash -c ls…" 22 minutes ago Exited (0) 22 minutes ago
批量删除容器
容器必须先停止,再删除
[root@nginx ~]# docker ps -a | awk '{print "docker rm "$1}' | bash
Error: No such container: CONTAINER
f9197f3ebbf7
Error response from daemon: You cannot remove a running container d080f27d8e0fb1269480a3bf7977e2104e4f580f5bfd68488a348e667b31d70e. Stop the container before attempting removal or force remove
a75d7c0e9f65
[root@nginx ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d080f27d8e0f nginx:latest "/bin/bash" 21 minutes ago Up 13 minutes 80/tcp jovial_cerf
[root@nginx ~]#
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。