Docker 要求 Ubuntu 系統的內核版本高於 3.10 ,查看本頁面的前提條件來驗證你的 Ubuntu 版本是否支持 Docker。php
經過 uname -r 命令查看你當前的內核版本html
經過 apt-get install docker.io 安裝dockerjava
-a stdin: 指定標準輸入輸出內容類型,可選 STDIN/STDOUT/STDERR 三項;mysql
-d: 後臺運行容器,並返回容器ID;nginx
-i: 以交互模式運行容器,一般與 -t 同時使用;git
-t: 爲容器從新分配一個僞輸入終端,一般與 -i 同時使用;github
--name="nginx-lb": 爲容器指定一個名稱;redis
-P: 容器的80端口映射到主機的隨機端口sql
-p: 容器的端口映射到主機的對應端口,例如: -p 80:80 docker
-v: 主機的目錄映射(掛載)到容器的目錄,例如:-v /home/ubuntu/nginx/www:/www
-m 或 --memory:設置內存使用限額。例如 -m 200M、--memory 300M;
--memory-swap:設置內存+swap的使用限額,當-m 200M --memory-swap=300M時,表示容器可使用200M內存和100Mswap;
--vm:啓動內存工做線程數。例如:--vm 1,啓動1個內存工做線程;
--vm-bytes 280M:每一個工做線程分配280M內存;
-c 或 --cpu-shares:設置容器使用cpu權重;
--dns 8.8.8.8: 指定容器使用的DNS服務器,默認和宿主一致;
--dns-search example.com: 指定容器DNS搜索域名,默認和宿主一致;
-h "mars": 指定容器的hostname;
-e username="ritchie": 設置環境變量;
--env-file=[]: 從指定文件讀入環境變量;
--cpuset="0-2" or --cpuset="0,1,2": 綁定容器到指定CPU運行;
-m :設置容器使用內存最大值;
--net="bridge": 指定容器的網絡鏈接類型,支持 bridge/host/none/container:<name|id> 四種類型;
--link=[]: 添加連接到另外一個容器;
--expose=[]: 開放一個端口或一組端口;
-a :顯示全部的容器,包括未運行的。例如:docker ps -a
-f :根據條件過濾顯示的內容。
--format :指定返回值的模板文件。
-l :顯示最近建立的容器。
-n : 列出最近建立的n個容器。例如:docker ps -n 5
--no-trunc :不截斷輸出。
-q :靜默模式,只顯示容器編號。
-s :顯示總的文件大小。
-f :指定返回值的模板文件。例如:docker inspect -f '{{.NetworkSettings.IPAddress}}' mycon (獲取正在運行的容器mycon的 IP)
-s :顯示總的文件大小。
--type :爲指定類型返回JSON。
-f : 跟蹤日誌輸出,例如:docker logs -f mycon(查看容器mycon的日誌輸出)
--since :顯示某個開始時間的全部日誌
-t : 顯示時間戳
--tail :僅列出最新N條容器日誌,例如:docker logs --since="2017-05-01" --tail=10 mycon(查看容器mycon從2017年5月1往後的最新10條日誌。)
-f :根據條件過濾事件;例如:docker events -f "image"="mysql:5.6" --since="1466302400" (顯示docker 鏡像爲mysql:5.6 這個時間戳對應的日期以後的相關事件。)
--since :從指定的時間戳後顯示全部事件;例如:docker events --since="1466302400" (顯示docker 在這個時間戳對應的日期以後的全部事件。)
--until :流水時間顯示到指定的時間爲止;
-L :保持源目標中的連接
docker cp /www/test mycon:/www/ #將主機/www/test目錄拷貝到容器mycon的/www目錄下
docker cp /www/test mycon:/www #將主機/www/test目錄拷貝到容器mycon中,目錄重命名爲www
docker cp mycon:/www /tmp/test #將容器mycon中的/www目錄拷貝到主機的/tmp/test目錄中
-o :將輸入內容寫到文件。例如:docker export -o mysql-`date +%Y%m%d`.tar a404c6c174a2 #將id爲a404c6c174a2的容器按日期保存爲tar文件。
-d :分離模式: 在後臺運行
-i :即便沒有附加也保持STDIN 打開
-t :分配一個僞終端
docker exec -i -t mycon /bin/bash #在容器mycon中開啓一個交互模式的終端
docker exec -it mycon /bin/sh /data/test.sh #在容器mycon中以交互模式執行容器內/data/test.sh腳本
-a :列出本地全部的鏡像(含中間映像層,默認狀況下,過濾掉中間映像層);
--digests :顯示鏡像的摘要信息;
-f :顯示知足條件的鏡像;
--format :指定返回值的模板文件;
--no-trunc :顯示完整的鏡像信息;
-q :只顯示鏡像ID。
-c :應用docker 指令建立鏡像;
-m :提交時的說明文字;
docker import my_ubuntu_v3.tar my/ubuntu:v4 #從鏡像歸檔文件my_ubuntu_v3.tar建立鏡像,命名爲my/ubuntu:v4
--build-arg=[] :設置鏡像建立時的變量;
--cpu-shares :設置 cpu 使用權重;
--cpu-period :限制 CPU CFS週期;
--cpu-quota :限制 CPU CFS配額;
--cpuset-cpus :指定使用的CPU id;
--cpuset-mems :指定使用的內存 id;
--disable-content-trust :忽略校驗,默認開啓;
-f :指定要使用的Dockerfile路徑;
--force-rm :設置鏡像過程當中刪除中間容器;
--isolation :使用容器隔離技術;
--label=[] :設置鏡像使用的元數據;
-m :設置內存最大值;
--memory-swap :設置Swap的最大值爲內存+swap,"-1"表示不限swap;
--no-cache :建立鏡像的過程不使用緩存;
--pull :嘗試去更新鏡像的新版本;
-q :安靜模式,成功後只輸出鏡像ID;
--rm :設置鏡像成功後刪除中間容器;
--shm-size :設置/dev/shm的大小,默認值是64M;
--ulimit :Ulimit配置。
docker build -t test/ubuntu:v1 #使用當前目錄的Dockerfile建立鏡像。
docker build github.com/creack/docker-firefox #使用URL github.com/creack/docker-firefox 的 Dockerfile 建立鏡像。
-a :提交的鏡像做者;
-c :使用Dockerfile指令來建立鏡像;
-m :提交時的說明文字;
-p :在commit時,將容器暫停。
docker commit -a "xst" -m "it is test" a404c6c174a2 mymysql:v1 #將容器a404c6c174a2 保存爲新的鏡像mymysql:v1,並添加提交人信息和說明信息。(當在容器內作了改變後能夠提交做爲本身定製的鏡像)
-f :強制刪除;
--no-prune :不移除該鏡像的過程鏡像,默認移除;
docker rmi -f test/ubuntu:16.04 #強制刪除本地鏡像test/ubuntu:16.04。
-o :輸出到的文件。
docker save -o ubuntu_16.tar test/ubuntu:v3 #將鏡像test/ubuntu:16.04 生成ubuntu_16.tar文檔
-H :以可讀的格式打印鏡像大小和日期,默認爲true;
--no-trunc :顯示完整的提交記錄;
-q :僅列出提交記錄ID。
docker history test/ubuntu:16.04 #查看本地鏡像test/ubuntu:16.04的建立歷史。
docker tag ubuntu:15.10 test/ubuntu:v15(將鏡像ubuntu:15.10標記爲 runoob/ubuntu:v15 鏡像。)
--automated :只列出 automated build類型的鏡像;
--no-trunc :顯示完整的鏡像描述;
-s :列出收藏數不小於指定值的鏡像。
docker search php:5.6-fpm #從Docker Hub查找鏡像名php5.6-fpm
docker search -s 10 java #從Docker Hub查找全部鏡像名包含java,而且收藏數大於10的鏡像
-a :拉取全部 tagged 鏡像
--disable-content-trust :忽略鏡像的校驗,默認開啓
docker pull php:5.6-fpm #從倉庫下載php5.6-fpm鏡像到本地
docker pull -a java #從倉庫下載REPOSITORY爲java的全部鏡像。
--disable-content-trust :忽略鏡像的校驗,默認開啓
docker push mycon:v1 #上傳本地鏡像mycon:v1到鏡像倉庫中。
獲取Container IP地址(Container狀態必須是Up)docker inspect id | grep IPAddress | cut -d '"' -f 4 #
docker inspect mycon | grep IPAddress #查看mycon容器的IP地址相關的字段
獲取端口映射docker inspect -f '{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' id #
獲取環境變量docker exec container_id env #
殺掉全部正在運行的容器docker kill $(docker ps -q) #
刪除老的(一週前建立)容器docker ps -a | grep 'weeks ago' | awk '{print $1}' | xargs docker rm #
刪除已經中止的容器docker rm `docker ps -a -q` #
刪除全部鏡像,當心docker rmi $(docker images -q) #
Dockerfile是docker構建鏡像的基礎,也是docker區別於其餘容器的重要特徵,正是有了Dockerfile,docker的自動化和可移植性才成爲可能。
不管是開發仍是運維,學會編寫Dockerfile幾乎是必備的,這有助於你理解整個容器的運行。
FROM ubuntu
MAINTAINER William <wlj@nicescale.com>
ENV TEST 1
RUN apt-get -y update RUN apt-get -y install nginx
ADD http://nicescale.com/ /data/nicescale.tgz
WORKDIR /var/www
USER nginx
VOLUME [‘/data’]
EXPOSE 80 443
ENTRYPOINT ["/usr/sbin/nginx"]
CMD ["start"]
docker建立、啓動container時執行的命令,若是設置了ENTRYPOINT,則CMD將做爲參數
docker build csphere/nginx:1.7 .
鏡像從Dockerfile build生成後,須要將鏡像推送(push)到鏡像倉庫。企業內部都須要構建一個私有docker registry,這個registry能夠看做二進制的scm,CI/CD也須要圍繞registry進行。
mkdir /registry docker run -p 80:5000 -e STORAGE_PATH=/registry -v /registry:/registry registry:2.0
假設192.168.1.2是registry倉庫的地址:
docker tag csphere/nginx:1.7 192.168.1.2/csphere/nginx:1.7 docker push 192.168.1.2/csphere/nginx:1.7
1.建立並拉取busybox
# docker run -it --name con01 busybox:latest / # ip addr #容器裏執行 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever Segmentation fault (core dumped) / # ping www.csphere.cn PING www.csphere.cn (117.121.26.243): 56 data bytes 64 bytes from 117.121.26.243: seq=0 ttl=48 time=3.139 ms 64 bytes from 117.121.26.243: seq=1 ttl=48 time=3.027 ms ^C --- www.csphere.cn ping statistics --- 2 packets transmitted, 2 packets received, 0% packet loss round-trip min/avg/max = 3.027/3.083/3.139 ms exit #退出容器
2.建立測試容器
docker run -d --name con03 csphere/test:0.1 efc9bda4a2ff2f479b18e0fc4698e42c47c9583a24c93f5ce6b28a828a172709
3.登錄到con03中
# docker exec -it con03 /bin/bash [root@efc9bda4a2ff /]# exit
4.中止con03
# docker stop con03 con03
5.開啓con03
# docker start con03 con03
6.刪除con03
# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES efc9bda4a2ff csphere/test:0.1 "/usr/local/bin/run 4 minutes ago Up 17 seconds con03 99aa6ee25adc busybox:latest "/bin/sh" 14 minutes ago Exited (0) 12 minutes ago con02 831c93de9b9f busybox:latest "/bin/sh" 2 hours ago Up 27 minutes con01 # docker rm con02 #容器中止的狀態 # docker rm -f con03 #容器開啓的狀態
1.從docker hub官方鏡像倉庫拉取鏡像
# docker pull busybox:latest atest: Pulling from busybox cf2616975b4a: Pull complete 6ce2e90b0bc7: Pull complete 8c2e06607696: Already exists busybox:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security. Digest: sha256:38a203e1986cf79639cfb9b2e1d6e773de84002feea2d4eb006b52004ee8502d Status: Downloaded newer image for busybox:latest
2.從本地上傳鏡像到鏡像倉庫
docker push 192.168.1.2/csphere/nginx:1.7
3.查找鏡像倉庫的某個鏡像
# docker search centos/nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED johnnyzheng/centos-nginx-php-wordpress 1 [OK] sergeyzh/centos6-nginx 1 [OK] hzhang/centos-nginx 1 [OK]
4.查看本地鏡像列表
# docker images TAG IMAGE ID CREATED VIRTUAL SIZE docker.io/csphere/csphere 0.10.3 604c03bf0c9e 3 days ago 62.72 MB docker.io/csphere/csphere latest 604c03bf0c9e 3 days ago 62.72 MB csphere/csphere 0.10.3 604c03bf0c9e 3 days ago 62.72 MB registry 2.0 2971b6ce766c 7 days ago 548.1 MB busybox latest 8c2e06607696 3 weeks ago 2.43 MB
5.刪除鏡像
docker rmi busybox:latest #沒有容器使用此鏡像建立,若是有容器在使用此鏡像會報錯:Error response from daemon: Conflict, cannot delete 8c2e06607696 because the running container 831c93de9b9f is using it, stop it and use -f to force FATA[0000] Error: failed to remove one or more images docker rmi -f busybox:latest #容器使用此鏡像建立,此容器狀態爲Exited
6.查看構建鏡像所用過的命令
# docker history busybox:latest IMAGE CREATED CREATED BY SIZE 8c2e06607696 3 weeks ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0 B 6ce2e90b0bc7 3 weeks ago /bin/sh -c #(nop) ADD file:8cf517d90fe79547c4 2.43 MB cf2616975b4a 3 weeks ago /bin/sh -c #(nop) MAINTAINER Jérôme Petazzo 0 B
補充:
sudo groupadd docker #添加一個新的docker用戶組
sudo gpasswd -a username docker #添加當前用戶到docker用戶組裏
sudo service docker restart #重啓Docker後臺監護進程
docker version #重啓以後,嘗試一下,是否生效
sudo reboot #若還未生效,則系統重啓,則生效
查看命令的詳細使用 例如:docker run --helpdocker cmd --help #
ps:假設讀者可以在ubuntu環境下搭建lnmp環境的狀況。
一、下載相應鏡像到本地
docker pull php:5.4-fpm
docker pull php:5.5-fpm
docker pull php:5.6-fpm
docker pull php:7.0-fpm
docker pull php:7.1-fpm
docker pull mysql:5.7
docker pull nginx:1.11
二、建立相應容器
#建立php:5.4-fpm的容器並命名php5.4,將容器的9000端口映射到主機的9000端口。把主機的/home/ubuntu/nginx/www目錄掛載到容器的/www目錄(這個目錄用於存放php腳本文件)
docker run -d -p 9000:9000 --name php5.4 -v /home/ubuntu/nginx/www:/www php:5.4-fpm
#在建立這個容器以前要先把前面建立的php5.4中止,否則9000端口會出現衝突。
docker run -d -p 9000:9000 --name php5.5 -v /home/ubuntu/nginx/www:/www php:5.5-fpm
#在建立這個容器以前要把前面建立的兩個都中止,不能同時運行多個php容器,要使用不一樣版本時,必須先stop正則運行的php容器,在start另外一個版本的php容器
docker run -d -p 9000:9000 --name php5.6 -v /home/ubuntu/nginx/www:/www php:5.6-fpm
#建立nginx容器,把配置主機的/home/ubuntu/nginx/conf目錄掛載到容器的/etc/nginx/conf.d目錄,用於處理nginx的配置文件,文件根目錄與PHP的指向同一個
docker run -d -p 80::80 --name nginx1.11 -v /home/ubuntu/nginx/conf:/etc/nginx/conf.d -v /home/ubuntu/nginx/www:/www nginx:1.11
#建立mysql容器,-e MYSQL_ROOT_PASSWORD=123456將mysql容器的root密碼初始化爲123456
docker run -d -p 3306:3306 --name mysql5.7 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
#建立infobright容器,與MySQL容器的建立方法相似
docker run -d -p 5029:5029 --name myinfobright -e MYSQL_ROOT_PASSWORD=root infobright:latest
#建立容器redis3.2, redis-server --apendonly yes 表示在容器執行redis-server啓動命令,並打開redis持久化配置
docker run -d -p 6379:6379 --name redis3.2 -v /home/ubuntu/redis/data:/data redis:3.2 redis-server --appendonly yes
docker run -d -p 11211:11211 --name memcached memcached:latest #建立memcached容器。
三、相關配置
而後在/home/ubuntu/nginx/conf目錄下新建文件default.conf文件,配置nginx虛擬機:
server { listen 80; root /www; index index.php index.html index.htm index.nginx-debian.html; server_name test.com; location / { index index.php index.html index.htm; if (!-e $request_filename) { rewrite ^/(.*)$ /index.php?$1 last; break; } } location ~ \.php$ { fastcgi_pass 172.17.0.3:9000; #這裏的ip是對應PHP版本的容器的ip地址,查看容器ip地址的方法前面有介紹 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
注意:安裝php的容器的時候,php的鏡像裏應該都是沒有相關擴展的。須要本身安裝相關擴展,例如gd,mysql,mysqli等等。
以上面建立的php5.6爲例:
首先進入容器:docker exec -it php5.6 /bin/bash
而後就能夠安裝相關擴展:docker-php-ext-install mysqli (mysql,gd,curl,...)安裝完擴展後可使用docker commit提交到鏡像成爲本身定製的docker鏡像。
安裝鏡像中非自帶的php擴展:例如 php-redis 和 php-memcached 擴展:
php-redis擴展下載地址:https://github.com/phpredis/phpredis (選擇適合的版本下載)
php-memcached下載地址:https://github.com/php-memcached-dev/php-memcached (選擇適合版本下載)
能夠在php容器內使用wget下載,也能夠在ubuntu中下載後拷貝到容器中,我這裏ubuntu中已經下載過這兩個擴展,就直接用docker cp 將這兩個擴展的源文件拷貝到php的容器中。
若是在容器中下載不要下載.zip類型,由於容器中沒有unzip命令。能夠在ubuntu中解壓後將目錄拷貝進容器。
docker cp /data/phpredis3.1.2 php5.6:/usr/src/php/ext/redis #將ubuntu中的/data/phpredis3.1.2拷貝到容器的/usr/src/php/ext/目錄下並重命名爲redis 。
docker-php-ext-install redis #安裝 php-redis 擴展
安裝memcached也是同理。。。
而後重啓該容器便可。
到這裏,環境已經初步搭建完成,訪問主機的IP,能夠查看運行狀況,打印phpinfo查看擴展安裝狀況。參考文章地址:https://blog.csphere.cn/archives/22