1. 程式人生 > >使用docker映象執行一個容器的操作…

使用docker映象執行一個容器的操作…

操作主機必須安裝和執行docker,以公司192.168.0.206伺服器為例

先可用docker images指令羅列所有docker映象

檢視有哪些docker映象可用

[[email protected] Dockerfiles]# docker images
REPOSITORY                       TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
dev-centos6-nginx-php5.6-redis   201602161823        7d9f7c151adf        40 hours ago        419.9 MB
dev-centos6-nginx-php5.6-redis   201602161753        930656246b34        40 hours ago        419.9 MB
dev-centos6-nginx-php5.6-redis   201602151417        fd5c52b3a9f7        2 days ago          425.2 MB
docker.io/mysql                  latest              596847483ae2        3 weeks ago         360.2 MB
dev-centos6-nginx-php5.6-redis   latest              136648699c60        4 weeks ago         416.2 MB
dev-centos6-nginx-php5.3-redis   latest              9b1345f095d0        7 weeks ago         446.6 MB
dev-centos7-nginx-php5.4-redis   latest              98aa3cd33449        8 weeks ago         382 MB
docker.pepszxy.com:5000/nginx    latest              813e3731b203        9 weeks ago         133.8 MB
nginx-php-redis                  php5.4              330efb9de8ba        10 weeks ago        454.3 MB
nginx-php                        latest              df3875b298c2        10 weeks ago        374.2 MB
docker.io/centos                 6                   3bbbf0aca359        4 months ago        190.6 MB
docker.io/centos                 7                   ce20c473cd8a        4 months ago        172.3 MB

通過docker映象的REPOSTORY:TAG的組合或IMAGE ID可唯一確定一個映象,一般使用REPOSTORY:TAG居多,IMAGE ID不太好記

使用docker run 指令基於映象啟動一個容器,docker run接受許多配置引數,可通過docker run help指令檢視幫助文件,通常使用的引數

解析如下:

-d 以daemon的形式執行docker 容器,而不是啟動後便停止,適合類似nginx,mysql,php等需長期提供服務的容器執行

-w 指定容器的工作目錄,類似使用者的家目錄,進入容器後會首先進入-w指定的目錄,通常不指定也沒關係

--name 指定容器名稱,不指定名稱,docker將使用任意字元代替

--restart 指定容器故障時的重啟策略,推薦always引數

-p 指定埠對映的方式,-p 8090:80代表將主機的8090埠對映至容器80埠

-v 指定資料卷對映,一般用於資料持久化儲存場景,比如程式碼目錄和資料目錄,-v 主機目錄:容器目錄表示將主機目錄掛載到容器目錄,容器

的實時資料操作將得到保留。刪除容器不會刪除資料

使用206dev-centos6-nginx-php5.6-redis:201602161823啟動一個容器

[[email protected] Dockerfiles]# docker run -d --name=test.dev.dtedu.com --restart=always -w /data/web -v /home/test.dev.dtedu.com:/data/web dev-centos6-nginx-php5.6-redis:201602161823
9449fc1a56696aba66925abe7c9ae37a9f6d97cb65f1e2162d8ba1b4e7caad3c

執行成功後,通過docker ps指令可查詢到啟動的容器

[[email protected] Dockerfiles]# docker ps -a
CONTAINER ID        IMAGE                                         COMMAND                  CREATED             STATUS              PORTS                           NAMES
9449fc1a5669        dev-centos6-nginx-php5.6-redis:201602161823   "supervisord -n"         52 seconds ago      Up 51 seconds       80/tcp                          test.dev.dtedu.com

有時候有更改容器名稱的需求,可通過docker rename指定來實現,比如可通過下列指令將test.dev.dtedu.com容器名修改為test2.dev.dtedu.com

[[email protected] Dockerfiles]# docker rename test.dev.dtedu.com test2.dev.dtedu.com
[[email protected] Dockerfiles]# docker ps -a
CONTAINER ID        IMAGE                                         COMMAND                  CREATED             STATUS              PORTS                           NAMES
9449fc1a5669        dev-centos6-nginx-php5.6-redis:201602161823   "supervisord -n"         3 minutes ago       Up 3 minutes        80/tcp                          test2.dev.dtedu.com