爲何要使用 Docker?php
做爲一種新興的虛擬化方式,Docker 跟傳統的虛擬化方式相比具備衆多的優點。
首先,Docker 容器的啓動能夠在秒級實現,這相比傳統的虛擬機方式要快得多。 其次,Docker 對系統資 源的利用率很高,一臺主機上能夠同時運行數千個 Docker 容器。
容器除了運行其中應用外,基本不消耗額外的系統資源,使得應用的性能很高,同時系統的開銷儘可能小。 傳統虛擬機方式運行 10 個不一樣的應用就要起 10 個虛擬機,而Docker 只須要啓動 10 個隔離的應用便可。
具體說來,Docker 在以下幾個方面具備較大的優點。html
更快速的交付和部署mysql
對開發和運維(devop)人員來講,最但願的就是一次建立或配置,能夠在任意地方正常運行。
開發者可使用一個標準的鏡像來構建一套開發容器,開發完成以後,運維人員能夠直接使用這個容器來 部署代碼。 Docker 能夠快速建立容器,快速迭代應用程序,並讓整個過程全程可見,使團隊中的其餘成員 更容易理解應用程序是如何建立和工做的。 Docker 容器很輕很快!容器的啓動時間是秒級的,大量地節約 開發、測試、部署的時間。linux
更輕鬆的遷移和擴展nginx
Docker 容器幾乎能夠在任意的平臺上運行,包括物理機、虛擬機、公有云、私有云、我的電腦、服務器 等。 這種兼容性可讓用戶把一個應用程序從一個平臺直接遷移到另一個。git
更簡單的管理web
使用 Docker,只須要小小的修改,就能夠替代以往大量的更新工做。全部的修改都以增量的方式被分發和 更新,從而實現自動化而且高效的管理。sql
對比傳統虛擬機總結docker
特性 |
容量 | 虛擬機 |
啓動 | 秒級 | 分鐘級 |
硬盤使用 | 通常爲 MB | 通常爲 GB |
性能 | 接近原生 | 弱於 |
系統支持量 | 單機支持上千個容器 | 通常幾十個 |
Docker 包括三個基本概念數據庫
鏡像(Image) 容器(Container) 倉庫(Repository)
理解了這三個概念,就理解了 Docker 的整個生命週期。
Docker 鏡像
Docker 鏡像就是一個只讀的模板。
例如:一個鏡像能夠包含一個完整的 ubuntu 操做系統環境,裏面僅安裝了 Apache 或用戶須要的其它應用 程序。
鏡像能夠用來建立 Docker 容器。
Docker 提供了一個很簡單的機制來建立鏡像或者更新現有的鏡像,用戶甚至能夠直接從其餘人那裏下載一 個已經作好的鏡像來直接使用。
Docker 容器
Docker 利用容器來運行應用。
容器是從鏡像建立的運行實例。它能夠被啓動、開始、中止、刪除。每一個容器都是相互隔離的、保證安全 的平臺。
能夠把容器看作是一個簡易版的 Linux 環境(包括root用戶權限、進程空間、用戶空間和網絡空間等)和運 行在其中的應用程序。
*注:鏡像是隻讀的,容器在啓動的時候建立一層可寫層做爲最上層。
Docker 倉庫
倉庫是集中存放鏡像文件的場所。有時候會把倉庫和倉庫註冊服務器(Registry)混爲一談,並不嚴格區 分。實際上,倉庫註冊服務器上每每存放着多個倉庫,每一個倉庫中又包含了多個鏡像,每一個鏡像有不一樣的 標籤(tag)。
倉庫分爲公開倉庫(Public)和私有倉庫(Private)兩種形式。
最大的公開倉庫是 Docker Hub,存放了數量龐大的鏡像供用戶下載。 國內的公開倉庫包括 Docker Pool 等,能夠提供大陸用戶更穩定快速的訪問。
固然,用戶也能夠在本地網絡內建立一個私有倉庫。
當用戶建立了本身的鏡像以後就可使用 push 命令將它上傳到公有或者私有倉庫,這樣下次在另一臺 機器上使用這個鏡像時候,只須要從倉庫上 pull 下來就能夠了。
*注:Docker 倉庫的概念跟 Git 相似,註冊服務器能夠理解爲 GitHub 這樣的託管服務。
CentOS 系列安裝 Docker
最小化安裝Centos7.5
[root@Docker ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) [root@Docker ~]# uname -r 3.10.0-862.3.3.el7.x86_64
關閉防火牆和selinux
[root@Docker ~]# systemctl stop firewalld [root@Docker ~]# systemctl disable firewalld [root@Docker ~]# setenforce 0 setenforce: SELinux is disabled
[root@Docker ~]# yum -y install docker [root@Docker ~]# systemctl start docker #啓動docker進程 [root@Docker ~]# systemctl enable docker #加入docker開機啓動 Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service. [root@Docker ~]# docker ps #查看封裝在docker鏡像中的正在運行的鏡像進程 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@Docker ~]# docker version Client: Version: 1.13.1 API version: 1.26 Package version: docker-1.13.1-63.git94f4240.el7.centos.x86_64 Go version: go1.9.4 Git commit: 94f4240/1.13.1 Built: Fri May 18 15:44:33 2018 OS/Arch: linux/amd64 Server: Version: 1.13.1 API version: 1.26 (minimum version 1.12) Package version: docker-1.13.1-63.git94f4240.el7.centos.x86_64 Go version: go1.9.4 Git commit: 94f4240/1.13.1 Built: Fri May 18 15:44:33 2018 OS/Arch: linux/amd64 Experimental: false
首先清理掉yum安裝的docker標準版
這裏有兩種方式
#第一種清理docker方法 [root@Docker ~]# yum remove docker-latest-logrotate docker-logrotate docker-selinux dockdocker-engine
#第二種清理docker方法
[root@Docker ~]# yum history list #查看yum安裝的歷史列表
已加載插件:fastestmirror
ID | 登陸用戶 | 日期和時間 | 操做 | 變動數
-------------------------------------------------------------------------------
31 | 系統 <空> | 2018-07-16 17:16 | Install | 9 E< #此次就是安裝
30 | root <root> | 2018-07-09 11:04 | Install | 9 >
29 | root <root> | 2018-07-08 10:38 | Install | 1
28 | root <root> | 2018-07-08 10:33 | Install | 3
27 | root <root> | 2018-06-26 20:27 | Install | 1
26 | root <root> | 2018-06-26 18:20 | Install | 5
25 | root <root> | 2018-06-26 17:13 | Install | 1
24 | root <root> | 2018-06-26 17:06 | Install | 1
23 | root <root> | 2018-06-21 14:50 | Install | 3
22 | root <root> | 2018-06-18 23:11 | Install | 50
21 | root <root> | 2018-06-18 23:04 | Install | 2
20 | root <root> | 2018-06-17 02:59 | I, U | 50
19 | root <root> | 2018-06-16 20:33 | I, U | 30
18 | root <root> | 2018-06-16 20:23 | Install | 1
17 | root <root> | 2018-06-16 20:20 | Install | 2
16 | root <root> | 2018-06-16 13:14 | Install | 1
history list
[root@Docker ~]# yum history info 31 #查看yum歷史安裝ID爲31的安裝信息
已加載插件:fastestmirror
事務 ID: 31
起始時間 : Mon Jul 16 17:16:46 2018
啓動 RPM 數據庫 : 971:bb0b16847e8bd5bbed6649c9045ceb3ea74c0b8d
結束時間 : 17:17:11 2018 (25 秒)
結束 RPM 數據庫 : 980:1158b800059814346da9aca5bc32093cb8af18b5
用戶 : 系統 <空>
返回碼 : 成功
命令行 : install docker-io
事務完成屬主:
已安裝 rpm-4.11.3-32.el7.x86_64 @anaconda
已安裝 yum-3.4.3-158.el7.centos.noarch @anaconda
已安裝 yum-metadata-parser-1.1.4-10.el7.x86_64 @anaconda
已安裝 yum-plugin-fastestmirror-1.1.31-45.el7.noarch @anaconda
已變動的包:
依賴安裝 container-selinux-2:2.55-1.el7.noarch @extras
依賴安裝 container-storage-setup-0.9.0-1.rhel75.gite0997c3.el7.noarch @extras
安裝 docker-2:1.13.1-63.git94f4240.el7.centos.x86_64 @extras
依賴安裝 docker-client-2:1.13.1-63.git94f4240.el7.centos.x86_64 @extras
依賴安裝 docker-common-2:1.13.1-63.git94f4240.el7.centos.x86_64 @extras
依賴安裝 oci-register-machine-1:0-6.git2b44233.el7.x86_64 @extras
依賴安裝 oci-systemd-hook-1:0.1.15-2.gitc04483d.el7.x86_64 @extras
依賴安裝 oci-umount-2:2.3.3-3.gite3c9055.el7.x86_64 @extras
依賴安裝 skopeo-containers-1:0.1.29-3.dev.git7add6fc.el7.0.x86_64 @extras
Scriptlet 輸出:
1 setsebool: SELinux is disabled.
history info
[root@Docker ~]# yum -y history undo 31 #進行yum安裝操做回退
#安裝依賴包 [root@Docker ~]# yum -y install yum-utils device-mapper-persistent-data lvm2 #添加docker的CE版本的yum源配置文件 [root@Docker ~]# curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 2424 100 2424 0 0 2544 0 --:--:-- --:--:-- --:--:-- 2543 [root@Docker ~]# ll /etc/yum.repos.d/docker-ce.repo -rw-r--r-- 1 root root 2424 7月 16 17:36 /etc/yum.repos.d/docker-ce.repo #安裝CE版本的docker [root@Docker ~]# yum -y install docker-ce [root@Docker ~]# systemctl start docker #啓動docker [root@Docker ~]# systemctl enable docker #添加開機啓動 [root@Docker ~]# docker version #查看docker版本 Client: Version: 18.03.1-ce API version: 1.37 Go version: go1.9.5 Git commit: 9ee9f40 Built: Thu Apr 26 07:20:16 2018 OS/Arch: linux/amd64 Experimental: false Orchestrator: swarm Server: Engine: Version: 18.03.1-ce API version: 1.37 (minimum version 1.12) Go version: go1.9.5 Git commit: 9ee9f40 Built: Thu Apr 26 07:23:58 2018 OS/Arch: linux/amd64 Experimental: false
# 國內部分用戶可能沒法鏈接docker官網提供的源,這裏提供阿里雲的鏡像節點供測試使用 [root@Docker ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo [root@Docker ~]# rpm --import http://mirrors.aliyun.com/docker-ce/linux/centos/gpg [root@Docker ~]# yum makecache fast [root@Docker ~]# yum -y install docker-ce
容器Hub服務 https://cr.console.aliyun.com的控制檯 提高pull的速度
能夠用支付寶登陸
cat /etc/docker/daemon.json { "registry-mirrors":[ "https://registry.docker-cn.com" ] } [root@docker ~]# systemctl daemon-reload [root@docker ~]# systemctl restart docker
用於從docker的官方公有鏡像倉庫查找鏡像
1.查看Docker Hub上公開的centos鏡像
[root@docker ~]# docker search centos NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 4419 [OK] ...如下省略無數行.... Name:鏡像的名字 DESCRIPTION:描述 STARS:星級(越高越好) OFFICIAL:是不是官方發佈的 AUTOMATED:是否自動化的
2.查找星級多於100的centos鏡像
[root@docker ~]# docker search centos -f stars=100 NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 4419 [OK] ansible/centos7-ansible Ansible on Centos7 114 [OK] /:符號用於分割做者名稱和鏡像名稱 ansible/centos7-ansible:ansible是做者名稱,centos7-ansible是鏡像名稱
3.多條件查找--filter
#查找官方發佈的,星級大於100的centos鏡像 [root@docker ~]# docker search centos --filter is-official=true --filter stars=100 NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 4419 [OK]
用於從Docker Hub上下載公有鏡像
#查找符合條件的hello-world鏡像 [root@docker ~]# docker search hello-world --filter is-official=true --filter stars=100 NAME DESCRIPTION STARS OFFICIAL AUTOMATED hello-world Hello World! (an example of minimal Dockeriz… 572 [OK] #下載目標hello-world鏡像 [root@docker ~]# docker pull hello-world Using default tag: latest latest: Pulling from library/hello-world 9bb5a5d4561a: Pull complete Digest: sha256:3e1764d0f546ceac4565547df2ac4907fe46f007ea229fd7ef2718514bcec35d Status: Downloaded newer image for hello-world:latest #下載成功latest是標記tag #下載目標centos:7鏡像 [root@docker 7]# docker pull centos:7 #網速很差的話,須要點時間耐心等待
用於本地鏡像的查看
[root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest 2cb0d9787c4d 5 days ago 1.85kB centos 7 49f7960eb7e4 5 weeks ago 200MB
REPOSITORY:鏡像倉庫(下邊羅列的都是本地已有鏡像名稱)
TAG:鏡像的標記(爲了區分同名鏡像)
IMAGES ID:鏡像的ID號
CREATED:此鏡像的建立時間
SIZE:此鏡像的大小
用於本地自定義鏡像的構建,須要建立Dockerfile文件
#建立Dockerfile文件的存儲目錄 [root@docker ~]# mkdir -p /root/dockerfile/lib/centos/7 [root@docker ~]# cd /root/dockerfile/lib/centos/7 #建立docker.sh腳本 [root@docker 7]# cat docker.sh #!/bin/bash while true do echo "welcome" sleep 5 done #建立Dockerfile配置文件,文件名稱必須爲Dockerfile,第一個字母必須大寫 [root@docker 7]# vim Dockerfile [root@docker 7]# cat Dockerfile FROM centos #從centos源鏡像的基礎上進行構建 LABEL MAINTATNER="DaMoWang.com" #做者的名稱 RUN ln -sfv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #RUN:在鏡像構建過程當中運行命令 ADD docker.sh /home/test/ #從本地系統中把docker.sh文件添加到構建中的鏡像的/home/test/目錄下 RUN chmod +x /home/test/docker.sh #在鏡像構建過程當中運行命令 CMD ["/home/test/docker.sh"] #構建鏡像完成時,最後執行的命令 #根據Dockfile配置文件構建一個自定義鏡像 docker build -t damowang/centos7:1 . #-t 指定鏡像名稱 :1 設定鏡像的tag標記 Sending build context to Docker daemon 3.072kB Step 1/6 : FROM centos ---> 49f7960eb7e4 Step 2/6 : LABEL MAINTATNER="damowang" ---> Using cache ---> 79ac7152beab Step 3/6 : RUN ln -sfv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ---> Using cache ---> eb1f30c3f53f Step 4/6 : ADD docker.sh /home/test/ ---> Using cache ---> ffbac1722ac7 Step 5/6 : RUN chmod +x /home/test/docker.sh ---> Using cache ---> 002a6fb94e60 Step 6/6 : CMD ["/home/test/docker.sh"] ---> Using cache ---> 6e709ee70391 Successfully built 6e709ee70391 #構建成功 Successfully tagged damowang/centos7:1 [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos7 1 6e709ee70391 4 minutes ago 200MB hello-world latest 2cb0d9787c4d 5 days ago 1.85kB centos 7 49f7960eb7e4 5 weeks ago 200MB
運行一個本地鏡像
[root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos7 1 6e709ee70391 4 minutes ago 200MB hello-world latest 2cb0d9787c4d 5 days ago 1.85kB centos 7 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker run -d -it 6e709ee70391 /bin/bash 7f76e680570f6e3047acdff116cf5253a840bb8719a492e1f6c20e34bd343f11 #docker run:運行一個指定的images id -d:放在後臺運行 -i:能夠進行命令交互 -t:製做一個僞終端用於登錄 6e709ee70391:鏡像的ID
查看已經運行的鏡像的進程
[root@docker ~]# docker ps -a #查看全部運行的鏡像進程(包含退出的exit) CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7f76e680570f 6e709ee70391 "/bin/bash" 2 minutes ago Up 2 minutes hungry_beaver #STATUS:進程的狀態,UP表示正在運行中,EXIT表示已經退出了。
從本地系統中切入到某個STATUS狀態是UP的鏡像進程裏
root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7f76e680570f 6e709ee70391 "/bin/bash" 7 minutes ago Up 7 minutes hungry_beaver [root@Docker 7]# docker attach 7f76e680570f #切入到容器號爲093e4fb8b09c的鏡像進程裏 [root@7f76e680570f /]# ls #已經進入容器裏了 bin etc lib media opt root sbin sys usr dev home lib64 mnt proc run srv tmp var [root@7f76e680570f /]# exit #退出容器 exit [root@Docker 7]# docker ps -a #容器的進程的STATUS已經處於EXIT狀態(以前是後臺運行的,切入進去後執行exit就等於手動退出了) CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7f76e680570f 6e709ee70391 "/bin/bash" 8 minutes ago Exited (0) 13 seconds ago hungry_beaver
用於中止一個正在運行着的容器進程
[root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7f76e680570f 6e709ee70391 "/bin/bash" 11 minutes ago Exited (0) 2 minutes ago hungry_beaver #再次在後臺啓動一個鏡像 [root@Docker 7]# docker run -d -it 6e709ee70391 /bin/bash a8dd8894050cca65230e70b1d1be40b620fc2bd3b67a642d68efdf7f2a38d42c [root@Docker 7]# docker ps -a #增長了一個容器進程 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8dd8894050c 6e709ee70391 "/bin/bash" 59 seconds ago Up 58 seconds priceless_shockley 7f76e680570f 6e709ee70391 "/bin/bash" 12 minutes ago Exited (0) 4 minutes ago hungry_beaver #中止一個運行着的容器進程 [root@Docker 7]# docker stop a8dd8894050c a8dd8894050c [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8dd8894050c 6e709ee70391 "/bin/bash" 2 minutes ago Exited (137) 36 seconds ago priceless_shockley 7f76e680570f 6e709ee70391 "/bin/bash" 14 minutes ago Exited (0) 6 minutes ago hungry_beaver
用於啓動一個已經中止了的容器進程
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8dd8894050c 6e709ee70391 "/bin/bash" 3 minutes ago Exited (137) About a minute ago priceless_shockley 7f76e680570f 6e709ee70391 "/bin/bash" 15 minutes ago Exited (0) 7 minutes ago hungry_beaver [root@Docker 7]# docker start a8dd8894050c a8dd8894050c [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8dd8894050c 6e709ee70391 "/bin/bash" 5 minutes ago Up 14 seconds priceless_shockley 7f76e680570f 6e709ee70391 "/bin/bash" 17 minutes ago Exited (0) 8 minutes ago hungry_beaver
用於刪除一個已經中止了的容器進程
[root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8dd8894050c 6e709ee70391 "/bin/bash" 5 minutes ago Up 14 seconds priceless_shockley 7f76e680570f 6e709ee70391 "/bin/bash" 17 minutes ago Exited (0) 8 minutes ago hungry_beaver [root@Docker 7]# docker rm 7f76e680570f #注意運行中的容器進程須要先stop,才能刪除 7f76e680570f [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8dd8894050c 6e709ee70391 "/bin/bash" 7 minutes ago Up 3 minutes priceless_shockley
用於刪除一個未用做容器啓動的本地鏡像
[root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos7 1 6e709ee70391 31 minutes ago 200MB hello-world latest 2cb0d9787c4d 5 days ago 1.85kB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker rmi -f 2cb0d9787c4d #-f 強制刪除(即使被佔用) Untagged: hello-world:latest Untagged: hello-world@sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a85c90c5e3c7afacdc Deleted: sha256:2cb0d9787c4dd17ef9eb03e512923bc4db10add190d3f84af63b744e353a9b34 Deleted: sha256:ee83fc5847cb872324b8a1f5dbfd754255367f4280122b4e2d5aee17818e31f5 [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos7 1 6e709ee70391 33 minutes ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker rmi 6e709ee70391 #但要注意,被用做容器啓動的鏡像是不能刪除的(需先rm刪除容器進程) Error response from daemon: conflict: unable to delete 6e709ee70391 (cannot be forced) - image is being used by running container a8dd8894050c docker rmi -f 6e709ee70391 Error response from daemon: conflict: unable to delete 6e709ee70391 (cannot be forced) - image is being used by running container a8dd8894050c [root@docker ~]# docker ps -a #查看容器進程,被佔用中 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8dd8894050c 6e709ee70391 "/bin/bash" 17 minutes ago Up 12 minutes priceless_shockley
將一個更改過的容器進程的容器狀態保存爲一個新的鏡像
[root@Docker 7]# docker ps -a #查看啓動的容器進程 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8dd8894050c 6e709ee70391 "/bin/bash" 18 minutes ago Up 13 minutes priceless_shockley [root@Docker 7]# docker attach a8dd8894050c #切入容器進程 [root@a8dd8894050c /]# ls bin etc lib media opt root sbin sys usr dev home lib64 mnt proc run srv tmp var [root@a8dd8894050c /]# mkdir damowang #在容器進程裏建立damowang目錄 [root@a8dd8894050c /]# ls bin dev home lib64 mnt proc run srv tmp var damowang etc lib media opt root sbin sys usr [root@a8dd8894050c /]# exit #退出容器進程 exit [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8dd8894050c 6e709ee70391 "/bin/bash" 20 minutes ago Exited (0) 8 seconds ago priceless_shockley [root@Docker 7]# docker commit a8dd8894050c damowang/centos:2 #將更改後的容器進程保存爲一個新的鏡像 sha256:0bfe626371890c061cbce5ac5b694b7223d512cb63a9386468ae6613f520c94f [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos 2 0bfe62637189 27 seconds ago 200MB #保存成功 damowang/centos7 1 6e709ee70391 41 minutes ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB #啓動新保存的鏡像 [root@Docker 7]# docker run -d -it 0bfe62637189 /bin/bash 711c165d7e4a030ce464ca43aee87afd9eab8e0359ae4cb591617c797b87da69 [root@Docker 7]# docker ps -a #查看新鏡像的容器進程 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 711c165d7e4a 0bfe62637189 "/bin/bash" 8 seconds ago Up 7 seconds heuristic_tereshkova a8dd8894050c 6e709ee70391 "/bin/bash" 22 minutes ago Exited (0) 2 minutes ago priceless_shockley [root@Docker 7]# docker attach 711c165d7e4a #切入新鏡像的容器進程 [root@711c165d7e4a /]# ls -d damowang #咱們發現以前建立的目錄仍舊存在 damowang
用於從本地操做系統上直接向容器進程發佈執行命令並返回結果
docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 711c165d7e4a 0bfe62637189 "/bin/bash" 5 minutes ago Up 3 seconds heuristic_tereshkova a8dd8894050c 6e709ee70391 "/bin/bash" 28 minutes ago Exited (0) 7 minutes ago priceless_shockley [root@Docker 7]# docker exec 711c165d7e4a ls /tmp/ #查看容器進程裏的/tmp目錄下全部內容 ks-script-3QMvMi yum.log [root@Docker 7]# docker exec 711c165d7e4a ls -d /damowang #查看容器進程裏/yunjisuan目錄 /damowang
用於在容器進程和本地系統之間複製文件
[root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 711c165d7e4a 0bfe62637189 "/bin/bash" 7 minutes ago Up About a minute heuristic_tereshkova a8dd8894050c 6e709ee70391 "/bin/bash" 29 minutes ago Exited (0) 9 minutes ago priceless_shockley [root@Docker 7]# docker exec 711c165d7e4a ls /tmp/ #向容器進程發佈命令 ks-script-3QMvMi yum.log [root@Docker 7]# docker cp 711c165d7e4a:/tmp/yum.log . [root@Docker 7]# ls Dockerfile docker.sh yum.log #已經複製過來了 [root@Docker 7]# docker cp docker.sh 711c165d7e4a:/tmp #將本地文件複製到容器進程裏 [root@Docker 7]# docker exec 711c165d7e4a ls /tmp/ docker.sh #複製成功 ks-script-3QMvMi yum.log
用於建立一個容器進程,可是並不啓動它
[root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos 2 0bfe62637189 15 minutes ago 200MB damowang/centos7 1 6e709ee70391 About an hour ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker create -it 0bfe62637189 #建立一個鏡像的容器進程,但不直接啓動 84d6659c1dd832f1a94e66e03d8f1c018afa24e7d558a2c5a61e1a624194e62b [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 84d6659c1dd8 0bfe62637189 "/bin/bash" 12 seconds ago Created gallant_hoover #建立狀態並未啓動 711c165d7e4a 0bfe62637189 "/bin/bash" 16 minutes ago Up 10 minutes heuristic_tereshkova a8dd8894050c 6e709ee70391 "/bin/bash" 39 minutes ago Exited (0) 18 minutes ago priceless_shockley [root@Docker 7]# docker start 84d6659c1dd8 84d6659c1dd8 [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 84d6659c1dd8 0bfe62637189 "/bin/bash" 2 minutes ago Up 34 seconds gallant_hoover 711c165d7e4a 0bfe62637189 "/bin/bash" 18 minutes ago Up 12 minutes heuristic_tereshkova a8dd8894050c 6e709ee70391 "/bin/bash" 41 minutes ago Exited (0) 20 minutes ago priceless_shockley
查看容器進程與源鏡像作對比,發生了改變的文件或文件夾
[root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 84d6659c1dd8 0bfe62637189 "/bin/bash" 3 minutes ago Up 2 minutes gallant_hoover 711c165d7e4a 0bfe62637189 "/bin/bash" 20 minutes ago Up 14 minutes heuristic_tereshkova a8dd8894050c 6e709ee70391 "/bin/bash" 42 minutes ago Exited (0) 22 minutes ago priceless_shockley [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos 2 0bfe62637189 21 minutes ago 200MB damowang/centos7 1 6e709ee70391 About an hour ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker run -d -it 0bfe62637189 #啓動一個鏡像的容器進程f4b9ce8842f3f123f4d65dfc9192946c82562ebd683fcf9070005f8d186002a0 [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f4b9ce8842f3 0bfe62637189 "/bin/bash" 17 seconds ago Up 16 seconds wonderful_aryabhata 84d6659c1dd8 0bfe62637189 "/bin/bash" 5 minutes ago Up 3 minutes gallant_hoover 711c165d7e4a 0bfe62637189 "/bin/bash" 21 minutes ago Up 15 minutes heuristic_tereshkova a8dd8894050c 6e709ee70391 "/bin/bash" 44 minutes ago Exited (0) 23 minutes ago priceless_shockley [root@Docker 7]# docker attach f4b9ce8842f3 #切入容器進程 [root@f4b9ce8842f3 /]# cd /damowang/ [root@f4b9ce8842f3 damowang]# pwd /damowang [root@f4b9ce8842f3 damowang]# touch {1..10} #在容器進程中建立文件 [root@f4b9ce8842f3 damowang]# ls 1 10 2 3 4 5 6 7 8 9 [root@f4b9ce8842f3 damowang]# exit #退出容器進程 exit [root@Docker 7]# docker diff f4b9ce8842f3 #查看容器進程的變化 C /damowang A /damowang/1 A /damowang/10 A /damowang/2 A /damowang/3 A /damowang/4 A /damowang/5 A /damowang/6 A /damowang/7 A /damowang/8 A /damowang/9 C /root/.bash_history
時時監測容器的變化狀況
[root@Docker ~]# docker events 2018-07-16T21:56:25.442638653+08:00 container export f4b9ce8842f3f123f4d65dfc9192946c82562ebd683fcf9070005f8d186002a0 (MAINTATNER=damowang, image=0bfe62637189, name=wonderful_aryabhata, org.label-schema.schema-version== 1.0 org.label-schema.name=CentOS Base Image org.label-schema.vendor=CentOS org.label-schema.license=GPLv2 org.label-schema.build-date=20180531)
#前臺時時監控容器的變化若要檢測,須要另外再起一個窗口進行操做
將容器進程的文件系統導出到本地
[root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f4b9ce8842f3 0bfe62637189 "/bin/bash" About an hour ago Up 2 minutes wonderful_aryabhata 84d6659c1dd8 0bfe62637189 "/bin/bash" 2 hours ago Exited (137) 6 minutes ago gallant_hoover 711c165d7e4a 0bfe62637189 "/bin/bash" 2 hours ago Exited (137) 6 minutes ago heuristic_tereshkova a8dd8894050c 6e709ee70391 "/bin/bash" 2 hours ago Exited (0) 2 hours ago priceless_shockley [root@Docker ~]# docker export f4b9ce8842f3 > test.tar #將容器進程導出成一個tar包 [root@Docker ~]# ls anaconda-ks.cfg dockerfile test.tar
用於將export導出的文件系統建立爲一個鏡像
[root@Docker ~]# ls [root@Docker ~]# docker import test.tar damowang/centos:3 #將導出的tar包文件系統生成一個新的鏡像 sha256:a46ed9ea14cb371ddd16ede6a137a7063794da27a0d0641a0bf441e29b0aa909 [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos 3 a46ed9ea14cb 40 seconds ago 200MB damowang/centos 2 0bfe62637189 4 hours ago 200MB damowang/centos7 1 6e709ee70391 4 hours ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB
用於查看一個鏡像的歷史修改紀錄
[root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos 3 a46ed9ea14cb 2 minutes ago 200MB damowang/centos 2 0bfe62637189 4 hours ago 200MB damowang/centos7 1 6e709ee70391 4 hours ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker ~]# docker history a46ed9ea14cb IMAGE CREATED CREATED BY SIZE COMMENT a46ed9ea14cb 2 minutes ago 200MB Imported from - [root@Docker ~]# docker history 0bfe62637189 IMAGE CREATED CREATED BY SIZE COMMENT 0bfe62637189 4 hours ago /bin/bash 80B 6e709ee70391 5 hours ago /bin/sh -c #(nop) CMD ["/home/test/docker.s… 0B 002a6fb94e60 5 hours ago /bin/sh -c chmod +x /home/test/docker.sh 88B ffbac1722ac7 5 hours ago /bin/sh -c #(nop) ADD file:5b33dd15349e0d728… 88B eb1f30c3f53f 5 hours ago /bin/sh -c ln -sfv /usr/share/zoneinfo/Asia/… 33B 79ac7152beab 5 hours ago /bin/sh -c #(nop) LABEL MAINTATNER=damowang 0B 49f7960eb7e4 5 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 5 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B <missing> 5 weeks ago /bin/sh -c #(nop) ADD file:8f4b3be0c1427b158… 200MB
用於查看當前操做系統的docker運行信息
[root@Docker ~]# [root@Docker ~]# docker info Containers: 2 #容器進程2個 Running: 1 #正在運行狀態的容器1個 Paused: 0 Stopped: 1 Images: 8 #一共有8個鏡像 Server Version: 18.03.1-ce Storage Driver: overlay2 Backing Filesystem: xfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 773c489c9c1b21a6d78b5c538cd395416ec50f88 runc version: 4fc53a81fb7c994640722ac585fa9ca548971871 init version: 949e6fa Security Options: seccomp Profile: default Kernel Version: 3.10.0-862.3.3.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 1 Total Memory: 974.6MiB Name: Docker ID: L23S:WCPW:NHU5:UK46:5MRA:OJMB:TOKW:OMYH:CYB6:P5XA:VL2Q:65TL Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Registry Mirrors: https://registry.docker-cn.com/ Live Restore Enabled: false
查看某個鏡像的詳細信息
[root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE damowang/centos 3 a46ed9ea14cb 21 minutes ago 200MB damowang/centos 2 0bfe62637189 4 hours ago 200MB damowang/centos7 1 6e709ee70391 5 hours ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker ~]# docker inspect a46ed9ea14cb [ { "Id": "sha256:a46ed9ea14cb371ddd16ede6a137a7063794da27a0d0641a0bf441e29b0aa909", "RepoTags": [ "damowang/centos:3" ], "RepoDigests": [], "Parent": "", "Comment": "Imported from -", "Created": "2018-07-16T15:52:27.076612643Z", "Container": "", "ContainerConfig": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": null, "Cmd": null, "Image": "", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": null }, "DockerVersion": "18.03.1-ce", "Author": "", "Config": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": null, "Cmd": null, "Image": "", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": null }, "Architecture": "amd64", "Os": "linux", "Size": 199678522, "VirtualSize": 199678522, "GraphDriver": { "Data": { "MergedDir": "/var/lib/docker/overlay2/1541e5357c7ddae19f6655a0cf5a7f002fe80e082abcff749b43c90fba1c7020/merged", "UpperDir": "/var/lib/docker/overlay2/1541e5357c7ddae19f6655a0cf5a7f002fe80e082abcff749b43c90fba1c7020/diff", "WorkDir": "/var/lib/docker/overlay2/1541e5357c7ddae19f6655a0cf5a7f002fe80e082abcff749b43c90fba1c7020/work" }, "Name": "overlay2" }, "RootFS": { "Type": "layers", "Layers": [ "sha256:e54921b330060eb66addbb19163b4bb489584248689f405999cadd2693e99ee6" ] }, "Metadata": { "LastTagTime": "2018-07-16T23:52:27.099320952+08:00" } } ]
強行中止一個或多個正在運行狀態的容器進程
[root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3cc1ab69dc5a a46ed9ea14cb "/bin/bash" 11 seconds ago Up 10 seconds mystifying_mirzakhani [root@Docker ~]# docker kill 3cc1ab69dc5a 3cc1ab69dc5a [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3cc1ab69dc5a a46ed9ea14cb "/bin/bash" 47 seconds ago Exited (137) 4 seconds ago mystifying_mirzakhani
用於將一個鏡像的文件系統導出到本地(export導出的是容器)
#從新build一個鏡像 [root@docker ~]# cd dockerfile/lib/centos/7/ [root@Docker 7]# [root@Docker 7]# docker build -t test:1 . Sending build context to Docker daemon 3.584kB Step 1/6 : FROM centos ---> 49f7960eb7e4 Step 2/6 : LABEL MAINTATNER="damowang" ---> Running in 30d86a376293 Removing intermediate container 30d86a376293 ---> 1e840728b7e7 Step 3/6 : RUN ln -sfv /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ---> Running in 016410b91ccc '/etc/localtime' -> '/usr/share/zoneinfo/Asia/Shanghai' Removing intermediate container 016410b91ccc ---> 35d117846318 Step 4/6 : ADD docker.sh /home/test/ ---> 9cd9a8848ed0 Step 5/6 : RUN chmod +x /home/test/docker.sh ---> Running in 9c1494f9ba1e Removing intermediate container 9c1494f9ba1e ---> 176685b39742 Step 6/6 : CMD ["/home/test/docker.sh"] ---> Running in 3da643e31136 Removing intermediate container 3da643e31136 ---> abc35d57c192 Successfully built abc35d57c192 Successfully tagged test:1 [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 2 seconds ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB #save導出test:1這個鏡像(也能夠用id號) [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 About a minute ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker save test:1 > test.tar [root@Docker 7]# ls Dockerfile docker.sh test.tar yum.log
用於將save導出到本地的tar包,從新加載爲鏡像(和源鏡像的名字標識徹底同樣)
[root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 2 minutes ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker rmi -f test:1 #刪除鏡像test:1 Untagged: test:1 Deleted: sha256:abc35d57c19219bc97806d54382a60745276b50b12d967c80afd334a944e6be4 Deleted: sha256:176685b397420e52b6024fce07991a7157bdd1c3e68be7035e156c91fd6f6b61 Deleted: sha256:3ba486b5820d4edb5a41bb3355d50307c768712ed2e6afce6455b90a977b0a93 Deleted: sha256:9cd9a8848ed022998eb1a51fceaabdf3d49e4ba8f7ff782b5ffda0bee6503db3 Deleted: sha256:b72e52a36c1a3d07366d5b28079ff9eb7f306ad023c1d0432f99f86441ff18fa Deleted: sha256:35d1178463185fac4647212533e83ce88efb4f5f4612cc3b4422f9be1d3b1ed8 Deleted: sha256:6334246fd055cb644551e76a872e4119df37f96b1d51e36eb545cfc78d480086 Deleted: sha256:1e840728b7e7a26279fcc059f493dab49f73a068ad5aebf08d4ecdc8d6ec0062 [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker load < test.tar 18991d1930a2: Loading layer 2.048kB/2.048kB 756520987930: Loading layer 3.584kB/3.584kB 4a48d0dbed96: Loading layer 3.072kB/3.072kB Loaded image: test:1 [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 4 minutes ago 200MB #和原來的鏡像徹底同樣
centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB
用於輸出一個容器進程內的操做日誌
[root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 5 minutes ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker run -d -it test:1 e01ec5ecc6927707619a8737e89d5ae30167a17188dc582672519dd0f3057e7b [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e01ec5ecc692 test:1 "/home/test/docker.sh" 2 seconds ago Up 1 seconds distracted_curran [root@Docker 7]# docker logs e01ec5ecc692 welcome welcome welcome welcome welcome welcome welcome welcome welcome welcome welcome welcome welcome [root@Docker 7]# docker logs --tail 5 e01ec5ecc692 #只顯示容器日誌的後5行 welcome welcome welcome welcome welcome
用於將一個或多個容器的進程暫停和恢復
[root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e01ec5ecc692 test:1 "/home/test/docker.sh" 2 minutes ago Up 2 minutes distracted_curran [root@Docker 7]# docker pause e01ec5ecc692 #暫停容器進程 e01ec5ecc692 [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e01ec5ecc692 test:1 "/home/test/docker.sh" 3 minutes ago Up 3 minutes (Paused) distracted_curran [root@Docker 7]# docker unpause e01ec5ecc692 #恢復容器進程 e01ec5ecc692 [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e01ec5ecc692 test:1 "/home/test/docker.sh" 3 minutes ago Up 3 minutes distracted_curran
用於列出一個容器的端口映射及協議
[root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 10 minutes ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker run -d -it -p 2222:22 test:1 #啓動一個鏡像的容器進程 -p 指定本地2222端口映射到容器的22端口 376d22e0957cf8216a9aabb417870384e896934566a20d8858374c0bf6002d9c [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 376d22e0957c test:1 "/home/test/docker.sh" 29 seconds ago Up 28 seconds 0.0.0.0:2222->22/tcp gallant_sinoussi e01ec5ecc692 test:1 "/home/test/docker.sh" 5 minutes ago Up 5 minutes distracted_curran
給容器進程重命名
[root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 376d22e0957c test:1 "/home/test/docker.sh" About a minute ago Up About a minute 0.0.0.0:2222->22/tcp gallant_sinoussi e01ec5ecc692 test:1 "/home/test/docker.sh" 6 minutes ago Up 6 minutes distracted_curran [root@Docker 7]# docker rename gallant_sinoussi damowang [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 376d22e0957c test:1 "/home/test/docker.sh" About a minute ago Up About a minute 0.0.0.0:2222->22/tcp damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 6 minutes ago Up 6 minutes distracted_curran
重啓一個容器進程
[root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 376d22e0957c test:1 "/home/test/docker.sh" 2 minutes ago Up 2 minutes 0.0.0.0:2222->22/tcp damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 7 minutes ago Up 7 minutes distracted_curran [root@Docker 7]# docker restart 376d22e0957c 376d22e0957c [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 376d22e0957c test:1 "/home/test/docker.sh" 2 minutes ago Up 3 seconds 0.0.0.0:2222->22/tcp damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 8 minutes ago Up 8 minutes distracted_curran
用於時時輸出容器的資源使用狀況
[root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 376d22e0957c test:1 "/home/test/docker.sh" 4 minutes ago Up About a minute 0.0.0.0:2222->22/tcp damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 9 minutes ago Up 9 minutes distracted_curran [root@Docker 7]# docker stats 376d22e0957c CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 376d22e0957c damowang 0.00% 288KiB / 974.6MiB 0.03% 648B / 0B 0B / 0B 2
#--no-tream只輸出一次 [root@Docker 7]# docker stats 376d22e0957c --no-stream CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS 376d22e0957c damowang 0.00% 284KiB / 974.6MiB 0.03% 648B / 0B 0B / 0B 2
用於從一個指定的鏡像建立另一個鏡像
[root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 18 minutes ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker tag test:1 test:2 [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 19 minutes ago 200MB test 2 abc35d57c192 19 minutes ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker rmi test:2 Untagged: test:2 [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 19 minutes ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker 7]# docker tag abc35d57c192 test:2 [root@Docker 7]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 19 minutes ago 200MB test 2 abc35d57c192 19 minutes ago 200MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB
用於顯示指定容器的進程信息
[root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 376d22e0957c test:1 "/home/test/docker.sh" 8 minutes ago Up 6 minutes 0.0.0.0:2222->22/tcp damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 14 minutes ago Up 14 minutes distracted_curran [root@Docker 7]# docker top 376d22e0957c UID PID PPID C STIME TTY TIME CMD root 2644 2629 0 00:37 pts/0 00:00:00 /bin/bash /home/test/docker.sh root 2893 2644 0 00:44 pts/0 00:00:00 sleep 5
用於調整一個或多個容器的啓動配置
[root@Docker 7]# docker update --help Usage: docker update [OPTIONS] CONTAINER [CONTAINER...] Update configuration of one or more containers Options: --blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) --cpu-period int Limit CPU CFS (Completely Fair Scheduler) period --cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota --cpu-rt-period int Limit the CPU real-time period in microseconds --cpu-rt-runtime int Limit the CPU real-time runtime in microseconds -c, --cpu-shares int CPU shares (relative weight) --cpus decimal Number of CPUs #調整CPU --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1) --cpuset-mems string MEMs in which to allow execution (0-3, 0,1) --kernel-memory bytes Kernel memory limit -m, --memory bytes Memory limit #調整內存 --memory-reservation bytes Memory soft limit --memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap --restart string Restart policy to apply when a container exits
version用於顯示docker的版本信息
wait用於捕捉一個或多個容器的退出狀態,並返回退出狀態碼
#顯示docker版本信息 [root@Docker 7]# docker version Client: Version: 18.03.1-ce API version: 1.37 Go version: go1.9.5 Git commit: 9ee9f40 Built: Thu Apr 26 07:20:16 2018 OS/Arch: linux/amd64 Experimental: false Orchestrator: swarm Server: Engine: Version: 18.03.1-ce API version: 1.37 (minimum version 1.12) Go version: go1.9.5 Git commit: 9ee9f40 Built: Thu Apr 26 07:23:58 2018 OS/Arch: linux/amd64 Experimental: false #監聽容器的退出狀態並返回狀態碼 [root@Docker 7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 376d22e0957c test:1 "/home/test/docker.sh" 12 minutes ago Up 10 minutes 0.0.0.0:2222->22/tcp damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 18 minutes ago Up 18 minutes distracted_curran [root@Docker 7]# docker wait 376d22e0957c #須要在開一個窗口stop這個容器進程再查看 137
login用於登錄docker hub官方公有倉庫
logout用於登出docker hub官方公有倉庫
push用於將本地鏡像提交到docker hub
因爲docker hub國內已經沒法註冊,所以沒法演示上述內容
DockerHub官方公有鏡像倉庫:https://hub.docker.com/
將Docker主機數據掛載到容器
Docker提供三種不一樣方式將數據從宿主機掛載到容器中:volumes,bind mounts和tmpfs。
#建立一個卷 [root@Docker ~]# docker volume create nginx-vol nginx-vol [root@Docker ~]# docker volume ls DRIVER VOLUME NAME local nginx-vol [root@Docker ~]# docker volume inspect nginx-vol [ { "CreatedAt": "2018-07-17T02:16:40+08:00", #建立時間 "Driver": "local", #驅動 "Labels": {}, "Mountpoint": "/var/lib/docker/volumes/nginx-vol/_data", #掛載點 "Name": "nginx-vol", #卷名 "Options": {}, "Scope": "local" } ] #下載一個nginx官方鏡像 [root@Docker ~]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx 683abbb4ea60: Pull complete a470862432e2: Pull complete 977375e58a31: Pull complete Digest: sha256:a65beb8c90a08b22a9ff6a219c2f363e16c477b6d610da28fe9cba37c2c3a2ac Status: Downloaded newer image for nginx:latest [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE test 1 abc35d57c192 2 hours ago 200MB test 2 abc35d57c192 2 hours ago 200MB nginx latest 3c5a05123222 10 days ago 109MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB [root@Docker ~]# docker run -dit --name=nginx-test --mount src=nginx-vol,dst=/usr/share/nginx/html nginx cd5c493d1633074f7fe2485f626bcda38ea96a320e08cbe40368f450445658f8 --name:容器的名字 --mount:掛載 src:源卷的名字 dst:掛載到容器中的路徑 [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cd5c493d1633 nginx "nginx -g 'daemon of…" 8 seconds ago Up 7 seconds 80/tcp nginx-test 376d22e0957c test:1 "/home/test/docker.sh" 2 hours ago Exited (137) 2 hours ago damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 2 hours ago Up 2 hours distracted_curran #向容器中的掛載目錄建立文件,查看是否掛載成功 [root@Docker ~]# docker exec nginx-test touch /usr/share/nginx/html/test.txt [root@Docker ~]# docker exec nginx-test ls /usr/share/nginx/html/ 50x.html index.html test.txt #成功寫入 [root@docker 7]# ls /var/lib/docker/volumes/nginx-vol/_data/ 50x.html index.html test.txt #成功 #清理容器進程 [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cd5c493d1633 nginx "nginx -g 'daemon of…" About a minute ago Up About a minute 80/tcp nginx-test 376d22e0957c test:1 "/home/test/docker.sh" 2 hours ago Exited (137) 2 hours ago damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 2 hours ago Up 2 hours distracted_curran [root@Docker ~]# docker stop nginx-test nginx-test [root@Docker ~]# docker rm nginx-test nginx-test [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 376d22e0957c test:1 "/home/test/docker.sh" 2 hours ago Exited (137) 2 hours ago damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 2 hours ago Up 2 hours distracted_curran [root@Docker ~]# ls /var/lib/docker/volumes/nginx-vol/_data/ 50x.html index.html test.txt #清理容器後,掛載的卷的數據仍舊存在 #從新啓動鏡像nginx的容器進程 [root@Docker ~]# docker run -dit --name nginx-test -p 88:80 --mount src=nginx-vol,dst=/usr/share/nginx/html nginx ef4c927e1f49e1774525c47fbe483fd8fb0abe7672488bc5ba45ada5d0310cc4 [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ef4c927e1f49 nginx "nginx -g 'daemon of…" 5 seconds ago Up 4 seconds 0.0.0.0:88->80/tcp nginx-test 376d22e0957c test:1 "/home/test/docker.sh" 2 hours ago Exited (137) 2 hours ago damowang e01ec5ecc692 test:1 "/home/test/docker.sh" 2 hours ago Up 2 hours distracted_curran
docker run的-p參數:指定端口的映射,88:80的意思就是將宿主機88端口的訪問映射到容器進程的80端口
經過瀏覽器訪問宿主機的88端口,進而訪問容器進程的80端口
#在數據卷nginx-vol裏換一個網頁在訪問 [root@Docker ~]# ls /var/lib/docker/volumes/nginx-vol/_data/ 50x.html index.html test.txt [root@Docker ~]# rm -rf /var/lib/docker/volumes/nginx-vol/_data/* [root@Docker ~]# echo "my name's damowang" > /var/lib/docker/volumes/nginx-vol/_data/index.html
啓動一個鏡像nginx的進程,讓兩個nginx的容器進程公用一個數據卷nginx-vol
[root@Docker ~]# docker run -dit --name nginx-test2 -p 89:80 --mount src=nginx-vol,dst=/usr/share/nginx/html nginx 9cedd1d6a884bbd23ea28fa95ff415e4296d6b7a103378463545c85eee468df3 [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9cedd1d6a884 nginx "nginx -g 'daemon of…" 50 seconds ago Up 49 seconds 0.0.0.0:89->80/tcp nginx-test2 ef4c927e1f49 nginx "nginx -g 'daemon of…" 14 minutes ago Up 14 minutes 0.0.0.0:88->80/tcp nginx-test
訪問docker宿主機的89端口
[root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9cedd1d6a884 nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:89->80/tcp nginx-test2 ef4c927e1f49 nginx "nginx -g 'daemon of…" 15 minutes ago Up 15 minutes 0.0.0.0:88->80/tcp nginx-test [root@Docker ~]# docker run -dit --name nginx-test3 -p 90:80 --mount type=bind,src=/var/lib/docker/volumes/nginx-vol/_data,dst=/usr/share/nginx/html nginx fa64fffd4db42f82d2e49e6a67d7bc27e4f400ab8705675e7f7564db2e482bb8 [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fa64fffd4db4 nginx "nginx -g 'daemon of…" 5 seconds ago Up 3 seconds 0.0.0.0:90->80/tcp nginx-test3 9cedd1d6a884 nginx "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:89->80/tcp nginx-test2 ef4c927e1f49 nginx "nginx -g 'daemon of…" 16 minutes ago Up 16 minutes 0.0.0.0:88->80/tcp nginx-test
訪問docker宿主機的90端口
bind mounts能夠掛載宿主機上的任意目錄,而volume先得建立後才能掛載
下載一個wordpress博客
wget https://cn.wordpress.org/wordpress-4.7.4-zh_CN.tar.gz
#下載MySQL5.7版本鏡像 [root@Docker ~]# docker pull mysql:5.7 5.7: Pulling from library/mysql 683abbb4ea60: Already exists 0550d17aeefa: Pull complete 7e26605ddd77: Pull complete 9882737bd15f: Pull complete 999c06ab75f6: Pull complete c71d695f9937: Pull complete c38f847c1491: Pull complete 74f9c61f40bf: Pull complete 30b252a90a12: Pull complete 9f92ebb7da55: Pull complete 90303981d276: Pull complete Digest: sha256:1203dfba2600f140b74e375a354b1b801fa1b32d6f80fdee5f155d1e9f38c841 Status: Downloaded newer image for mysql:5.7 [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 3c5a05123222 10 days ago 109MB mysql 5.7 66bc0f66b7af 2 weeks ago 372MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB #建立一個自定義網絡 [root@Docker ~]# docker network create lnmp 2b1f72c8c9d1cda46549081ef807d12925363aa36cf0d2750cdc990c13b31d23 [root@Docker ~]# docker network ls NETWORK ID NAME DRIVER SCOPE a2232bb435d4 bridge bridge local 31ad8236c965 host host local 2b1f72c8c9d1 lnmp bridge local #默認爲網橋 4d18e42bc424 none null local #啓動MySQL數據庫容器 [root@Docker ~]# docker run -dit --name lnmp_mysql --network lnmp -p 3306:3306 --mount src=mysql-vol,dst=/var/lib/mysql -e MYSQL_ROOT_PASSWORD=475541270 mysql:5.7 --character-set-server=utf8 d5f911e7d17415c64695363619917925f9f23b2b17edd1ec2131f7bcce2e9542 [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d5f911e7d174 mysql:5.7 "docker-entrypoint.s…" 18 seconds ago Up 17 seconds 0.0.0.0:3306->3306/tcp lnmp_mysql fa64fffd4db4 nginx "nginx -g 'daemon of…" 12 minutes ago Up 12 minutes 0.0.0.0:90->80/tcp nginx-test3 9cedd1d6a884 nginx "nginx -g 'daemon of…" 15 minutes ago Up 15 minutes 0.0.0.0:89->80/tcp nginx-test2 ef4c927e1f49 nginx "nginx -g 'daemon of…" 28 minutes ago Up 28 minutes 0.0.0.0:88->80/tcp nginx-test 自定義網絡lnmp若是不提早建立的話,在啓動容器進程時會報錯 若是沒有提早pull好mysql:5.6那麼容器在啓動時會自動下載對應鏡像 若是沒有提早docker volume create mysql-vol,那麼容器啓動時會自動建立 #查看容器lnmp_mysql的日誌輸出 [root@Docker ~]# docker logs lnmp_mysql #查看容器裏啓動的進程 [root@Docker ~]# docker top lnmp_mysql UID PID PPID C STIME TTY TIME CMD polkitd 5363 5347 0 02:49 pts/0 00:00:00 mysqld --character-set-server=utf8
[root@Docker ~]# docker exec lnmp_mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD" -e"create database bdk"' mysql: [Warning] Using a password on the command line interface can be insecure.
由於已經將mysql容器的3306端口映射到了docker宿主機的3306,所以咱們訪問本地便可
#安裝mysql客戶端 [root@docker ~]# yum -y install mysql #查看本機IP地址 [root@Docker ~]# hostname -I | xargs -n1 | head -1 192.168.94.103 #遠程方式鏈接docker宿主機的3306端口 [root@Docker ~]# mysql -h192.168.94.103 -P3306 -uroot -p475541270 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.22 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | bdk | #已成功建立bdk庫 | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
#建立一個網頁目錄 [root@Docker ~]# mkdir -p /app/damowang [root@Docker ~]# ll -d /app/damowang drwxr-xr-x 2 root root 6 7月 17 03:00 /app/damowang #下載richarvey/nginx-php-fpm鏡像 [root@docker ~]# docker pull richarvey/nginx-php-fpm [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 3c5a05123222 10 days ago 109MB richarvey/nginx-php-fpm latest 26c0e6f09c52 11 days ago 300MB mysql 5.7 66bc0f66b7af 2 weeks ago 372MB centos 7 49f7960eb7e4 5 weeks ago 200MB centos latest 49f7960eb7e4 5 weeks ago 200MB #啓動richarvey/nginx-php-fpm鏡像的容器 [root@Docker ~]# docker run -dit --name lnmp_web --network lnmp -p 88:80 --mount type=bind,src=/app/damowang,dst=/var/www/html richarvey/nginx-php-fpm 56a0139a59f6ac18520a7edeece8fc340a65801afaa7c8e72a6d17ae8c25deff [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 56a0139a59f6 richarvey/nginx-php-fpm "docker-php-entrypoi…" 2 minutes ago Up 2 minutes 443/tcp, 9000/tcp, 0.0.0.0:88->80/tcp lnmp_web d5f911e7d174 mysql:5.7 "docker-entrypoint.s…" 18 minutes ago Up 18 minutes 0.0.0.0:3306->3306/tcp lnmp_mysql
[root@Docker ~]# tar xf wordpress-4.7.4-zh_CN.tar.gz -C /app/damowang/ [root@Docker ~]# cd /app/damowang/ [root@Docker damowang]# ls wordpress [root@Docker damowang]#
指定本機ip地址的88端口 若是不通 , 那麼就在 ip:88/wordpress/ 後面加上index.php
或者關閉 iptables 和 firewall
關閉selinux