一、使用Docker的限制
創(chuàng)新互聯(lián)公司主營西市網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,app軟件定制開發(fā),西市h5成都微信小程序搭建,西市網(wǎng)站營銷推廣歡迎西市等地區(qū)企業(yè)咨詢
1、要想使用linux容器,內(nèi)核版本要高于3.8以上,并且至少在linux內(nèi)核級要支持兩種技術,namespaces和CGroups。接著借助于在用戶空間組織一些工具,利用內(nèi)核級所提供的這些技術, 從而實現(xiàn)容器運行的目的,Docker在容器運行使用簡化的道路上又近了一步,Docker提供了鏡像,而且是分層構建聯(lián)合掛載的方式,使得容器技術的使用更加被簡化。后來在Docker的主導下,又有了OCI和OCF標準。
OCI(Open Container Initiative):由Linux基金會主導于2015年6月創(chuàng)立,OCI定義了容器運行時的標準,
OCI有兩部分組成:
the Runtime Specification:運行時標準
the image Specification:鏡像格式標準
OCF(Open Container Format):runC是Docker按照開放容器格式標準(OCF)制定的一種具體實現(xiàn),runC是從Docker的libcontainer中遷移而來的,實現(xiàn)了容器啟停,資源隔離等功能,Docker默認提供了docker-runc實現(xiàn)。
二、Docker的鏡像
默認Docker的鏡像是集中放置在Docker Hub上的,docker在創(chuàng)建容器時,Docker會先檢查本地是否有鏡像,如果沒有會到docker hub上去下載指定的鏡像到本地,并且下載下來的鏡像在容器使用結束后不會刪除,鏡像是不可修改的,只能重構。下面是docker的整體架構
三、Docker的安裝及使用
3.1、依賴及基礎環(huán)境:
1、64 bits CPU
2、Linux Kernel 3.10+
3、 Linux Kernel cgroups and namespaces
3.2、centos 7安裝方式
1、extras repository;默認centos7的yum倉庫extras就有docker,版本比較老,下載新版本的docker的yum倉庫
[root@MiWiFi-R3L-srv ~]# cd /etc/yum.repos.d/ [root@MiWiFi-R3L-srv yum.repos.d]# wget https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo [root@MiWiFi-R3L-srv yum.repos.d]# sed -i 's#https://download.docker.com#https://mirrors.tuna.tsinghua.edu.cn/docker-ce#g' docker-ce.repo [root@MiWiFi-R3L-srv yum.repos.d]# yum update [root@MiWiFi-R3L-srv yum.repos.d]# yum install docker-ce
2、docker-ce的配置文件;需要手動創(chuàng)建
[root@MiWiFi-R3L-srv yum.repos.d]# mkdir /etc/docker/ [root@MiWiFi-R3L-srv docker]# echo -e '{\n\t"registry-mirrors": ["https://registry.docker-cn.com"]\n}' >/etc/docker/daemon.json [root@MiWiFi-R3L-srv docker]# systemctl start docker.service [root@MiWiFi-R3L-srv docker]# docker info Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 18.06.1-ce Storage Driver: overlay2 # docker要實現(xiàn)分層構建,聯(lián)合掛載。必須使用這種文件系統(tǒng)來支持。在centos7.4以前用的是device mapper,這種是lvm的實現(xiàn),在docker上性能極差,還不穩(wěn)定,已經(jīng)廢棄。 ...... ...... ......
在能看到上面的一系列信息,說明docker已經(jīng)安裝成功并可以使用了
3、docker 倉庫格式
[root@MiWiFi-R3L-srv docker]# docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 10034 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con?? 1440 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of?? 633 [OK] jrcs/letsencrypt-nginx-proxy-companion LetsEncrypt container to use with nginx as p?? 428 [OK] kong Open-source Microservice & API Management la?? 237 [OK] webdevops/php-nginx Nginx with PHP-FPM 117 [OK] kitematic/hello-world-nginx A light-weight nginx container that demonstr?? 112 zabbix/zabbix-web-nginx-MySQL Zabbix frontend based on Nginx web-server wi?? 74 [OK] bitnami/nginx Bitnami nginx Docker Image 58 [OK] 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 48 [OK] linuxserver/nginx An Nginx container, brought to you by LinuxS?? 42 tobi312/rpi-nginx NGINX on Raspberry Pi / armhf 23 [OK] blacklabelops/nginx Dockerized Nginx Reverse Proxy Server. 12 [OK] wodby/drupal-nginx Nginx for Drupal container image 11 [OK] centos/nginx-18-centos7 Platform for running nginx 1.8 or building n?? 8 webdevops/nginx Nginx container 8 [OK] nginxdemos/hello NGINX webserver that serves a simple page co?? 8 [OK] centos/nginx-112-centos7 Platform for running nginx 1.12 or building ?? 5 1science/nginx Nginx Docker images that include Consul Temp?? 4 [OK] travix/nginx NGinx reverse proxy 2 [OK] mailu/nginx Mailu nginx frontend 2 [OK] pebbletech/nginx-proxy nginx-proxy sets up a container running ngin?? 2 [OK] toccoag/openshift-nginx Nginx reverse proxy for Nice running on same?? 1 [OK] ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 0 [OK] wodby/nginx Generic nginx 0 [OK]
上面列表中,沒有/分隔開的倉庫是頂級倉庫,一般是docker hub官方的。帶/分隔開的是個人用戶創(chuàng)建的鏡像
4、啟動一個docker鏡像
docker run命令啟動容器時會先在本地倉庫查找鏡像,如果沒有在去docker hub倉庫中下載鏡像并啟動
[root@MiWiFi-R3L-srv docker]# docker container run --name nginx1 -d nginx:stable Unable to find image 'nginx:stable' locally # 說明本地倉庫中沒有nginx:stable鏡像 stable: Pulling from library/nginx f17d81b4b692: Pull complete 3df1ab0a1750: Pull complete 576b56a453df: Pull complete Digest: sha256:8b600a4d029481cc5b459f1380b30ff6cb98e27544fc02370de836e397e34030 Status: Downloaded newer image for nginx:stable # 從docker hub倉庫中下載nginx:stable鏡像成功 d5eb28ecbc5cbe86fb430c4098361b7199c9147e79c06096868746dc638ebec1 [root@MiWiFi-R3L-srv docker]# docker container ls # 查看正在運行的容器,發(fā)現(xiàn)nginx:stable已經(jīng)正常運行 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d5eb28ecbc5c nginx:stable "nginx -g 'daemon of?? 4 minutes ago Up 4 minutes 80/tcp nginx1
查看正在運行的容器詳細信息
[root@MiWiFi-R3L-srv docker]# docker container inspect nginx1 [ { "Id": "d5eb28ecbc5cbe86fb430c4098361b7199c9147e79c06096868746dc638ebec1", "Created": "2018-10-31T13:45:39.761183953Z", "Path": "nginx", "Args": [ "-g", "daemon off;" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 30605, "ExitCode": 0, "Error": "", "StartedAt": "2018-10-31T13:45:40.064902138Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:ecc98fc2f376d6560311b66d6958e4350a5a485ee07aa2d1235842d0bce440da", "ResolvConfPath": "/var/lib/docker/containers/d5eb28ecbc5cbe86fb430c4098361b7199c9147e79c06096868746dc638ebec1/resolv.conf", "HostnamePath": "/var/lib/docker/containers/d5eb28ecbc5cbe86fb430c4098361b7199c9147e79c06096868746dc638ebec1/hostname", "HostsPath": "/var/lib/docker/containers/d5eb28ecbc5cbe86fb430c4098361b7199c9147e79c06096868746dc638ebec1/hosts", "LogPath": "/var/lib/docker/containers/d5eb28ecbc5cbe86fb430c4098361b7199c9147e79c06096868746dc638ebec1/d5eb28ecbc5cbe86fb430c4098361b7199c9147e79c06096868746dc638ebec1-json.log", "Name": "/nginx1", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "DNS": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "shareable", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DiskQuota": 0, "KernelMemory": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": 0, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "MaskedPaths": [ "/proc/acpi", "/proc/kcore", "/proc/keys", "/proc/latency_stats", "/proc/timer_list", "/proc/timer_stats", "/proc/sched_debug", "/proc/scsi", "/sys/firmware" ], "ReadonlyPaths": [ "/proc/asound", "/proc/bus", "/proc/fs", "/proc/irq", "/proc/sys", "/proc/sysrq-trigger" ] }, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/99e98bcb3268cca54fb9964a69452a424e9de607df50ef6ce1631da4be9f203b-init/diff:/var/lib/docker/overlay2/30039edd43cad0ec36a0fb2e546da0b33bb38336d2fa548e2cfca11a382b1ecb/diff:/var/lib/docker/overlay2/3e588e0ac8a29338bf759041cee08eff0bc955e47f7f32b0fe65c6536cb83a2b/diff:/var/lib/docker/overlay2/c80f5826b0afb09f3cd5ff12fa5a16e67aa2aaed0ae7ea0b36aeffc218559abd/diff", "MergedDir": "/var/lib/docker/overlay2/99e98bcb3268cca54fb9964a69452a424e9de607df50ef6ce1631da4be9f203b/merged", "UpperDir": "/var/lib/docker/overlay2/99e98bcb3268cca54fb9964a69452a424e9de607df50ef6ce1631da4be9f203b/diff", "WorkDir": "/var/lib/docker/overlay2/99e98bcb3268cca54fb9964a69452a424e9de607df50ef6ce1631da4be9f203b/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "d5eb28ecbc5c", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "80/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "NGINX_VERSION=1.14.0-1~stretch", "NJS_VERSION=1.14.0.0.2.0-1~stretch" ], "Cmd": [ "nginx", "-g", "daemon off;" ], "ArgsEscaped": true, "Image": "nginx:stable", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "maintainer": "NGINX Docker Maintainers" }, "StopSignal": "SIGTERM" }, "NetworkSettings": { "Bridge": "", "SandboxID": "045cba9e5a6f39424dfb35c57d0ca43c9e335004d76c3dda36552aac740e014f", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "80/tcp": null }, "SandboxKey": "/var/run/docker/netns/045cba9e5a6f", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "3c28fb379c1c2018d05ae03ff163aae9a0c5c12621282e0d98f803daadaf97a6", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "0a0cc62c3f1fba5667917f833b002ad0f5c1342acb61ff67317e17544e7a7ea1", "EndpointID": "3c28fb379c1c2018d05ae03ff163aae9a0c5c12621282e0d98f803daadaf97a6", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02", "DriverOpts": null } } } } ]
IPAddress是當前容器監(jiān)聽的網(wǎng)絡地址,下面嘗試訪問nginx
[root@MiWiFi-R3L-srv docker]# curl 172.17.0.2Welcome to nginx! Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.Thank you for using nginx.
5、停止一個容器
[root@MiWiFi-R3L-srv docker]# docker container stop nginx1 # 停止一個運行中的容器,kill相當于 kill -9 ,stop相當于kill -15 nginx1 [root@MiWiFi-R3L-srv docker]# docker container ls # ls 只能看到正在運行中的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@MiWiFi-R3L-srv docker]# docker container ls -a # ls -a 可以看到停止狀態(tài)的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d5eb28ecbc5c nginx:stable "nginx -g 'daemon of?? 11 minutes ago Exited (0) 10 seconds ago nginx1
6、啟動一個停止的容器
[root@MiWiFi-R3L-srv docker]# docker container start nginx1 nginx1 [root@MiWiFi-R3L-srv docker]# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d5eb28ecbc5c nginx:stable "nginx -g 'daemon of?? 14 minutes ago Up 3 seconds 80/tcp nginx1
7、暫停/取消暫停一個運行中的容器
[root@MiWiFi-R3L-srv docker]# docker container pause nginx1 # 暫停一個nginx容器 nginx1 [root@MiWiFi-R3L-srv docker]# docker container ls # 可以看到nginx的容器現(xiàn)在處于Paused狀態(tài) CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d5eb28ecbc5c nginx:stable "nginx -g 'daemon of?? 15 minutes ago Up About a minute (Paused) 80/tcp nginx1 [root@MiWiFi-R3L-srv docker]# [root@MiWiFi-R3L-srv docker]# docker container unpause nginx1 # 取消一個容器的暫停狀態(tài) nginx1 [root@MiWiFi-R3L-srv docker]# docker container ls # nginx容器的狀態(tài)又恢復運行 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d5eb28ecbc5c nginx:stable "nginx -g 'daemon of?? 16 minutes ago Up About a minute 80/tcp nginx1
8、在容器中執(zhí)行交互命令
[root@MiWiFi-R3L-srv docker]# docker container exec -it redis1 /bin/sh
/data # ps
PID USER TIME COMMAND
1 redis 0:00 redis-server
25 root 0:00 /bin/sh
29 root 0:00 ps
9、在終端查看容器中進程的日志
[root@MiWiFi-R3L-srv docker]# docker container logs nginx1 172.17.0.1 - - [31/Oct/2018:13:54:40 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" 172.17.0.1 - - [31/Oct/2018:14:02:19 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-" 172.17.0.1 - - [31/Oct/2018:14:14:17 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
四、docker容器的狀態(tài)轉換和各常用命令