Docker概述
Docker 是 PaaS 提供商 dotCloud 開源的一個基於 LXC 的高級容器引擎,源代碼托管在 Github 上, 基於go語言並遵從Apache2.0協議開源。
Docker是通過內核虛擬化技術(namespace以及cgroups等)來提供容器的資源隔離與安全保障。由於Docker通過操作系統層的虛擬化實現隔離,所以Docker容器在運行時,不需要類似虛擬機( VM)額外的操作系統開銷,提高資源利用率。
原理:
建立====》傳送====》運行
架構:
C/S架構
組件:
鏡像(Image)
容器(Container)
倉庫(Repository)
與VM的區別
docker與Openstack的對比
Docker能干嘛
簡單配置、代碼流水線管理、開發效率、應用隔離、服務器整合、調試能力、多租戶、快速部署
Docker改變了什么?
面向產品:產品交付
面向開發:簡化環境配置
面向測試:多版本測試
面向運維:環境一致性
面向架構:自動化擴容
Docker的部署安裝
環境准備
yum install -y docker
systemctl start docker
systemctl enable docker
鏡像的查看
[root@linux-node2 ~]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE docker.io/centos latest 60e65a8e4030 36 hours ago 196.6 MB docker.io/nginx latest 813e3731b203 9 days ago 133.8 MB docker.io/registry latest a8706c2bfd21 2 weeks ago 422.8 MB
鏡像倉庫 標簽 鏡像ID 創建時間 鏡像大小
鏡像的下載、導出和導入
[root@linux-node2 ~]# docker save centos > /opt/centos.tar.gz [root@linux-node2 ~]# docker load < /opt/centos.tar.gz
[root@linux-node2 ~]# docker pull centos
這次咱們做實驗需要三個鏡像: nginx、centos、registry
鏡像的刪除
docker:命令 rmi:參數 后面
[root@linux-node2 ~]# docker rmi 813e3731b203
你的第一次(創建容器)
[root@linux-node2 ~]# docker run centos /bin/echo "hehe" hehe
命令解讀:使用centos鏡像,run執行命令,使用echo命令輸出hehe
查看容器狀態
可以使用docker ps只能看見存活的容器,docker ps -a 查看全部的容器
容器ID 使用的鏡像 執行的命令 創建的時間 狀態 端口 名稱(如果不指定,自動生成)
[root@linux-node2 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES daeb4d7f7aab centos "/bin/echo hehe" About a minute ago Exited (0) About a minute ago insane_einstein
創建容器
--name:指定容器名稱
-t :分配一個tty終端
-i :容器的標准輸保持打開的狀態
[root@linux-node2 ~]# docker run --name mydocker -t -i centos /bin/bash [root@94ab7a046f7c /]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.1 11772 1872 ? Ss 03:42 0:00 /bin/bash root 14 0.0 0.0 35888 1472 ? R+ 03:43 0:00 ps aux
這種方式創建自動進入容器,開啟的容器只執行/bin/bash;
在容器中查看主機名
[root@94ab7a046f7c /]# hostname 94ab7a046f7c [root@94ab7a046f7c /]# exit
啟動、停止容器
[root@linux-node2 ~]# docker stop ID
[root@linux-node2 ~]# docker start ID
進入容器
方式一:
[root@linux-node2 ~]# docker attach 94ab7a046f7c
[root@94ab7a046f7c /]#
方式二:
先獲取進程的PID然后在通過pid進入容器
[root@linux-node2 ~]# docker inspect --format "{{.State.Pid}}" 94ab7a046f7c 4101 [root@linux-node2 ~]# nsenter -t 4101 -u -i -p [root@linux-node2 opt]# nsenter --help -t, --target <pid> target process to get namespaces from -m, --mount [=<file>] enter mount namespace -u, --uts [=<file>] enter UTS namespace (hostname etc) -i, --ipc [=<file>] enter System V IPC namespace -n, --net [=<file>] enter network namespace -p, --pid [=<file>] enter pid namespace -r, --root [=<dir>] set the root directory -w, --wd [=<dir>] set the working directory -F, --no-fork do not fork before exec'ing <program>
通過腳本實現進入容器
#/bin/sh PID=$(docker inspect --format "{{.State.Pid}}" $1) nsenter -t $PID -p -i -u -n
刪除容器
[root@linux-node2 ~]# docker rm ID/名稱 加-f 強制刪除,包括正在運行中的容器
映射
隨機映射
端口的映射是系統自動分配的?
[root@linux-node2 ~]# docker run -d -P nginx 90316d97ee975b4e62e1927a9fb31f20703556b1a3ea07880d0c68dcb5bbd3bb [root@linux-node2 ~]# docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 90316d97ee97 nginx "nginx -g 'daemon off" 25 seconds ago Up 23 seconds 0.0.0.0:32769->80/tcp, 0.0.0.0:32768->443/tcp ecstatic_almeida
指定映射
指定端口的映射
[root@linux-node2 ~]# docker run -d -p 81:80 nginx 0294a8f5b4fc81ba31383a8eb98ec62b136826eba92360c84afd87bf1bf819fc [root@linux-node2 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0294a8f5b4fc nginx "nginx -g 'daemon off" 11 seconds ago Up 10 seconds 443/tcp, 0.0.0.0:81->80/tcp admiring_ramanujan
查看日志
[root@linux-node2 ~]# docker log +ID
數據管理
數據卷
默認掛載目錄
創建一個數據卷,名稱是volume-test1,掛載到data下默認掛載目錄
[root@linux-node2 ~]# docker run -it --name volume-test1 -v /data centos [root@1768d6414cfc /]# ls -l /data/ total 0
打開一個新的窗口
列出容器的所有信息,查看mounts模塊
[root@linux-node2 ~]# docker inspect 1768d6414cfc "Mounts": [ { "Name": "55c97df0276bb8879398b4e7286fc41f9872e9203267da7e23060e24ba06d167", "Source": "/var/lib/docker/volumes/55c97df0276bb8879398b4e7286fc41f9872e9203267da7e23060e24ba06d167/_data", "Destination": "/data", "Driver": "local", "Mode": "", "RW": true } ],
查找掛載點並進入
查看掛載的位置 [root@linux-node2 ~]# ll /var/lib/docker/volumes/55c97df0276bb8879398b4e7286fc41f9872e9203267da7e23060e24ba06d167/_data 總用量 0 進入到掛載點 [root@linux-node2 ~]# cd /var/lib/docker/volumes/55c97df0276bb8879398b4e7286fc41f9872e9203267da7e23060e24ba06d167/_data
創建一個cgt測試,並重新回到容器中查看
[root@linux-node2 _data]# mkdir cgt 去容器中查看 [root@1768d6414cfc /]# ls -l /data/ total 4 drwxr-xr-x 2 root root 4096 Jan 4 14:04 cgt
指定掛載目錄
將/opt掛載到/opt目錄下 [root@linux-node2 ~]# docker run -it --name volume-test1 -v /opt:/opt centos
指定權限
只需要在掛載后面加上權限即可。
加讀寫rw;只讀ro
[root@linux-node2 ~]# docker run -it --name volume-test1 -v /opt:/opt:rw centos
掛載單個文件
記錄歷史記錄
[root@linux-node2 ~]# docker run -it -v ~/.bash_history:/.bash_history centos
數據卷容器
讓一個容器可以訪問另一個容器的數據卷
啟動兩個容器
啟動nfs容器,掛在一個卷,使用-d直接在后台執行 [root@linux-node2 ~]# docker run -d --name nfs -v /data centos 209bc89b365ad6bc1eeae693ada01c04c2d08e9ee2b8816e624882944c116126 啟動test1容器,掛載到nfs的數據卷容器上, [root@linux-node2 ~]# docker run -it --name test1 --volumes-from nfs centos [root@5e399198d6a8 /]# ls /data/ 查看沒內容
找到nfs容器的掛載點
(可以使用名稱,不僅僅是ID)
找到nfs容器的ID [root@linux-node2 opt]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 209bc89b365a centos "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago nfs 找到nfs容器的掛載點
[root@linux-node2 _data]# docker inspect nfs [root@linux-node2 opt]# cd /var/lib/docker/volumes/3938c9b1143d41340e148a4c7bc12d13b53966b15380c5b958a9e035897450d5/_data [root@linux-node2 _data]# touch cgt
在test1上查看
到test1上查看 [root@5e399198d6a8 /]# ls /data/ cgt
點睛:數據卷容器不論停止還是開啟,不影響其他容器掛載使用
如何制作鏡像
方式一:手動構建容器
1:創建一個容器mynginx,使用centos鏡像
[root@linux-node2 ~]# docker run --name mynginx -it centos [root@f9c7dfb6f552 /]# rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm [root@f9c7dfb6f552 /]# yum -y install nginx [root@f9c7dfb6f552 /]# exit exit
2:基於mynginx容器做一個鏡像mynginx:v1
[root@linux-node2 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f9c7dfb6f552 centos "/bin/bash" 3 minutes ago Exited (0) 15 seconds ago mynginx 基於mynginx這個容器做一個鏡像 [root@linux-node2 ~]# docker commit -m "my nginx" f9c7dfb6f552 cgt/mynginx:v1 3f3adc859b77b2b47c3631229761bee6c7066f1c708bc01c5173c2ef5c0adce8 提交鏡像,同時打一個標簽叫mynginx:v1,cgt相當於你向github上提交的用戶名 查看鏡像 [root@linux-node2 ~]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE cgt/mynginx v1 3f3adc859b77 About a minute ago 326.4 MB
3:基於mynginx:v1創建一個容器mynginxv1
目的是修改nginx不讓其在后台運行
[root@linux-node2 ~]# docker run -it --name nginxv1 cgt/mynginx:v1 [root@ea64c5855006 /]# vi /etc/nginx/nginx.conf daemon off; # 不再后台運行 [root@ea64c5855006 /]# exit exit [root@linux-node2 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ea64c5855006 cgt/mynginx:v1 "/bin/bash" 2 minutes ago Exited (0) 42 seconds ago nginxv1
4:基於mynginxv1提交mynginxv2版本
重新提交V2版本 [root@linux-node2 ~]# docker commit -m "my nginx" ea64c5855006 cgt/mynginx:v2 a480cdf9055ec4e640c65df6404c6ba42903ea77198a26cec75eef0e4965fe67 查看V2鏡像 [root@linux-node2 ~]# docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE cgt/mynginx v2 a480cdf9055e 25 seconds ago
5:基於mynginxv2鏡像,創建mynginxv2容器
啟動容器,-d后台運行,-p指定端口 在后面是鏡像,最后是命令(因為是yum安裝的,可以直接寫nginx,如果不是yum,那要寫絕對路徑) [root@linux-node2 ~]# docker run -d -p 82:80 cgt/mynginx:v2 nginx 4eaf8a19034a673100f9355504628fad45e6ecbab91615afd6cb4e7a18b82171 [root@linux-node2 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4eaf8a19034a cgt/mynginx:v2 "nginx" 15 seconds ago Up 14 seconds 0.0.0.0:82->80/tcp elegant_leakey 可以在瀏覽器訪問82端口
方式二:Dockerfile
1:Dockerfile包含的信息
基礎鏡像信息
維護者信息
鏡像操作指令
容器啟動時執行指令
2:文件的編寫
[root@linux-node2 ~]# mkdir /opt/dockerfile/nginx/ -p [root@linux-node2 ~]# cd /opt/dockerfile/nginx/ 將index.html上傳到此處 [root@linux-node2 nginx]# vim Dockerfile # This is docker file # version v1 # Author caoxiaojian # Base image(基礎鏡像) FROM centos # Maintainer(維護者信息) MAINTAINER caoxiaojian 1111@qq.com # Commands(執行命令) RUN rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm RUN yum -y install nginx # Add(添加文件) ADD index.html /usr/share/nginx/html/index.html # index.html是自己編寫的文件,放在后面的目錄中,因為yum安裝后Documentroot是在這里 RUN echo "daemon off;" >>/etc/nginx/nginx.conf EXPOSE 80 # 對外的端口 CMD ['nginx'] # 執行的命令
3:構建容器,並運行
建立newnginx容器,-t:標簽,執行/opt/dockerfile/nginx/下面的默認的Dockerfile文件 [root@linux-node2 nginx]# docker build -t cgt/mynginx:v3 /opt/dockerfile/nginx/ [root@linux-node2 nginx]# docker run -d -p 83:80 cgt/mynginx:v3
基情鏈接:
http://itgladiator.github.io/架構/2015/06/06/Docker學習.html