Docker是一個開源的引擎,能夠輕鬆的爲任何應用建立一個輕量級的、可移植的、自給自足的容器。讓開發者能夠打包他們的應用到一個可移植的容器中,而後發佈到任何Linux機器上,還能夠實現虛擬化。容器是徹底使用沙箱機制,相互之間不會有任何接口。html
1 Docker和虛擬機的區別 :linux
容器是一種輕量級的基於操做系統級別的虛擬技術,不像虛擬機具備完整的CPU、內存和磁盤,是基於硬件的虛擬化技術。容器和宿主機共享內核,全部容器共享操做系統。nginx
容器啓動快 開源免費 輕量級虛擬化可以部署100-1000個容器docker
2 重要概念:vim
1)組件:
centos
docker鏡像(Images):鏡像運行以後成爲容器,包含了應用程序和其運行時依賴環境的只讀文件。bash
docker倉庫:Registry是Docker鏡像的中央存儲倉庫網絡
docker容器:容器是獨立運行的一個或一組應用。能夠當作是一個簡易版的Linux運行環境(能夠理解爲在沙盒中運行的進程)。tcp
3 安裝ide
系統環境:CentOS 7.2
[zwj@localhost home]$ sudo yum -y install docker (CentOS6的安裝:yum -y install docker-io)
[zwj@localhost home]$ sudo systemctl start docker.service
[zwj@localhost home]$ sudo systemctl status docker.service
[zwj@localhost home]$ sudo systemctl enable docker.service
[zwj@localhost system]$ docker -v
Docker version 1.12.6, build 1398f24/1.12.6
4 基本操做
經常使用命令:
搜索某個鏡像:docker search name
獲取某個鏡像:docker pull name
查看鏡像:docker p_w_picpaths
刪除鏡像:docker rmi name
啓動容器:docker start 容器ID
中止容器:docker stop 容器ID
查看容器:docker ps -a
進入容器:docker exec 或 docker attach、nsenter,如:docker exec -it 6f48b7e16fe8 /bin/bash,建議使用nsenter命令進入
刪除容器:docker rm 容器ID(須要先中止容器)
建立容器,退出時自動刪除容器:docker run --rm centos /bin/echo "hehe"
4.1 鏡像和容器
下載鏡像:
[zwj@localhost system]$ sudo docker pull centos
查看本地鏡像:
[zwj@localhost ~]$ sudo docker p_w_picpaths
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 3bee3060bfc8 12 days ago 192.5 MB
使用centos鏡像建立一個容器,並在容器中執行命令(若是本地沒有鏡像,會從公共倉庫去下載):
[zwj@localhost ~]$ sudo docker run centos /bin/echo "hehe"
hehe
查看當前全部容器,每一個容器有惟一的ID和名稱:
[zwj@localhost ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ba7ecb84d0e1 centos "/bin/echo hehe" 5 minutes ago Exited (0) 5 minutes ago silly_carson
建立一個name叫mydocker的容器,而且登錄到容器:參數-t:表示爲容器分配一個僞終端,-i:啓動一個容器並進入交互模式,-d:後臺運行
[zwj@localhost ~]$ sudo docker run --name mydocker -t -i centos /bin/bash
[root@6f48b7e16fe8 /]#
退出容器後,容器處於退出狀態
[root@6f48b7e16fe8 ~]# exit
exit
[zwj@localhost ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6f48b7e16fe8 centos "/bin/bash" 10 minutes ago Exited (0) About a minute ago mydocker
ba7ecb84d0e1 centos "/bin/echo hehe" 24 minutes ago Exited (0) 23 minutes ago silly_carson
有什麼辦法在退出以後容器依然處於啓動狀態呢,步驟以下:
[zwj@localhost ~]$ sudo yum -y install util-linux
[zwj@localhost ~]$ sudo docker start 6f48b7e16fe8 #啓動容器
6f48b7e16fe8
[zwj@localhost ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6f48b7e16fe8 centos "/bin/bash" About an hour ago Up About a minute mydocker
ba7ecb84d0e1 centos "/bin/echo hehe" About an hour ago Exited (0) About an hour ago silly_carson
[zwj@localhost ~]$ sudo docker inspect --format "``.`State`.`Pid`" 6f48b7e16fe8 #獲取容器PID
3820
[zwj@localhost ~]$ sudo nsenter -t 3820 -u -i -n -p #進入容器
[root@6f48b7e16fe8 zwj]#
[root@6f48b7e16fe8 zwj]# exit #退出後,容器依然處於啓動狀態
logout
[zwj@localhost ~]$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6f48b7e16fe8 centos "/bin/bash" About an hour ago Up 16 minutes mydocker
ba7ecb84d0e1 centos "/bin/echo hehe" About an hour ago Exited (0) About an hour ago silly_carson
4.2 網絡和存儲
建立並運行nginx容器,建立成功後會返回容器ID號,可是查看時一般是顯示前幾位而不是整個ID號。-d:後臺運行,-P:默認匹配docker容器端口到宿主機隨機端口。
[zwj@localhost system]$ sudo docker run -d -P nginx
06b751db90b24ca8750c6d25f0b424e72dba36d9261d0d09817c435ada5c0e68
由下面的輸出能夠看到,把本機的32768端口映射到了容器的80號端口。
[zwj@localhost system]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
06b751db90b2 nginx "nginx -g 'daemon off" 7 minutes ago Up 7 minutes 0.0.0.0:32768->80/tcp distracted_ride
6f48b7e16fe8 centos "/bin/bash" 2 hours ago Up About an hour mydocker
從客戶端訪問結果以下:
查看容器日誌:
[zwj@localhost system]$ sudo docker logs 06b751db90b2
查看日誌的實時輸出:
[zwj@localhost system]$ sudo docker logs -f 06b751db90b2
也能夠指定特定端口映射,注意docker run會另外建立一個容器
[zwj@localhost system]$ sudo docker run -d -p 88:80 nginx
f54ba5b475a04371ba84a2b193ff691d7545461e5f6d85309967fa0e86ca2b45
[zwj@localhost system]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f54ba5b475a0 nginx "nginx -g 'daemon off" 11 seconds ago Up 6 seconds 0.0.0.0:88->80/tcp reverent_liskov
06b751db90b2 nginx "nginx -g 'daemon off" 26 minutes ago Up 26 minutes 0.0.0.0:32768->80/tcp distracted_ride
6f48b7e16fe8 centos "/bin/bash" 2 hours ago Up About an hour mydocker
存儲之數據卷:
使用-v參數建立數據卷而且掛載到容器,-v /data
[zwj@localhost system]$ sudo docker run -it --name volume-test1 -v /data centos
[root@834a476135d3 /]# ls -l /data
total 0
使用-v參數掛載本地目錄至容器,-v src:dst
[zwj@localhost ~]$ sudo docker run -it --name test -v /opt:/opt centos
掛載並授予讀寫的權限,
[zwj@localhost ~]$ sudo docker run -it --name test -v /opt:/opt:rw centos
除了目錄,還能夠掛載文件。
存儲之數據卷容器 --volumes-from:一般適應於一個容器訪問另外一個容器,
建立一個容器取名nfs
[zwj@localhost ~]$ sudo docker run -d --name nfs -v /data centos
d65376b3a601dc338ad93cf2ab3ec163218e595cd36e4a4353f82e08b38b2670
建立另外一個容器test01,而且把nfs掛載到test01,能夠看到test01根目錄下有/data目錄
[zwj@localhost ~]$ sudo docker run -it --name test01 --volumes-from nfs centos
[root@d38127cd879b /]# ls
anaconda-post.log bin data dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
4.3 手動構建鏡像
下載基礎鏡像
[zwj@localhost system]$ sudo docker pull centos
建立容器,而且進入容器
[zwj@localhost ~]$ sudo docker run --name mynginx -it centos
在容器中安裝epel源
[root@1418e12567d5 /]# rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
在容器中安裝nginx
[root@1418e12567d5 /]# yum -y install nginx
而後建立新的鏡像,
[zwj@localhost ~]$ sudo docker commit -m "my nginx" -a "zwj" 1418e12567d5 aa/a01:1.1
參數說明:
-m:提交的描述信息
-a:指定鏡像做者
1418e12567d5:容器ID
aa/a01:指定要建立的鏡像名
查看鏡像,aa/a01鏡像已存在
[zwj@localhost ~]$ sudo docker p_w_picpaths
REPOSITORY TAG IMAGE ID CREATED SIZE
aa/a01 1.1 797ed1764943 33 seconds ago 367.9 MB
docker.io/centos latest 3bee3060bfc8 12 days ago 192.5 MB
docker.io/nginx latest 958a7ae9e569 2 weeks ago 109.4 MB
基於aa/a01鏡像建立新的容器nginx01
[zwj@localhost ~]$ sudo docker run -it --name nginx01 aa/a01:1.1
修改容器nginx01中nginx配置文件,使容器在後臺運行時nginx可以運行
[root@bd27b093bf3e /]# vi /etc/nginx/nginx.conf
daemon off;
user nginx;
基於上次修改,再次提交建立新的鏡像
[zwj@localhost ~]$ sudo docker commit -m "my nginx" -a "zwj" bd27b093bf3e aa/a01:1.2
[zwj@localhost ~]$ sudo docker p_w_picpaths
REPOSITORY TAG IMAGE ID CREATED SIZE
aa/a01 1.2 42da08d9d427 2 minutes ago 367.9 MB
aa/a01 1.1 797ed1764943 22 minutes ago 367.9 MB
docker.io/centos latest 3bee3060bfc8 12 days ago 192.5 MB
docker.io/nginx latest 958a7ae9e569 2 weeks ago 109.4 MB
基於新鏡像建立容器,而且在容器中運行nginx命令
[zwj@localhost ~]$ sudo docker run -d -p 82:80 aa/a01:1.2 nginx
[zwj@localhost ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
999ce7bd6559 aa/a01:1.2 "nginx" About a minute ago Up About a minute 0.0.0.0:82->80/tcp prickly_meninsky
4.4 Docker-file構建鏡像:由一系列參數和命令構成的腳本,最終建立成新的鏡像。
[zwj@www ~]$ sudo mkdir -pv dockerfile/nginx
mkdir: created directory ‘dockerfile’
mkdir: created directory ‘dockerfile/nginx’
[zwj@www ~]$ sudo tree dockerfile/
dockerfile/
└── nginx
1 directory, 0 files
[zwj@www ~]$ cd dockerfile/nginx/
[zwj@www nginx]$ sudo vim index.html
<h>Server 192.168.3.158</h>
#創建Dockerfile文件,其中#號表示註釋
[zwj@www nginx]$ sudo vim Dockerfile
#This is Dockerfile
#Version:1.1
#Author:zwj
#Base p_w_picpath
FROM centos
#Author
MAINTAINER zwj 2962372861@qq.com
#Commands
RUN rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
RUN yum install -y nginx
ADD index.html /usr/share/nginx/html/index.html
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx"]
#根據文件建立鏡像,-t指定鏡像名,後面是Dockerfile文件的絕對路徑(目錄的路徑)
[zwj@www nginx]$ sudo docker build -t test/nginx:1.0 /home/zwj/dockerfile/nginx/
#查看建立的鏡像
[zwj@www nginx]$ sudo docker p_w_picpaths
REPOSITORY TAG IMAGE ID CREATED SIZE
test/nginx 1.0 86dcc8884006 46 seconds ago 400.8 MB
docker.io/centos latest 36540f359ca3 10 days ago 192.5 MB
#如今可使用建立的鏡像創建容器
[zwj@www nginx]$ sudo docker run -d -p 808:80 --name nginx test/nginx:1.0
[zwj@www nginx]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6bf9453e4b71 test/nginx:1.0 "nginx" 38 seconds ago Up 37 seconds 0.0.0.0:808->80/tcp nginx
附:Dockerfile文件的命令介紹:
FROM:必須是Dockerfile的首個命令,用於定義鏡像源
MAINTAINER:用於聲明做者
ADD:從源文件系統複製文件到目標容器的文件系統
CMD:描述容器啓動後,執行的程序
ENTRYPOINT:配置一個容器使之可執行化,
ENV:用於設置環境變量,如:key=value
EXPOSE:用來指定端口
RUN:是Dockerfile執行命令的核心部分,它接受命令做爲參數並用於建立的鏡像
USER:設置容器的UID
VOLUME:用於讓你的容器訪問主機上的目錄
WORKDIR:用於設置CMD指明的命令的運行目錄
4.5 創建私有倉庫
#下載registry鏡像(用於創建私有倉庫)
[zwj@www nginx]$ sudo docker pull registry
[zwj@www nginx]$ sudo docker p_w_picpaths
REPOSITORY TAG IMAGE ID CREATED SIZE
test/nginx 1.0 86dcc8884006 39 minutes ago 400.8 MB
docker.io/centos latest 36540f359ca3 10 days ago 192.5 MB
docker.io/registry latest c2a449c9f834 2 weeks ago 33.18 MB
#使用該鏡像建立一個容器:默認狀況下,會將倉庫存放於容器內的/tmp/registry目錄下
[zwj@www ~]$ sudo docker run -d -p 5000:5000 registry
b6b84915dbd24426c6cfae5737555f4a9e3f3ef2145ed6317f347ff99c1e119e
[zwj@www ~]$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b6b84915dbd2 registry "/entrypoint.sh /etc/" 18 minutes ago Up 18 minutes 0.0.0.0:5000->5000/tcp lonely_heisenberg
#標記本地鏡像:
[zwj@www ~]$ sudo docker tag test/nginx:1.0 120.77.148.249:5000/zwj/nginx:1.1
#把鏡像推送至倉庫:
[zwj@www ~]$ sudo docker push 120.77.148.249:5000/zwj/nginx:1.1
生產環境應用:
1)Docker Web管理工具:shipyard
2)規劃
3)準備好基礎鏡像