Docker 容器技術

前言:

以前感受Docker是一種小虛擬機,docker和KVM虛擬機之間有什麼區別、聯繫、能夠應用在什麼樣的生產環境? php

  

1、Docker是什麼以及和KVM的區別

1.docker概念?html

Docker是基於LXC容器技術使用Go開發的開源軟件,能夠打包代碼的運行環境+代碼,而且移植性強,能夠把代碼的運行環境+代碼打包成docker鏡像,處處運行java

 

2.docker的設計理念python

Docker的設計理念:構建(build)、運輸(ship)、運行(running)mysql

 

3.docker的架構nginx

docker是C/S 架構 docker server 是服務端,咱們執行的docker命令就是操做客戶端;git

[root@localhost zhanggen]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

-----------------------------------------------------------
[root@localhost zhanggen]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2019-03-20 15:19:16 CST; 6min ago
     Docs: https://docs.docker.com
 Main PID: 55180 (dockerd)
   Memory: 75.6M
   CGroup: /system.slice/docker.service
           └─55180 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
組成部分

 

 

4.論docker and kvmweb

Docker Server 掛了全部的docker容器都會掛掉 (kvm的管理軟件 和libvirtd徹底隔離)redis

kvm是在內核級別徹底虛擬化出來的虛擬機 虛擬機之間徹底隔離sql

docker 容器是在操做系統層面進行的資源的隔離

Docker和KVM均可以進行狀態的回滾

 

 

5.docker的優點和應用場景

簡化了系統配置、減小了代碼和系統環境間的依賴性(運行別人的代碼還要部署別人的運行環境)
代碼流水線管理 代碼開發---->代碼測試----》代碼到生產 一個docker容器搞定
減小開發部署系統環境工做,提升開發效率。
docker容器和虛擬機相比能夠快速得運行起來

面向產品:把本身的系統運行環境+代碼打包進docker容器,直接完產品交付;
面向開發:作爲一個python開發我不再用去裝什麼Python三、pip、Nginx了,我只關心個人代碼怎麼寫?
面向測試:測試的時候不用搭建多套系統環境了
面向運行:環境一致性,系統環境能夠回滾
面向架構:自動化擴容

 

 

6.docker安裝、配置、操做、

Centos7 yum安裝docker

 

{
"registry-mirrors": ["https://pee6w651.mirror.aliyuncs.com"]
}
換個阿里雲的docker源 vim /etc/docker/daemon.json

 

查看docker日誌

這個很關鍵,遇到報錯必定不要心浮氣躁,也不要直接去百度搜索報錯內容, 科學的作法是:看日誌-----》鎖定問題-----》解決問題

[root@localhost zhanggen]# journalctl -xe
Mar 20 14:28:31 localhost.localdomain polkitd[582]: Registered Authentication Agent for unix-process:55506:719734 (system bus name :1.510 [/
Mar 20 14:28:31 localhost.localdomain systemd[1]: Starting Docker Storage Setup...
-- Subject: Unit docker-storage-setup.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit docker-storage-setup.service has begun starting up.
Mar 20 14:28:31 localhost.localdomain container-storage-setup[55512]: INFO: Volume group backing root filesystem could not be determined
Mar 20 14:28:31 localhost.localdomain systemd[1]: Started Docker Storage Setup.
-- Subject: Unit docker-storage-setup.service has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit docker-storage-setup.service has finished starting up.
-- 
-- The start-up result is done.
Mar 20 14:28:31 localhost.localdomain systemd[1]: Starting Docker Application Container Engine...
-- Subject: Unit docker.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit docker.service has begun starting up.
Mar 20 14:28:31 localhost.localdomain dockerd-current[55557]: unable to configure the Docker daemon with file /etc/docker/daemon.json: inval
Mar 20 14:28:31 localhost.localdomain systemd[1]: docker.service: main process exited, code=exited, status=1/FAILURE
Mar 20 14:28:31 localhost.localdomain systemd[1]: Failed to start Docker Application Container Engine.
-- Subject: Unit docker.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- 
-- Unit docker.service has failed.
-- 
-- The result is failed.
Mar 20 14:28:31 localhost.localdomain systemd[1]: Unit docker.service entered failed state.
Mar 20 14:28:31 localhost.localdomain systemd[1]: docker.service failed.
Mar 20 14:28:31 localhost.localdomain polkitd[582]: Unregistered Authentication Agent for unix-process:55506:719734 (system bus name :1.510,
journalctl -xe 查看docker日誌

 

查看docker版本

[root@localhost zhanggen]# docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: 
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
docker version

 

7.docker的組件

docker的組件分爲:鏡像image、容器container 、倉庫(repository)

鏡像:全部的docker容器都是由鏡像啓動構建的

倉庫:集中存放鏡像,用戶同網絡訪問倉庫得到想要鏡像 DockerHub,就是GitHub同樣;

容器:就是由鏡像建立的實例(簡化版的Linux環境)

 

2、docker快速入門

從docker的三大組件開始快速入門docker;

1.docker鏡像

去docker Hub 上搜素鏡像

[root@localhost zhanggen]# docker search centos
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
centos                             The official build of CentOS.                   5261                [OK]                
ansible/centos7-ansible            Ansible on Centos7                              121                                     [OK]
jdeathe/centos-ssh                 CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x86…   109                                     [OK]
consol/centos-xfce-vnc             Centos container with "headless" VNC session…   83                                      [OK]
imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              52                                      [OK]
centos/mysql-57-centos7            MySQL 5.7 SQL database server                   49                                      
tutum/centos                       Simple CentOS docker image with SSH access      44                                      
gluster/gluster-centos             Official GlusterFS Image [ CentOS-7 +  Glust…   40                                      [OK]
openshift/base-centos7             A Centos7 derived base image for Source-To-I…   39                                      
centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relational …   37                                      
kinogmt/centos-ssh                 CentOS with SSH                                 26                                      [OK]
centos/httpd-24-centos7            Platform for running Apache httpd 2.4 or bui…   22                                      
openshift/jenkins-2-centos7        A Centos7 based Jenkins v2.x image for use w20                                      
centos/php-56-centos7              Platform for building and running PHP 5.6 ap…   20                                      
pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag names…   10                                      
openshift/wildfly-101-centos7      A Centos7 based WildFly v10.1 image for use …   6                                       
openshift/jenkins-1-centos7        DEPRECATED: A Centos7 based Jenkins v1.x ima…   4                                       
darksheer/centos                   Base Centos Image -- Updated hourly             3                                       [OK]
pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile t…   2                                       
pivotaldata/centos                 Base centos, freshened up a little with a Do…   2                                       
pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated wi…   1                                       
blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                                       [OK]
openshift/wildfly-81-centos7       A Centos7 based WildFly v8.1 image for use w1                                       
smartentry/centos                  centos with smartentry                          0                                       [OK]
jameseckersall/sonarr-centos       Sonarr on CentOS 7     
docker search centos

 去docker hub上下載鏡像

[root@localhost zhanggen]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
8ba884070f61: Pull complete 
Digest: sha256:8d487d68857f5bc9595793279b33d082b03713341ddec91054382641d14db861
Status: Downloaded newer image for centos:latest
[root@localhost zhanggen]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              9f38484d220f        6 days ago          2
docker pull centos

 docker鏡像保存爲文件(鏡像導出)

[root@localhost zhanggen]# docker save -o Centos.tart centos
[root@localhost zhanggen]# ls
Centos.tart  Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
docker save -o Centos.tar

文件還原爲docker鏡像(鏡像導入)

[root@localhost zhanggen]# docker load --input Centos.tar
Loaded image: centos:latest
[root@localhost zhanggen]# 
docker load --input Centos.tar

 刪除docker鏡像

[root@localhost zhanggen]# docker rmi -f centos
Untagged: centos:latest
Untagged: centos@sha256:8d487d68857f5bc9595793279b33d082b03713341ddec91054382641d14db861
[root@localhost zhanggen]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              9f38484d220f        6 days ago          202MB
[root@localhost zhanggen]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
Digest: sha256:8d487d68857f5bc9595793279b33d082b03713341ddec91054382641d14db861
Status: Downloaded newer image for centos:latest
[root@localhost zhanggen]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              9f38484d220f        6 days ago          202MB
[root@localhost zhanggen]#
docker rmi -f centos

 

2.docker容器

鏡像的管理就是這些,主要仍是本身能夠構建鏡像

[root@localhost zhanggen]# docker run centos /bin/echo 'hellow I wanna fuck you!!'
WARNING: IPv4 forwarding is disabled. Networking will not work.
hellow I wanna fuck you!!
[root@localhost zhanggen]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost zhanggen]# 
docker run centos /bin/echo 'hellow I wanna fuck you!!' 

PS:爲何 我運行了一個 echo 程序  docker容器就立刻中止了?

由於docker容器默認會在docker容器的前臺運行1個進程,這個進程結束docker,容器也隨之中止了;(也許這就是它的一次性設計理念吧。。)

是否是很納悶 爲何docker運行的進程退出,容器怎麼就退出了? 這就是docker的理念---不可變基礎設施理念!

docker就是給進程作隔離使用的,不一樣於虛擬機,虛擬機纔是給操做系統作隔離使用的;

docker只是個容器 不是虛擬機!

[root@261f12e2d6e4 /]# cat /proc/cpuinfo 
processor    : 0
vendor_id    : GenuineIntel
cpu family    : 6
model        : 78
model name    : Intel(R) Core(TM) i7-6500U CPU @ 2.50GHz
stepping    : 3
microcode    : 0x88
cpu MHz        : 2591.294
cache size    : 4096 KB
physical id    : 0
siblings    : 1
core id        : 0
cpu cores    : 1
apicid        : 0
initial apicid    : 0
fpu        : yes
fpu_exception    : yes
cpuid level    : 22
wp        : yes
flags        : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch arat fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec
bogomips    : 5184.39
clflush size    : 64
cache_alignment    : 64
address sizes    : 43 bits physical, 48 bits virtual
power management:
你進入容器千萬不要感受是進入了虛擬機

 

運行新的docker   --name (容器名稱) mydocker    -t (terminal 打開一個終端)    -i (std input 打開標準輸入 )  

[root@localhost zhanggen]# docker run  --name mydocker -t -i centos /bin/bash
WARNING: IPv4 forwarding is disabled. Networking will not work.
[root@261f12e2d6e4 /]# 
docker run --name mydocker -t -i centos /bin/bash

 

 啓動一個關閉的docker容器

[root@localhost zhanggen]# docker start mydocker
mydocker
[root@localhost zhanggen]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS               NAMES
261f12e2d6e4        centos              "/bin/bash"              About an hour ago   Up 7 seconds                                       mydocker
d0dd5f7a79e8        centos              "/bin/echo 'hellow I…"   About an hour ago   Exited (0) About an hour ago                       sharp_chandrasekhar
docker start mydocker

 

 進如正在運行的容器看看

[root@localhost zhanggen]# docker attach mydocker
[root@261f12e2d6e4 /]# 
docker attach mydocker

 

刪除正在中止的容器

[root@localhost zhanggen]# docker rm -f e627d59e818c 
e627d59e818c
[root@localhost zhanggen]# docker rm -f d0dd5f7a79e8 
d0dd5f7a79e8
[root@localhost zhanggen]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost zhanggen]# 
docker rm -f e627d59e818c
[root@localhost /]# docker rm $(docker ps -aq)
eecdba33cb61
b2da31fc0100
06afb1402c3e
[root@localhost /]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost /]# 
docker rm $(docker ps -aq) 一次性刪除全部

 

 

 

 

3.進入正在運行中docker容器的2種最佳方式

查看正在運行容器的進程

[root@localhost zhanggen]# docker inspect -f "{{.State.Pid}}" mydocker
64721
docker inspect -f "{{.State.Pid}}" mydocker

經過進程ID反覆進入docker容器

[root@localhost zhanggen]# nsenter -t 64721 -m -u -i -n -p
-bash: __vte_prompt_command: command not found
[root@261f12e2d6e4 /]# 
nsenter -t 64721 -m -u -i -n -p

爲何經過 nsenter -t 64721 -m -u -i -n -p 進入docker容器exit以後不退出?

其實就是多開了一個bash進程

[root@localhost zhanggen]# nsenter -t 64721 -m -u -i -n -p
-bash: __vte_prompt_command: command not found
[root@261f12e2d6e4 /]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 08:43 pts/0    00:00:00 /bin/bash
root         28      0  0 08:49 pts/0    00:00:00 -bash        #其實就是多開了1個bash進程
root         42     28  0 08:52 pts/0    00:00:00 ps -ef
-bash: __vte_prompt_command: command not found
[root@261f12e2d6e4 /]# exit
logout
[root@localhost zhanggen]# nsenter -t 64721 -m -u -i -p
-bash: __vte_prompt_command: command not found
[root@261f12e2d6e4 /]# 
nsenter -t 64721 -m -u -i -n -p

docker exec 在不進入docker容器前提下 執行1個命令(一個程序)

root@localhost zhanggen]# docker exec mydocker whoami
root
[root@localhost zhanggen]# docker exec mydocker pwd
/
[root@localhost zhanggen]# docker exec mydocker ls
anaconda-post.log
bin
dev
etc
docker exec mydocker whoami

 經過docker exec 打開bash程序之間進入docker容器

[root@localhost zhanggen]# docker exec -it mydocker /bin/bash
[root@261f12e2d6e4 /]# exit 
exit
[root@localhost zhanggen]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              9f38484d220f        6 days ago          202MB
[root@localhost zhanggen]# 
docker exec -it mydocker

 在宿主機後臺運行docker容器

[root@localhost zhanggen]# docker run --name my_nginx -d nginx
WARNING: IPv4 forwarding is disabled. Networking will not work.
3aafe4fe15248ec4cd526a1873b6fc1e50b9c87926f6504755ebd021f76f5d61
[root@localhost zhanggen]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
3aafe4fe1524        nginx               "nginx -g 'daemon of…"   4 seconds ago       Up 3 seconds        80/tcp              my_nginx
[root@localhost zhanggen]# 
後臺運行docker容器

 

2、docker的網絡訪問

As a senior Web application developer,how to access it  if I`ve I developed a  Web application in docker container. 

我在docker裏面寫了個Django程序,我怎麼訪問它的8001端口呢?這就須要給docker容器開通網絡訪問功能;

 docker安裝以後會自動建立一個 docker0的橋接網卡

[root@localhost zhanggen]# ifconfig 
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        inet6 fe80::42:b4ff:fe4c:7745  prefixlen 64  scopeid 0x20<link>
        ether 02:42:b4:4c:77:45  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3  bytes 258 (258.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 
docker0

docker的網絡就是經過docker0這個網橋進行 NAT 轉換到docker容器的

 

配置centos7開啓IPV4轉發並重啓網卡

net.ipv4.ip_forward=1
vim /usr/lib/sysctl.d/00-system.conf

 開啓隨機端口訪問

[root@localhost zhanggen]# vim /usr/lib/sysctl.d/00-system.conf
[root@localhost zhanggen]# docker run -d -P nginx
06afb1402c3eb177dbae8b72cd94be90c63713ce73a1bac7ab8f85ecdae50caa
[root@localhost zhanggen]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
06afb1402c3e        nginx               "nginx -g 'daemon of…"   11 seconds ago      Up 9 seconds        0.0.0.0:32769->80/tcp   zen_hellman
docker run -d -P nginx

指定端口進行訪問

[root@localhost zhanggen]# docker run --name mydocker -d -p 8001:80 nginx
b2da31fc010033f378e0930a2b77cec498ba6cbc1d288353562e3d0c6b23ae3c
[root@localhost zhanggen]# 
docker run --name mydocker -d -p 8001:80 nginx

 

 

3、docker的數據管理

說到docker的數據管理,首先得清楚docker鏡像的分層存儲結構?

docker鏡像是採用分層結構存儲的,Docker鏡像是隻讀、不可寫的,docker容器在docker鏡像之上是可寫的,用戶在docker容器的基礎上操做以後commit 修改爲只讀鏡像,蓋了一層再來一層....

 

As I mentioned docker 採用的是一種開箱  既用-->用完--->即仍 的一次性理念,那麼在docker運行中產生的一些日誌..如何持久化?有一下兩種方式;

 

 

1.數據卷

咱們把宿主機中的一個目錄mount 到docker容器的一個目錄裏面,docker容器運行中產生的數據寫得宿主機的目錄裏;

[root@localhost zhanggen]# docker run -d --name nginx-volume -v /data nginx
70d688ad2c40e4490d05da0fa8bdbc0cc342315f1bbc61e7a63d4422832512c5
docker run -d --name nginx-volume -v /data nginx

 若是你想知道docker容器的 /data目錄到底掛載到宿主機的哪一個目錄上了?

[root@localhost zhanggen]# docker run -d --name nginx-volume -v /data nginx
70d688ad2c40e4490d05da0fa8bdbc0cc342315f1bbc61e7a63d4422832512c5
[root@localhost zhanggen]# docker inspect -f {{.Mounts}} nginx-volume
[{volume 4fbd49850452bcca0a867f09776f716b137c5f13f5a8b8a3a5e84e26496f9fcd /var/lib/docker/volumes/4fbd49850452bcca0a867f09776f716b137c5f13f5a8b8a3a5e84e26496f9fcd/_data /data local  true }]
[root@localhost zhanggen]# ls
Centos.tart  Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
[root@localhost zhanggen]# cd /var/lib/docker/volumes/4fbd49850452bcca0a867f09776f716b137c5f13f5a8b8a3a5e84e26496f9fcd/_data 
[root@localhost _data]# pwd
/var/lib/docker/volumes/4fbd49850452bcca0a867f09776f716b137c5f13f5a8b8a3a5e84e26496f9fcd/_data
[root@localhost _data]# ls
[root@localhost _data]# 
docker run -d --name nginx-volume -v /data nginxdocker & inspect -f {{.Mounts}} nginx-volume
root@localhost _data]# docker inspect -f {{.Mounts}} nginx-volume
[{volume 4fbd49850452bcca0a867f09776f716b137c5f13f5a8b8a3a5e84e26496f9fcd /var/lib/docker/volumes/4fbd49850452bcca0a867f09776f716b137c5f13f5a8b8a3a5e84e26496f9fcd/_data /data local  true }]
[root@localhost _data]# pwd
/var/lib/docker/volumes/4fbd49850452bcca0a867f09776f716b137c5f13f5a8b8a3a5e84e26496f9fcd/_data
[root@localhost _data]# touch 草你媽王X聰
[root@localhost _data]# docker exec -it nginx-volume /bin/bash
root@70d688ad2c40:/# ls   
bin  boot  data  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@70d688ad2c40:/# cd d 
data/ dev/  
root@70d688ad2c40:/# cd data/
root@70d688ad2c40:/data# ls
FUCK_WANGSICHONGTAMA  ??????????????????
root@70d688ad2c40:/data# 
驗證一下docker容器裏面的/data目錄是否和宿主機的目錄關聯在一塊兒了

在生產環境咱們通常會本身建立一個目錄 在掛載到docker目錄裏

[root@localhost _data]# docker run -d --name nginx-volume -v  /data/docker/volume-nginx/:/data nginx
09fc60548e01040805d6de92ace28062055a91eb832129b075e7e2605c149b45
[root@localhost _data]# docker exec -it nginx-volume /bin/bash
root@09fc60548e01:/# ls
bin  boot  data  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@09fc60548e01:/# cd data/
root@09fc60548e01:/data# ls
root@09fc60548e01:/data# echo 'sss' > Martin.txt
root@09fc60548e01:/data# ls
Martin.txt
root@09fc60548e01:/data# cat Martin.txt 
sss
root@09fc60548e01:/data# exit
exit
[root@localhost _data]# pwd
/var/lib/docker/volumes/4fbd49850452bcca0a867f09776f716b137c5f13f5a8b8a3a5e84e26496f9fcd/_data
[root@localhost _data]# cd /data/docker/volume-nginx/
[root@localhost volume-nginx]# ls
Martin.txt
[root@localhost volume-nginx]# cat Martin.txt 
sss
[root@localhost volume-nginx]# 
docker run -d --name nginx-volume -v /data/docker/volume-nginx/:/data nginx

 

 2.數據卷容器

 在容器是否運行、關閉的狀態下,容器之間均可以共享容器卷數據;(NFS)

  使用--volumes-from 指定要和哪一個容器共享容器卷

[root@localhost volume-nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
09fc60548e01        nginx               "nginx -g 'daemon of…"   16 minutes ago      Up 16 minutes       80/tcp              nginx-volume
[root@localhost volume-nginx]# docker run -it --name volume-test --volumes-from nginx-volume centos /bin/bash
[root@c16407be0cf4 /]# ls data/
Martin.txt
[root@c16407be0cf4 /]# exit
exit
[root@localhost volume-nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
09fc60548e01        nginx               "nginx -g 'daemon of…"   18 minutes ago      Up 18 minutes       80/tcp              nginx-volume
[root@localhost volume-nginx]# docker exec -it nginx-volume /bin/bash
root@09fc60548e01:/# ls data/
Martin.txt
root@09fc60548e01:/# 
docker run -it --name volume-test --volumes-from nginx-volume centos /bin/bash

 生成環境:通常都會起 1個專門的容器掛載到宿主機的1個目錄上,之後其餘容器都 經過 --volumes-from 該容器   的形式來達到容器卷數據共享的效果;

 

 

4、docker鏡像的構建

docker鏡像是docker能夠傳播的介質,那麼如何製做本身的docker鏡像?分爲以下2種方式:

手動構建

本身在容器裏面作操做,操做完畢以後,commit成本身的鏡像,

Dockerfile構建

以文件的形式執行力裏面的命令進行快速構建,比較自動化化,相比容器較爲輕量也更易於傳播;

最後還會談談我在生產環境中使用dockerfile的經驗。

 

1.手動建立鏡像

docker run --name mynginx -it centos    #使用基礎鏡像 centos 運行1個名爲 mynginx 的容器
vim /etc/nginx/nginx.conf 進入docker容器,修改Nginx配置文件

exit 退出容器

docker ps -a                                 #查看剛剛exit的容器
docker commit -m 'MyNginx' cd35 zhanggen/mynginx:v1              #commit把中止的容器提交到本地分支
docker images                                                     #製做完成
docker run --name mynginxv1 -d -p 81:80 zhanggen/mynginx:v1 nginx #啓動

PS:麻蛋發現本身製做的docker鏡像啓動不起來咋辦?

查看容器啓動的報錯日誌----》從新進入容器-----》檢查操做,修改---》從新提交本地分支-----》從新啓動

docker ps -a                                   #查看啓動失敗的鏡像ID
docker logs container_id                                          #查看報錯日誌
docker run -it zhanggen/mynginx:v1 /bin/bash                      #從新運行 該鏡像進去修改
docker commit -m 'MyNginx' cd35 zhanggen/mynginx:v1               #修改完畢從新提交到本地分支
docker run --name mynginxv1 -d -p 81:80 zhanggen/mynginx:v1 nginx #從新啓動鏡像
自制鏡像沒法啓動攻略

 

2.Docker file構建

Docker file語法

OM centos
MAINTAINER Martin.Zhang
RUN rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
RUN yum install -y nginx && yum clean all
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
ADD index.html /usr/share/nginx/html/index.html
EXPOSE 80
CMD ["nginx"]
Dockerfile
<html>
<body>
<h1>ssssssssss!!!!</h1>
</body>
</html>
index.html
[root@localhost nginx]# pwd
/dockerfile/nginx
[root@localhost nginx]# ls
Dockerfile  index.html
[root@localhost nginx]# docker build -t mynginx:v2 .
Sending build context to Docker daemon  3.072kB
Step 1/8 : FROM centos
 ---> 9f38484d220f
Step 2/8 : MAINTAINER Martin.Zhang
 ---> Running in be6d86ffa2a9
Removing intermediate container be6d86ffa2a9
 ---> 0dc16c8dadcc
Step 3/8 : RUN rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
 ---> Running in 3ce6b480a0a2
warning: /var/tmp/rpm-tmp.qHH8Wh: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Retrieving http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
Preparing...                          ########################################
Updating / installing...
epel-release-7-11                     ########################################
Removing intermediate container 3ce6b480a0a2
 ---> 6b2b42a8e360
Step 4/8 : RUN yum install -y nginx && yum clean all
 ---> Running in 3e228aab6696
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * epel: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.12.2-2.el7 will be installed
--> Processing Dependency: nginx-all-modules = 1:1.12.2-2.el7 for package: 1:nginx-1.12.2-2.el7.x86_64
--> Processing Dependency: nginx-filesystem = 1:1.12.2-2.el7 for package: 1:nginx-1.12.2-2.el7.x86_64
--> Processing Dependency: nginx-filesystem for package: 1:nginx-1.12.2-2.el7.x86_64
--> Processing Dependency: openssl for package: 1:nginx-1.12.2-2.el7.x86_64
--> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.12.2-2.el7.x86_64
--> Running transaction check
---> Package gperftools-libs.x86_64 0:2.6.1-1.el7 will be installed
---> Package nginx-all-modules.noarch 1:1.12.2-2.el7 will be installed
--> Processing Dependency: nginx-mod-http-geoip = 1:1.12.2-2.el7 for package: 1:nginx-all-modules-1.12.2-2.el7.noarch
--> Processing Dependency: nginx-mod-http-image-filter = 1:1.12.2-2.el7 for package: 1:nginx-all-modules-1.12.2-2.el7.noarch
--> Processing Dependency: nginx-mod-http-perl = 1:1.12.2-2.el7 for package: 1:nginx-all-modules-1.12.2-2.el7.noarch
--> Processing Dependency: nginx-mod-http-xslt-filter = 1:1.12.2-2.el7 for package: 1:nginx-all-modules-1.12.2-2.el7.noarch
--> Processing Dependency: nginx-mod-mail = 1:1.12.2-2.el7 for package: 1:nginx-all-modules-1.12.2-2.el7.noarch
--> Processing Dependency: nginx-mod-stream = 1:1.12.2-2.el7 for package: 1:nginx-all-modules-1.12.2-2.el7.noarch
---> Package nginx-filesystem.noarch 1:1.12.2-2.el7 will be installed
---> Package openssl.x86_64 1:1.0.2k-16.el7_6.1 will be installed
--> Processing Dependency: openssl-libs(x86-64) = 1:1.0.2k-16.el7_6.1 for package: 1:openssl-1.0.2k-16.el7_6.1.x86_64
--> Processing Dependency: make for package: 1:openssl-1.0.2k-16.el7_6.1.x86_64
--> Running transaction check
---> Package make.x86_64 1:3.82-23.el7 will be installed
---> Package nginx-mod-http-geoip.x86_64 1:1.12.2-2.el7 will be installed
--> Processing Dependency: GeoIP for package: 1:nginx-mod-http-geoip-1.12.2-2.el7.x86_64
--> Processing Dependency: libGeoIP.so.1()(64bit) for package: 1:nginx-mod-http-geoip-1.12.2-2.el7.x86_64
---> Package nginx-mod-http-image-filter.x86_64 1:1.12.2-2.el7 will be installed
--> Processing Dependency: gd for package: 1:nginx-mod-http-image-filter-1.12.2-2.el7.x86_64
--> Processing Dependency: libgd.so.2()(64bit) for package: 1:nginx-mod-http-image-filter-1.12.2-2.el7.x86_64
---> Package nginx-mod-http-perl.x86_64 1:1.12.2-2.el7 will be installed
--> Processing Dependency: perl >= 5.006001 for package: 1:nginx-mod-http-perl-1.12.2-2.el7.x86_64
--> Processing Dependency: perl(:MODULE_COMPAT_5.16.3) for package: 1:nginx-mod-http-perl-1.12.2-2.el7.x86_64
--> Processing Dependency: perl(Exporter) for package: 1:nginx-mod-http-perl-1.12.2-2.el7.x86_64
--> Processing Dependency: perl(XSLoader) for package: 1:nginx-mod-http-perl-1.12.2-2.el7.x86_64
--> Processing Dependency: perl(constant) for package: 1:nginx-mod-http-perl-1.12.2-2.el7.x86_64
--> Processing Dependency: perl(strict) for package: 1:nginx-mod-http-perl-1.12.2-2.el7.x86_64
--> Processing Dependency: perl(warnings) for package: 1:nginx-mod-http-perl-1.12.2-2.el7.x86_64
--> Processing Dependency: libperl.so()(64bit) for package: 1:nginx-mod-http-perl-1.12.2-2.el7.x86_64
---> Package nginx-mod-http-xslt-filter.x86_64 1:1.12.2-2.el7 will be installed
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.11)(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.18)(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64
--> Processing Dependency: libexslt.so.0()(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64
--> Processing Dependency: libxslt.so.1()(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64
---> Package nginx-mod-mail.x86_64 1:1.12.2-2.el7 will be installed
---> Package nginx-mod-stream.x86_64 1:1.12.2-2.el7 will be installed
---> Package openssl-libs.x86_64 1:1.0.2k-16.el7 will be updated
---> Package openssl-libs.x86_64 1:1.0.2k-16.el7_6.1 will be an update
--> Running transaction check
---> Package GeoIP.x86_64 0:1.5.0-13.el7 will be installed
---> Package gd.x86_64 0:2.0.35-26.el7 will be installed
--> Processing Dependency: libpng15.so.15(PNG15_0)(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Processing Dependency: libjpeg.so.62(LIBJPEG_6.2)(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Processing Dependency: libpng15.so.15()(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Processing Dependency: libjpeg.so.62()(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Processing Dependency: libfreetype.so.6()(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Processing Dependency: libfontconfig.so.1()(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Processing Dependency: libXpm.so.4()(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Processing Dependency: libX11.so.6()(64bit) for package: gd-2.0.35-26.el7.x86_64
---> Package libxslt.x86_64 0:1.1.28-5.el7 will be installed
---> Package perl.x86_64 4:5.16.3-294.el7_6 will be installed
--> Processing Dependency: perl(Socket) >= 1.3 for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Scalar::Util) >= 1.10 for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl-macros for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(threads::shared) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(threads) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Time::Local) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Time::HiRes) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Storable) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Socket) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Scalar::Util) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Pod::Simple::XHTML) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Pod::Simple::Search) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Getopt::Long) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Filter::Util::Call) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(File::Temp) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(File::Spec::Unix) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(File::Spec::Functions) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(File::Spec) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(File::Path) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Cwd) for package: 4:perl-5.16.3-294.el7_6.x86_64
--> Processing Dependency: perl(Carp) for package: 4:perl-5.16.3-294.el7_6.x86_64
---> Package perl-Exporter.noarch 0:5.68-3.el7 will be installed
---> Package perl-constant.noarch 0:1.27-2.el7 will be installed
---> Package perl-libs.x86_64 4:5.16.3-294.el7_6 will be installed
--> Running transaction check
---> Package fontconfig.x86_64 0:2.13.0-4.3.el7 will be installed
--> Processing Dependency: fontpackages-filesystem for package: fontconfig-2.13.0-4.3.el7.x86_64
--> Processing Dependency: dejavu-sans-fonts for package: fontconfig-2.13.0-4.3.el7.x86_64
---> Package freetype.x86_64 0:2.8-12.el7_6.1 will be installed
---> Package libX11.x86_64 0:1.6.5-2.el7 will be installed
--> Processing Dependency: libX11-common >= 1.6.5-2.el7 for package: libX11-1.6.5-2.el7.x86_64
--> Processing Dependency: libxcb.so.1()(64bit) for package: libX11-1.6.5-2.el7.x86_64
---> Package libXpm.x86_64 0:3.5.12-1.el7 will be installed
---> Package libjpeg-turbo.x86_64 0:1.2.90-6.el7 will be installed
---> Package libpng.x86_64 2:1.5.13-7.el7_2 will be installed
---> Package perl-Carp.noarch 0:1.26-244.el7 will be installed
---> Package perl-File-Path.noarch 0:2.09-2.el7 will be installed
---> Package perl-File-Temp.noarch 0:0.23.01-3.el7 will be installed
---> Package perl-Filter.x86_64 0:1.49-3.el7 will be installed
---> Package perl-Getopt-Long.noarch 0:2.40-3.el7 will be installed
--> Processing Dependency: perl(Pod::Usage) >= 1.14 for package: perl-Getopt-Long-2.40-3.el7.noarch
--> Processing Dependency: perl(Text::ParseWords) for package: perl-Getopt-Long-2.40-3.el7.noarch
---> Package perl-PathTools.x86_64 0:3.40-5.el7 will be installed
---> Package perl-Pod-Simple.noarch 1:3.28-4.el7 will be installed
--> Processing Dependency: perl(Pod::Escapes) >= 1.04 for package: 1:perl-Pod-Simple-3.28-4.el7.noarch
--> Processing Dependency: perl(Encode) for package: 1:perl-Pod-Simple-3.28-4.el7.noarch
---> Package perl-Scalar-List-Utils.x86_64 0:1.27-248.el7 will be installed
---> Package perl-Socket.x86_64 0:2.010-4.el7 will be installed
---> Package perl-Storable.x86_64 0:2.45-3.el7 will be installed
---> Package perl-Time-HiRes.x86_64 4:1.9725-3.el7 will be installed
---> Package perl-Time-Local.noarch 0:1.2300-2.el7 will be installed
---> Package perl-macros.x86_64 4:5.16.3-294.el7_6 will be installed
---> Package perl-threads.x86_64 0:1.87-4.el7 will be installed
---> Package perl-threads-shared.x86_64 0:1.43-6.el7 will be installed
--> Running transaction check
---> Package dejavu-sans-fonts.noarch 0:2.33-6.el7 will be installed
--> Processing Dependency: dejavu-fonts-common = 2.33-6.el7 for package: dejavu-sans-fonts-2.33-6.el7.noarch
---> Package fontpackages-filesystem.noarch 0:1.44-8.el7 will be installed
---> Package libX11-common.noarch 0:1.6.5-2.el7 will be installed
---> Package libxcb.x86_64 0:1.13-1.el7 will be installed
--> Processing Dependency: libXau.so.6()(64bit) for package: libxcb-1.13-1.el7.x86_64
---> Package perl-Encode.x86_64 0:2.51-7.el7 will be installed
---> Package perl-Pod-Escapes.noarch 1:1.04-294.el7_6 will be installed
---> Package perl-Pod-Usage.noarch 0:1.63-3.el7 will be installed
--> Processing Dependency: perl(Pod::Text) >= 3.15 for package: perl-Pod-Usage-1.63-3.el7.noarch
--> Processing Dependency: perl-Pod-Perldoc for package: perl-Pod-Usage-1.63-3.el7.noarch
---> Package perl-Text-ParseWords.noarch 0:3.29-4.el7 will be installed
--> Running transaction check
---> Package dejavu-fonts-common.noarch 0:2.33-6.el7 will be installed
---> Package libXau.x86_64 0:1.0.8-2.1.el7 will be installed
---> Package perl-Pod-Perldoc.noarch 0:3.20-4.el7 will be installed
--> Processing Dependency: perl(parent) for package: perl-Pod-Perldoc-3.20-4.el7.noarch
--> Processing Dependency: perl(HTTP::Tiny) for package: perl-Pod-Perldoc-3.20-4.el7.noarch
--> Processing Dependency: groff-base for package: perl-Pod-Perldoc-3.20-4.el7.noarch
---> Package perl-podlators.noarch 0:2.5.1-3.el7 will be installed
--> Running transaction check
---> Package groff-base.x86_64 0:1.22.2-8.el7 will be installed
---> Package perl-HTTP-Tiny.noarch 0:0.033-3.el7 will be installed
---> Package perl-parent.noarch 1:0.225-244.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                       Arch     Version                 Repository
                                                                           Size
================================================================================
Installing:
 nginx                         x86_64   1:1.12.2-2.el7          epel      530 k
Installing for dependencies:
 GeoIP                         x86_64   1.5.0-13.el7            base      1.5 M
 dejavu-fonts-common           noarch   2.33-6.el7              base       64 k
 dejavu-sans-fonts             noarch   2.33-6.el7              base      1.4 M
 fontconfig                    x86_64   2.13.0-4.3.el7          base      254 k
 fontpackages-filesystem       noarch   1.44-8.el7              base      9.9 k
 freetype                      x86_64   2.8-12.el7_6.1          updates   380 k
 gd                            x86_64   2.0.35-26.el7           base      146 k
 gperftools-libs               x86_64   2.6.1-1.el7             base      272 k
 groff-base                    x86_64   1.22.2-8.el7            base      942 k
 libX11                        x86_64   1.6.5-2.el7             base      606 k
 libX11-common                 noarch   1.6.5-2.el7             base      164 k
 libXau                        x86_64   1.0.8-2.1.el7           base       29 k
 libXpm                        x86_64   3.5.12-1.el7            base       55 k
 libjpeg-turbo                 x86_64   1.2.90-6.el7            base      134 k
 libpng                        x86_64   2:1.5.13-7.el7_2        base      213 k
 libxcb                        x86_64   1.13-1.el7              base      214 k
 libxslt                       x86_64   1.1.28-5.el7            base      242 k
 make                          x86_64   1:3.82-23.el7           base      420 k
 nginx-all-modules             noarch   1:1.12.2-2.el7          epel       16 k
 nginx-filesystem              noarch   1:1.12.2-2.el7          epel       17 k
 nginx-mod-http-geoip          x86_64   1:1.12.2-2.el7          epel       23 k
 nginx-mod-http-image-filter   x86_64   1:1.12.2-2.el7          epel       26 k
 nginx-mod-http-perl           x86_64   1:1.12.2-2.el7          epel       36 k
 nginx-mod-http-xslt-filter    x86_64   1:1.12.2-2.el7          epel       26 k
 nginx-mod-mail                x86_64   1:1.12.2-2.el7          epel       54 k
 nginx-mod-stream              x86_64   1:1.12.2-2.el7          epel       76 k
 openssl                       x86_64   1:1.0.2k-16.el7_6.1     updates   493 k
 perl                          x86_64   4:5.16.3-294.el7_6      updates   8.0 M
 perl-Carp                     noarch   1.26-244.el7            base       19 k
 perl-Encode                   x86_64   2.51-7.el7              base      1.5 M
 perl-Exporter                 noarch   5.68-3.el7              base       28 k
 perl-File-Path                noarch   2.09-2.el7              base       26 k
 perl-File-Temp                noarch   0.23.01-3.el7           base       56 k
 perl-Filter                   x86_64   1.49-3.el7              base       76 k
 perl-Getopt-Long              noarch   2.40-3.el7              base       56 k
 perl-HTTP-Tiny                noarch   0.033-3.el7             base       38 k
 perl-PathTools                x86_64   3.40-5.el7              base       82 k
 perl-Pod-Escapes              noarch   1:1.04-294.el7_6        updates    51 k
 perl-Pod-Perldoc              noarch   3.20-4.el7              base       87 k
 perl-Pod-Simple               noarch   1:3.28-4.el7            base      216 k
 perl-Pod-Usage                noarch   1.63-3.el7              base       27 k
 perl-Scalar-List-Utils        x86_64   1.27-248.el7            base       36 k
 perl-Socket                   x86_64   2.010-4.el7             base       49 k
 perl-Storable                 x86_64   2.45-3.el7              base       77 k
 perl-Text-ParseWords          noarch   3.29-4.el7              base       14 k
 perl-Time-HiRes               x86_64   4:1.9725-3.el7          base       45 k
 perl-Time-Local               noarch   1.2300-2.el7            base       24 k
 perl-constant                 noarch   1.27-2.el7              base       19 k
 perl-libs                     x86_64   4:5.16.3-294.el7_6      updates   688 k
 perl-macros                   x86_64   4:5.16.3-294.el7_6      updates    44 k
 perl-parent                   noarch   1:0.225-244.el7         base       12 k
 perl-podlators                noarch   2.5.1-3.el7             base      112 k
 perl-threads                  x86_64   1.87-4.el7              base       49 k
 perl-threads-shared           x86_64   1.43-6.el7              base       39 k
Updating for dependencies:
 openssl-libs                  x86_64   1:1.0.2k-16.el7_6.1     updates   1.2 M

Transaction Summary
================================================================================
Install  1 Package  (+54 Dependent packages)
Upgrade             (  1 Dependent package)

Total download size: 21 M
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
warning: /var/cache/yum/x86_64/7/base/packages/fontpackages-filesystem-1.44-8.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for fontpackages-filesystem-1.44-8.el7.noarch.rpm is not installed
Public key for freetype-2.8-12.el7_6.1.x86_64.rpm is not installed
warning: /var/cache/yum/x86_64/7/epel/packages/nginx-all-modules-1.12.2-2.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Public key for nginx-all-modules-1.12.2-2.el7.noarch.rpm is not installed
--------------------------------------------------------------------------------
Total                                              608 kB/s |  21 MB  00:35     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-6.1810.2.el7.centos.x86_64 (@CentOS)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
 Userid     : "Fedora EPEL (7) <epel@fedoraproject.org>"
 Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 Package    : epel-release-7-11.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Updating   : 1:openssl-libs-1.0.2k-16.el7_6.1.x86_64                     1/57 
  Installing : fontpackages-filesystem-1.44-8.el7.noarch                   2/57 
  Installing : 2:libpng-1.5.13-7.el7_2.x86_64                              3/57 
  Installing : freetype-2.8-12.el7_6.1.x86_64                              4/57 
  Installing : dejavu-fonts-common-2.33-6.el7.noarch                       5/57 
  Installing : dejavu-sans-fonts-2.33-6.el7.noarch                         6/57 
  Installing : fontconfig-2.13.0-4.3.el7.x86_64                            7/57 
  Installing : 1:make-3.82-23.el7.x86_64                                   8/57 
  Installing : 1:openssl-1.0.2k-16.el7_6.1.x86_64                          9/57 
  Installing : libX11-common-1.6.5-2.el7.noarch                           10/57 
  Installing : gperftools-libs-2.6.1-1.el7.x86_64                         11/57 
  Installing : libXau-1.0.8-2.1.el7.x86_64                                12/57 
  Installing : libxcb-1.13-1.el7.x86_64                                   13/57 
  Installing : libX11-1.6.5-2.el7.x86_64                                  14/57 
  Installing : libXpm-3.5.12-1.el7.x86_64                                 15/57 
  Installing : libxslt-1.1.28-5.el7.x86_64                                16/57 
  Installing : 1:nginx-filesystem-1.12.2-2.el7.noarch                     17/57 
  Installing : GeoIP-1.5.0-13.el7.x86_64                                  18/57 
  Installing : libjpeg-turbo-1.2.90-6.el7.x86_64                          19/57 
  Installing : gd-2.0.35-26.el7.x86_64                                    20/57 
  Installing : groff-base-1.22.2-8.el7.x86_64                             21/57 
  Installing : 1:perl-parent-0.225-244.el7.noarch                         22/57 
  Installing : perl-HTTP-Tiny-0.033-3.el7.noarch                          23/57 
  Installing : perl-podlators-2.5.1-3.el7.noarch                          24/57 
  Installing : perl-Pod-Perldoc-3.20-4.el7.noarch                         25/57 
  Installing : perl-Encode-2.51-7.el7.x86_64                              26/57 
  Installing : perl-Text-ParseWords-3.29-4.el7.noarch                     27/57 
  Installing : 1:perl-Pod-Escapes-1.04-294.el7_6.noarch                   28/57 
  Installing : perl-Pod-Usage-1.63-3.el7.noarch                           29/57 
  Installing : 4:perl-libs-5.16.3-294.el7_6.x86_64                        30/57 
  Installing : 4:perl-macros-5.16.3-294.el7_6.x86_64                      31/57 
  Installing : 4:perl-Time-HiRes-1.9725-3.el7.x86_64                      32/57 
  Installing : perl-Exporter-5.68-3.el7.noarch                            33/57 
  Installing : perl-constant-1.27-2.el7.noarch                            34/57 
  Installing : perl-Time-Local-1.2300-2.el7.noarch                        35/57 
  Installing : perl-Carp-1.26-244.el7.noarch                              36/57 
  Installing : perl-Storable-2.45-3.el7.x86_64                            37/57 
  Installing : perl-PathTools-3.40-5.el7.x86_64                           38/57 
  Installing : perl-Scalar-List-Utils-1.27-248.el7.x86_64                 39/57 
  Installing : 1:perl-Pod-Simple-3.28-4.el7.noarch                        40/57 
  Installing : perl-File-Temp-0.23.01-3.el7.noarch                        41/57 
  Installing : perl-File-Path-2.09-2.el7.noarch                           42/57 
  Installing : perl-threads-shared-1.43-6.el7.x86_64                      43/57 
  Installing : perl-threads-1.87-4.el7.x86_64                             44/57 
  Installing : perl-Filter-1.49-3.el7.x86_64                              45/57 
  Installing : perl-Socket-2.010-4.el7.x86_64                             46/57 
  Installing : perl-Getopt-Long-2.40-3.el7.noarch                         47/57 
  Installing : 4:perl-5.16.3-294.el7_6.x86_64                             48/57 
  Installing : 1:nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64           49/57 
  Installing : 1:nginx-mod-http-geoip-1.12.2-2.el7.x86_64                 50/57 
  Installing : 1:nginx-mod-stream-1.12.2-2.el7.x86_64                     51/57 
  Installing : 1:nginx-mod-mail-1.12.2-2.el7.x86_64                       52/57 
  Installing : 1:nginx-mod-http-image-filter-1.12.2-2.el7.x86_64          53/57 
  Installing : 1:nginx-1.12.2-2.el7.x86_64                                54/57 
  Installing : 1:nginx-mod-http-perl-1.12.2-2.el7.x86_64                  55/57 
  Installing : 1:nginx-all-modules-1.12.2-2.el7.noarch                    56/57 
  Cleanup    : 1:openssl-libs-1.0.2k-16.el7.x86_64                        57/57 
  Verifying  : perl-HTTP-Tiny-0.033-3.el7.noarch                           1/57 
  Verifying  : fontconfig-2.13.0-4.3.el7.x86_64                            2/57 
  Verifying  : 1:nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64            3/57 
  Verifying  : perl-threads-shared-1.43-6.el7.x86_64                       4/57 
  Verifying  : 4:perl-Time-HiRes-1.9725-3.el7.x86_64                       5/57 
  Verifying  : 1:nginx-mod-http-geoip-1.12.2-2.el7.x86_64                  6/57 
  Verifying  : groff-base-1.22.2-8.el7.x86_64                              7/57 
  Verifying  : perl-Exporter-5.68-3.el7.noarch                             8/57 
  Verifying  : perl-constant-1.27-2.el7.noarch                             9/57 
  Verifying  : perl-PathTools-3.40-5.el7.x86_64                           10/57 
  Verifying  : 2:libpng-1.5.13-7.el7_2.x86_64                             11/57 
  Verifying  : dejavu-fonts-common-2.33-6.el7.noarch                      12/57 
  Verifying  : fontpackages-filesystem-1.44-8.el7.noarch                  13/57 
  Verifying  : 1:nginx-mod-stream-1.12.2-2.el7.x86_64                     14/57 
  Verifying  : libjpeg-turbo-1.2.90-6.el7.x86_64                          15/57 
  Verifying  : 1:perl-parent-0.225-244.el7.noarch                         16/57 
  Verifying  : GeoIP-1.5.0-13.el7.x86_64                                  17/57 
  Verifying  : 4:perl-libs-5.16.3-294.el7_6.x86_64                        18/57 
  Verifying  : perl-Encode-2.51-7.el7.x86_64                              19/57 
  Verifying  : perl-File-Temp-0.23.01-3.el7.noarch                        20/57 
  Verifying  : 1:perl-Pod-Simple-3.28-4.el7.noarch                        21/57 
  Verifying  : perl-Getopt-Long-2.40-3.el7.noarch                         22/57 
  Verifying  : 1:nginx-all-modules-1.12.2-2.el7.noarch                    23/57 
  Verifying  : libxcb-1.13-1.el7.x86_64                                   24/57 
  Verifying  : perl-Time-Local-1.2300-2.el7.noarch                        25/57 
  Verifying  : 4:perl-macros-5.16.3-294.el7_6.x86_64                      26/57 
  Verifying  : 4:perl-5.16.3-294.el7_6.x86_64                             27/57 
  Verifying  : libXpm-3.5.12-1.el7.x86_64                                 28/57 
  Verifying  : perl-Text-ParseWords-3.29-4.el7.noarch                     29/57 
  Verifying  : 1:openssl-1.0.2k-16.el7_6.1.x86_64                         30/57 
  Verifying  : 1:nginx-filesystem-1.12.2-2.el7.noarch                     31/57 
  Verifying  : perl-Carp-1.26-244.el7.noarch                              32/57 
  Verifying  : libxslt-1.1.28-5.el7.x86_64                                33/57 
  Verifying  : libX11-1.6.5-2.el7.x86_64                                  34/57 
  Verifying  : perl-Storable-2.45-3.el7.x86_64                            35/57 
  Verifying  : dejavu-sans-fonts-2.33-6.el7.noarch                        36/57 
  Verifying  : 1:nginx-mod-mail-1.12.2-2.el7.x86_64                       37/57 
  Verifying  : perl-Scalar-List-Utils-1.27-248.el7.x86_64                 38/57 
  Verifying  : gd-2.0.35-26.el7.x86_64                                    39/57 
  Verifying  : 1:perl-Pod-Escapes-1.04-294.el7_6.noarch                   40/57 
  Verifying  : 1:nginx-mod-http-image-filter-1.12.2-2.el7.x86_64          41/57 
  Verifying  : perl-Pod-Usage-1.63-3.el7.noarch                           42/57 
  Verifying  : 1:openssl-libs-1.0.2k-16.el7_6.1.x86_64                    43/57 
  Verifying  : 1:nginx-1.12.2-2.el7.x86_64                                44/57 
  Verifying  : perl-Pod-Perldoc-3.20-4.el7.noarch                         45/57 
  Verifying  : perl-podlators-2.5.1-3.el7.noarch                          46/57 
  Verifying  : libXau-1.0.8-2.1.el7.x86_64                                47/57 
  Verifying  : perl-File-Path-2.09-2.el7.noarch                           48/57 
  Verifying  : perl-threads-1.87-4.el7.x86_64                             49/57 
  Verifying  : gperftools-libs-2.6.1-1.el7.x86_64                         50/57 
  Verifying  : libX11-common-1.6.5-2.el7.noarch                           51/57 
  Verifying  : perl-Filter-1.49-3.el7.x86_64                              52/57 
  Verifying  : freetype-2.8-12.el7_6.1.x86_64                             53/57 
  Verifying  : 1:nginx-mod-http-perl-1.12.2-2.el7.x86_64                  54/57 
  Verifying  : perl-Socket-2.010-4.el7.x86_64                             55/57 
  Verifying  : 1:make-3.82-23.el7.x86_64                                  56/57 
  Verifying  : 1:openssl-libs-1.0.2k-16.el7.x86_64                        57/57 

Installed:
  nginx.x86_64 1:1.12.2-2.el7                                                   

Dependency Installed:
  GeoIP.x86_64 0:1.5.0-13.el7                                                   
  dejavu-fonts-common.noarch 0:2.33-6.el7                                       
  dejavu-sans-fonts.noarch 0:2.33-6.el7                                         
  fontconfig.x86_64 0:2.13.0-4.3.el7                                            
  fontpackages-filesystem.noarch 0:1.44-8.el7                                   
  freetype.x86_64 0:2.8-12.el7_6.1                                              
  gd.x86_64 0:2.0.35-26.el7                                                     
  gperftools-libs.x86_64 0:2.6.1-1.el7                                          
  groff-base.x86_64 0:1.22.2-8.el7                                              
  libX11.x86_64 0:1.6.5-2.el7                                                   
  libX11-common.noarch 0:1.6.5-2.el7                                            
  libXau.x86_64 0:1.0.8-2.1.el7                                                 
  libXpm.x86_64 0:3.5.12-1.el7                                                  
  libjpeg-turbo.x86_64 0:1.2.90-6.el7                                           
  libpng.x86_64 2:1.5.13-7.el7_2                                                
  libxcb.x86_64 0:1.13-1.el7                                                    
  libxslt.x86_64 0:1.1.28-5.el7                                                 
  make.x86_64 1:3.82-23.el7                                                     
  nginx-all-modules.noarch 1:1.12.2-2.el7                                       
  nginx-filesystem.noarch 1:1.12.2-2.el7                                        
  nginx-mod-http-geoip.x86_64 1:1.12.2-2.el7                                    
  nginx-mod-http-image-filter.x86_64 1:1.12.2-2.el7                             
  nginx-mod-http-perl.x86_64 1:1.12.2-2.el7                                     
  nginx-mod-http-xslt-filter.x86_64 1:1.12.2-2.el7                              
  nginx-mod-mail.x86_64 1:1.12.2-2.el7                                          
  nginx-mod-stream.x86_64 1:1.12.2-2.el7                                        
  openssl.x86_64 1:1.0.2k-16.el7_6.1                                            
  perl.x86_64 4:5.16.3-294.el7_6                                                
  perl-Carp.noarch 0:1.26-244.el7                                               
  perl-Encode.x86_64 0:2.51-7.el7                                               
  perl-Exporter.noarch 0:5.68-3.el7                                             
  perl-File-Path.noarch 0:2.09-2.el7                                            
  perl-File-Temp.noarch 0:0.23.01-3.el7                                         
  perl-Filter.x86_64 0:1.49-3.el7                                               
  perl-Getopt-Long.noarch 0:2.40-3.el7                                          
  perl-HTTP-Tiny.noarch 0:0.033-3.el7                                           
  perl-PathTools.x86_64 0:3.40-5.el7                                            
  perl-Pod-Escapes.noarch 1:1.04-294.el7_6                                      
  perl-Pod-Perldoc.noarch 0:3.20-4.el7                                          
  perl-Pod-Simple.noarch 1:3.28-4.el7                                           
  perl-Pod-Usage.noarch 0:1.63-3.el7                                            
  perl-Scalar-List-Utils.x86_64 0:1.27-248.el7                                  
  perl-Socket.x86_64 0:2.010-4.el7                                              
  perl-Storable.x86_64 0:2.45-3.el7                                             
  perl-Text-ParseWords.noarch 0:3.29-4.el7                                      
  perl-Time-HiRes.x86_64 4:1.9725-3.el7                                         
  perl-Time-Local.noarch 0:1.2300-2.el7                                         
  perl-constant.noarch 0:1.27-2.el7                                             
  perl-libs.x86_64 4:5.16.3-294.el7_6                                           
  perl-macros.x86_64 4:5.16.3-294.el7_6                                         
  perl-parent.noarch 1:0.225-244.el7                                            
  perl-podlators.noarch 0:2.5.1-3.el7                                           
  perl-threads.x86_64 0:1.87-4.el7                                              
  perl-threads-shared.x86_64 0:1.43-6.el7                                       

Dependency Updated:
  openssl-libs.x86_64 1:1.0.2k-16.el7_6.1                                       

Complete!
Loaded plugins: fastestmirror, ovl
Cleaning repos: base epel extras updates
Cleaning up list of fastest mirrors
Removing intermediate container 3e228aab6696
 ---> d31beb8cce1e
Step 5/8 : RUN echo "daemon off;" >> /etc/nginx/nginx.conf
 ---> Running in e2770ee72af0
Removing intermediate container e2770ee72af0
 ---> 56382e656884
Step 6/8 : ADD index.html /usr/share/nginx/html/index.html
 ---> 9a268246ca18
Step 7/8 : EXPOSE 80
 ---> Running in 92412b2bb046
Removing intermediate container 92412b2bb046
 ---> 05ce68ba1519
Step 8/8 : CMD ["nginx"]
 ---> Running in fa9037b49675
Removing intermediate container fa9037b49675
 ---> 5aee9f4c1ada
Successfully built 5aee9f4c1ada
Successfully tagged mynginx:v2
[root@localhost nginx]# docker image

Usage:    docker image COMMAND

Manage images

Commands:
  build       Build an image from a Dockerfile
  history     Show the history of an image
  import      Import the contents from a tarball to create a filesystem image
  inspect     Display detailed information on one or more images
  load        Load an image from a tar archive or STDIN
  ls          List images
  prune       Remove unused images
  pull        Pull an image or a repository from a registry
  push        Push an image or a repository to a registry
  rm          Remove one or more images
  save        Save one or more images to a tar archive (streamed to STDOUT by default)
  tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

Run 'docker image COMMAND --help' for more information on a command.
[root@localhost nginx]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mynginx             v2                  5aee9f4c1ada        14 seconds ago      307MB
centos              latest              9f38484d220f        10 days ago         202MB
nginx               latest              881bd08c0b08        2 weeks ago         109MB
[root@localhost nginx]# 
docker build -t mynginx:v2 .

 啓動鏡像

[root@localhost nginx]# cat index.html 
<html>
<body>
<h1>首頁!!!!</h1>
</body>
</html>
[root@localhost nginx]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mynginx             v2                  5aee9f4c1ada        4 minutes ago       307MB
centos              latest              9f38484d220f        10 days ago         202MB
nginx               latest              881bd08c0b08        2 weeks ago         109MB
[root@localhost nginx]# docker run --name mynginxv2 -d -p 81:80 mynginx:v2 nginx
8e269403c1b4181dd187eb20c74eb465f5f165ce0bfbc9b444d57164b1621e24
[root@localhost nginx]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                NAMES
8e269403c1b4        mynginx:v2          "nginx"             4 seconds ago       Up 3 seconds        0.0.0.0:81->80/tcp   mynginxv2
[root@localhost nginx]# ip a s
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    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
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:86:32:27 brd ff:ff:ff:ff:ff:ff
    inet 192.168.226.159/24 brd 192.168.226.255 scope global dynamic ens33
       valid_lft 1636sec preferred_lft 1636sec
    inet6 fe80::6f1c:2b48:af8b:a03a/64 scope link 
       valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
    link/ether 52:54:00:31:bf:a8 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
    link/ether 52:54:00:31:bf:a8 brd ff:ff:ff:ff:ff:ff
5: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:b4:4c:77:45 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:b4ff:fe4c:7745/64 scope link 
       valid_lft forever preferred_lft forever
79: veth3778ee4@if78: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP 
    link/ether d2:b8:51:47:0d:1d brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::d0b8:51ff:fe47:d1d/64 scope link 
       valid_lft forever preferred_lft forever
[root@localhost nginx]# curl 192.168.226.159:81
<html>
<body>
<h1>首頁!!!!</h1>
</body>
</html>
[root@localhost nginx]# 

 docker run --name mynginxv2 -d -p 81:80 mynginx:v2 nginx
docker run --name mynginxv2 -d -p 81:80 mynginx:v2 nginx

 

5、docker file生產實踐

docker file go live 

1.目錄環境準備

[root@localhost docker_image]# ls
apps  runtime  system
[root@localhost docker_image]# tree 
.
├── apps
│   └── work_order_api
├── runtime
│   ├── java
│   ├── php
│   └── python
└── system
    ├── centos
    └── centos-ssh

在架構設計裏面有個分層設計的理念,咱們也可把生產環境分爲:系統層、運行環境層、業務層,這也迎合了docker鏡像的分層,

Linux系統: centos、Ubuntu....

運行環境: Java 、Python 、PHP.....

App:API、WebAPP、BackGroundManagement ...

Docker file最大的亮點在於FROM,有了它就能夠基於不一樣的鏡像,衍生出多種鏡像,就和搭積木相似,分好層 能夠靈活組裝就好。

 

2.系統環境層docker file (centos7)

下載epel.repo源文件

[root@localhost centos]# pwd
/docker_image/system/centos
[root@localhost centos]# wget -O ./epel.repo  http://mirrors.aliyun.com/repo/epel-7.repo
--2019-03-26 09:34:20--  http://mirrors.aliyun.com/repo/epel-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 175.6.29.211, 175.6.29.218, 175.6.29.213, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|175.6.29.211|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 664 [application/octet-stream]
Saving to: ‘./epel.repo’

100%[==================================================================================================>] 664         --.-K/s   in 0s      

2019-03-26 09:34:20 (138 MB/s) - ‘./epel.repo’ saved [664/664]

[root@localhost centos]# ls
epel.repo
wget -O ./epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
FROM centos
MAINTAINER zhanggen
ADD epel.repo /etc/yum.repos.d/
run yum install -y wget mysql-devel supervisor git redis tree net-toos sudo psmisc && yum clean all
Dockerfile
[root@localhost centos]# ls
Dockerfile  epel.repo
[root@localhost centos]# docker build -t zhanggen/centos:v1 .
docker build -t zhanggen/centos:v1 .

 

3.開發環境層docker file (Python3.6.1)

[root@localhost python]# pwd
/docker_image/runtime/python
[root@localhost python]# ls
Dockerfile  Python-3.6.1.tgz
[root@localhost python]# 
FROM zhanggen/centos:v1

MAINTAINER Gen.Zhang

RUN yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel gcc automake autoconf libtool make db4-devel libpcap-devel xz-devel

RUN yum clean all

ADD Python-3.6.1.tgz /python_packages/

RUN mkdir -p /zhanggen/python3.6.1

RUN cd /python_packages/Python-3.6.1 && ./configure --prefix=/zhanggen/python3.6.1 && make && make install

RUN echo -e "PATH=$PATH:$HOME/bin:/zhanggen/python3.6.1/bin \n export PATH" > /etc/profile.d/python3.6.1.sh && source /etc/profile.d/python3.6.1.sh
Dockerfile

 構建python開發環境鏡像

[root@localhost runtime]# cd python/
[root@localhost python]# ls
Dockerfile  Python-3.6.1.tgz
[root@localhost python]# docker build -t zhanggen/python3.6.1:v1 .
Sending build context to Docker daemon  22.54MB
Step 1/8 : FROM zhanggen/centos:v1
 ---> 9c10a3b17a66
Step 2/8 : MAINTAINER Gen.Zhang
 ---> Using cache
 ---> 4c630636d1f9
Step 3/8 : RUN yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel gcc automake autoconf libtool make db4-devel libpcap-devel xz-devel
 ---> Using cache
 ---> ff12b7d7dbd3
Step 4/8 : RUN yum clean all
 ---> Using cache
 ---> 32c615445b0a
Step 5/8 : ADD Python-3.6.1.tgz /python_packages/
 ---> Using cache
 ---> ac70242e584b
Step 6/8 : RUN mkdir -p /zhanggen/python3.6.1
 ---> Using cache
 ---> 426627a6cb1f
Step 7/8 : RUN cd /python_packages/Python-3.6.1 && ./configure --prefix=/zhanggen/python3.6.1 && make && make install
 ---> Using cache
 ---> d15daa242a06
Step 8/8 : RUN echo -e "PATH=$PATH:$HOME/bin:/zhanggen/python3.6.1/bin \n export PATH" > /etc/profile.d/python3.6.1.sh && source /etc/profile.d/python3.6.1.sh
 ---> Using cache
 ---> 20e32f2fde08
Successfully built 20e32f2fde08
Successfully tagged zhanggen/python3.6.1:v1
[root@localhost python]# docker build -t zhanggen/python3.6.1:v1 .
Sending build context to Docker daemon  22.54MB
Step 1/8 : FROM zhanggen/centos:v1
 ---> 9c10a3b17a66
Step 2/8 : MAINTAINER Gen.Zhang
 ---> Using cache
 ---> 4c630636d1f9
Step 3/8 : RUN yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel gcc automake autoconf libtool make db4-devel libpcap-devel xz-devel
 ---> Using cache
 ---> ff12b7d7dbd3
Step 4/8 : RUN yum clean all
 ---> Using cache
 ---> 32c615445b0a
Step 5/8 : ADD Python-3.6.1.tgz /python_packages/
 ---> Using cache
 ---> ac70242e584b
Step 6/8 : RUN mkdir -p /zhanggen/python3.6.1
 ---> Using cache
 ---> 426627a6cb1f
Step 7/8 : RUN cd /python_packages/Python-3.6.1 && ./configure --prefix=/zhanggen/python3.6.1 && make && make install
 ---> Using cache
 ---> d15daa242a06
Step 8/8 : RUN echo -e "PATH=$PATH:$HOME/bin:/zhanggen/python3.6.1/bin \n export PATH" > /etc/profile.d/python3.6.1.sh && source /etc/profile.d/python3.6.1.sh
 ---> Using cache
 ---> 20e32f2fde08
Successfully built 20e32f2fde08
Successfully tagged zhanggen/python3.6.1:v1
[root@localhost python]# pwd
/docker_image/runtime/python
docker build -t zhanggen/python3.6.1:v1 .

 

四、APP層 docker file(Django web app)

Docker容器只容許在前臺 運行1個進程,咱們如何破解?

Supervisor是Python開發的進程管理工具,能夠同時開啓、重啓、關閉多個進程,能夠啓動1個Supervisor進程使用Supercisor啓動、管理N個進程; 

我發現Supervisor不只能夠統一管理進程,還能夠規範進程的日誌路徑,規範了日誌路徑以後,就能夠很方便得找得這些日誌,進行數據分析

[root@localhost web_application]# pwd
/docker_image/apps/web_application
[root@localhost web_application]# ls
django_app  Dockerfile  requirements.txt  supervisord.conf  supervisor.ini
[root@localhost web_application]# 

構建web app鏡像所須要的文件

APP項目---本身寫

ADD django_app/ /root/django_app/
ADD requirements.txt /root/django_app/

ADD supervisord.conf /etc/supervisord.conf
ADD supervisor.ini  /etc/supervisord.d/
RUN  /zhanggen/python3.6.1/bin/pip3 install -r /root/django_app/requirements.txt -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

EXPOSE 22 8008

CMD ["/usr/bin/supervisord","-c","/etc/supervisord.conf"]
Dockerfile
Django==1.11.4
requirements.txt APP項目須要的 python包
nodaemon=true               ; (start in foreground if true;default false)
supervisord的配置文件 supervisord.conf

配置supervisor須要啓動的啓動的 程序 

ps:必定記得讓supervisord進程 在前臺運行(修改配置中件  nodaemon=true)

[program:django_project]
command=/zhanggen/python3.6.1/bin/python3  /root/django_app/manage.py runserver 0.0.0.0:8008
process_name=%(program_name)s
autostart=true
user=root
stdout_logfile=/tmp/app.log
stderr_logfile=/tmp/app.error


[program:sshd]
command=/usr/sbin/sshd -D
process_name=%(program_name)s
autostart=true
user=root
stdout_logfile=/tmp/sshd.log
stderr_logfile=/tmp/sshd.error
supervisor.ini

 構建 web app鏡像

Successfully built 73fbd9076308
Successfully tagged zhanggen/django_app:v1
[root@localhost web_application]# pwd
/docker_image/apps/web_application
[root@localhost web_application]# docker build -t zhanggen/django_app:v1 .
docker build -t zhanggen/django_app:v1 .

啓動web app

[root@localhost web_application]# docker run --name my_django_app -d -p 8008:8008 -p 8022:22 zhanggen/django_app:v1 
185c311d6a951e33fa01d64a332127cf9ad6d45b0ee60d018ab25be68c0a86bf
[root@localhost web_application]# docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                                          NAMES
185c311d6a95        zhanggen/django_app:v1   "/usr/bin/supervisor…"   7 seconds ago       Up 5 seconds        0.0.0.0:8008->8008/tcp, 0.0.0.0:8022->22/tcp   my_django_app
[root@localhost web_application]# 
docker run --name my_django_app -d -p 8008:8008 -p 8022:22 zhanggen/django_app:v1

 最後的最後

[root@localhost web_application]# docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
zhanggen/django_app    v1                  a25dd5479dd9        8 minutes ago       905MB
zhanggen/python3.6.1   v1                  20e32f2fde08        19 hours ago        882MB
zhanggen/centos        v1                  9c10a3b17a66        20 hours ago        300MB
[root@localhost web_application]# docker run --name my_django_app -d -p 8008:8008 -p 8022:22 zhanggen/django_app:v1 
最終構建的3個鏡像

 

6、Docker倉庫(Docker Registry)

docker鏡像作好了,這些鏡像怎麼分享給其餘同事,其餘同事的鏡像 How to share with me?

因而私有Docker Hub-----Harbor閃亮登場了!

咱們能夠把這些鏡像 push  / pull 到Docker倉庫裏面,經過Docker倉庫 使鏡像 得以分享、傳播;

 

 

 1.Harbor私有倉庫搭建

 

2.Docker 添加--insecure-registry信任關係

ExecStart=/usr/bin/dockerd --insecure-registry=192.168.226.159:80
vim /usr/lib/systemd/system/docker.service

 

3.首次使用Harbor須要先登陸作一下認證 (Authenticating)

[root@localhost /]# docker login 192.168.226.159:80
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
docker login 192.168.226.159:80

 

4.在提交到Harbor以前 要先對本地鏡像進行 docker tag

[root@localhost /]# docker tag 192.168.226.159/zhanggen/django_app:v1 192.168.226.159:80/zhanggen/django_app:v1
[root@localhost /]# docker push 192.168.226.159:80/zhanggen/django_app:v1
The push refers to repository [192.168.226.159:80/zhanggen/django_app]
1adfc5932576: Pushed 
f35015fe99dc: Pushed 
130d88c10146: Pushed 
af1537967ae6: Pushed 
8bc7239a5750: Pushed 
4044728999b8: Pushed 
7393a01b80a0: Pushed 
8682a21900da: Pushed 
4b12cdc82a32: Pushed 
325cddbb1805: Pushed 
5e3f3498e333: Pushed 
9b0549f1a2e2: Pushed 
5d14ce4c4502: Pushed 
d7180aa9852e: Pushed 
d69483a6face: Mounted from zhanggen/mycentos 
v1: digest: sha256:9b4aaeb6d37466a92d3705b68b20c5bf27db1a21ef75456dd2433822b0bf5b60 size: 3457
docker tag 192.168.226.159/zhanggen/django_app:v1 192.168.226.159:80/zhanggen/django_app:v1

 

 5.docker push ---->Harbor倉庫

[root@localhost harbor]# docker login http://192.168.226.159:80
Username: admin
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@localhost harbor]# docker push 192.168.226.159:80/zhanggen/mycentos:v1  
The push refers to repository [192.168.226.159:80/zhanggen/mycentos]
d69483a6face: Pushed 
v1: digest: sha256:ca58fe458b8d94bc6e3072f1cfbd334855858e05e1fd633aa07cf7f82b048e66 size: 529
[root@localhost harbor]# 
docker push 192.168.226.159:80/zhanggen/mycentos:v1

 

6.docker pull  本地 <--------Harbor倉庫

[root@localhost /]# docker rmi 192.168.226.159:80/zhanggen/mycentos:v1
Untagged: 192.168.226.159:80/zhanggen/mycentos:v1
Untagged: 192.168.226.159:80/zhanggen/mycentos@sha256:ca58fe458b8d94bc6e3072f1cfbd334855858e05e1fd633aa07cf7f82b048e66
[root@localhost /]# docker pull 192.168.226.159:80/zhanggen/mycentos:v1
v1: Pulling from zhanggen/mycentos
Digest: sha256:ca58fe458b8d94bc6e3072f1cfbd334855858e05e1fd633aa07cf7f82b048e66
Status: Downloaded newer image for 192.168.226.159:80/zhanggen/mycentos:v1
[root@localhost /]# docker images
REPOSITORY                               TAG                 IMAGE ID            CREATED             SIZE
192.168.226.159/zhanggen/django_app      v1                  a25dd5479dd9        29 hours ago        905MB
192.168.226.159:80/zhanggen/django_app   v1                  a25dd5479dd9        29 hours ago        905MB
zhanggen/django_app                      v1                  a25dd5479dd9        29 hours ago        905MB
zhanggen/python3.6.1                     v1                  20e32f2fde08        2 days ago          882MB
zhanggen/centos                          v1                  9c10a3b17a66        2 days ago          300MB
192.168.226.159/zhanggen/mycentos        v1                  9f38484d220f        13 days ago         202MB
192.168.226.159:80/zhanggen/mycentos     v1                  9f38484d220f        13 days ago         202MB
centos                                   latest              9f38484d220f        13 days ago         202MB
goharbor/chartmuseum-photon              v0.8.1-v1.7.4       7e2272c02339        3 weeks ago         113MB
goharbor/harbor-migrator                 v1.7.4              968c31d07d2f        3 weeks ago         678MB
goharbor/redis-photon                    v1.7.4              611d1ead0a28        3 weeks ago         99.7MB
goharbor/clair-photon                    v2.0.7-v1.7.4       01090529ab14        3 weeks ago         165MB
goharbor/notary-server-photon            v0.6.1-v1.7.4       737518b1b943        3 weeks ago         135MB
goharbor/notary-signer-photon            v0.6.1-v1.7.4       495dc3326120        3 weeks ago         132MB
goharbor/harbor-registryctl              v1.7.4              723aed7bbf8d        3 weeks ago         102MB
goharbor/registry-photon                 v2.6.2-v1.7.4       f4743bd7b0d9        3 weeks ago         86.7MB
goharbor/nginx-photon                    v1.7.4              dda34e6afafe        3 weeks ago         35.9MB
goharbor/harbor-log                      v1.7.4              bf4916eef530        3 weeks ago         81.4MB
goharbor/harbor-jobservice               v1.7.4              1b6a0445ae9c        3 weeks ago         84.1MB
goharbor/harbor-core                     v1.7.4              e603b8750d26        3 weeks ago         95.6MB
goharbor/harbor-portal                   v1.7.4              2ca1d845cafa        3 weeks ago         40.6MB
goharbor/harbor-adminserver              v1.7.4              5706c65d65dc        3 weeks ago         72.3MB
goharbor/harbor-db                       v1.7.4              08d163f732f3        3 weeks ago         136MB
[root@localhost /]# 
docker pull 192.168.226.159:80/zhanggen/mycentos:v1

 

7.Harbor的 後臺管理頁面

默認登陸密碼:Harbor12345

 

 

七.docker實踐總結

雖然結果只是build了3個鏡像,可是我發現docker 的優點在於它能夠經過鏡像的方式 把Linux系統、開發環境、代碼 進行了分層, 咱們能夠把這些鏡像 想象成一個個的積木,有了這些小積木,咱們就能夠在 實體機、甚至虛擬機裏面  靈活、搭建出 各式各樣的房子,分給不一樣需求的人,並且構建速度 快,構建的房子還能夠輕易的搬到其餘地方;

docker是1種能夠 靈活、快速得 構建出 多種不一樣得 開發、測試 環境的容器技術,Buddy.... 若是你的公司須要構建大量、不一樣的系統環境用docker 吧!

若是你的代碼須要在不少地方快速運行起來用docker吧!

Build-----> Ship---->Run  

相關文章
相關標籤/搜索