Docker掃盲之容器與鏡像的基本使用

環境

OS:CentOS Linux release 7.6.1810 (Core)php

Docker版本:Docker version 1.13.1, build 07f3374/1.13.1mysql

參考文獻

docs.docker-cn.com/get-started…linux

介紹

先提三個問,docker是什麼?docker能幫咱們解決什麼問題?爲何要使用docker?本文將快速帶領你走進容器的世界。程序員

  1. docker是什麼? 官方解釋:Docker 是一個開源的應用容器引擎,讓開發者能夠打包他們的應用以及依賴包到一個可移植的容器中,而後發佈到任何流行的 Linux 機器上,也能夠實現虛擬化。容器是徹底使用沙箱機制,相互之間不會有任何接口sql

    我的大白話:或許你如今尚未用過docker,可是你可能用過虛擬機吧?docker和虛擬機的差異在哪兒呢?首先第一個,虛擬機給人的第一映像就是大和重,想要部署一個環境到虛擬機上面,就必須安裝一個操做系統才能作相關的操做,其次就是你無論安裝什麼你都得安裝一個操做系統才能運行。可是如今docker的出現就解決了這個問題,爲何docker就解決了這個問題呢?其實docker就是將這個軟件所須要的操做系統或者相關環境給精簡了,去除了全部你用不到的東西,只留下軟件最基本的依賴,這些依賴會一層依賴一層,這就會讓一個docker的鏡像很是小,比起整個操做系統輕便了很是多,並且最主要的是能夠將你的環境打包成一個鏡像發佈到dockerhub等地方,而後你就在各個流行的linux上面直接將鏡像下載下來就能夠直接使用了,能夠說就是一次部署,處處使用。docker

  2. docker能幫咱們解決什麼問題? 衆所周知,每次咱們的項目代碼編寫完成事後,會將代碼發佈到服務器上,可是服務器必需要安裝一系列咱們須要的工具或者依賴包,亦或者是一些依賴的軟件,這個時候發佈項目的時間成本就過高了,若是碰到不熟悉的工做人員,這個時間可能會拉的更長,可是有了docker就不同了,咱們只須要簡單的會docker,會一些基礎的image和container相關的命令,就能完成這項工做,並且在不少太服務器的時候,也只須要一個docker就能幫咱們完成部署。ubuntu

  3. 爲何要使用docker? 可能你在想我都有一個Linux系統了還要docker來幹嗎?可是你有沒有想過當你係統的環境須要遷移的時候,好比說大家須要集羣,或者須要搭建測試服務器這些等等等等,你就須要花大量的時間來搭建環境,而後下載一些相應的包,可是這中間頗有可能出現各類各樣的坑,這些都是沒法避免的,可是docker就是一個容器,它無論在那個環境上面運行,它都依賴於本身的容器,而不會去依賴裝載容器的系統,這也就避免了由於系統的緣由致使安裝的步驟都不同。centos

docker hello world

仍是以程序員的靈魂hello world來做爲例子,而後快速進入docker的世界,可使用docker run 鏡像名來啓動一個鏡像,當這個鏡像在本地不存在時,那麼docker會到docker倉庫去獲取這個鏡像。 執行命令bash

docker run hello-world
複製代碼

響應結果服務器

[root@localhost ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
Trying to pull repository docker.io/library/hello-world ... 
latest: Pulling from docker.io/library/hello-world
1b930d010525: Pull complete 
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Status: Downloaded newer image for docker.io/hello-world:latest
WARNING: IPv4 forwarding is disabled. Networking will not work.

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

[root@localhost ~]#
複製代碼

docker的基本使用

查看鏡像

上面咱們已經運行了一個hello-word,那麼咱們來看一下執行了hello-world咱們會生成那些東西?首先第一個是鏡像,也是最重要的一個。執行如下命令能夠查看容器中的全部鏡像 執行命令

docker images
複製代碼

響應結果

[root@localhost ~]# docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world               latest              fce289e99eb9        2 months ago        1.84 kB
[root@localhost ~]# 
複製代碼

從上面的響應信息能夠看到咱們已經有了一個鏡像 docker.io/hello-world,分析一下這個鏡像的字段吧

  1. REPOSITORY:這個是鏡像所在的倉庫,能夠理解一種類型的數據,好比牙膏。
  2. TAG:倉庫的標籤,這裏的latest是對倉庫裏面的類型進行標記,能夠理解成是牙膏裏面的中華牙膏這個標記(其實就是版本),當沒有指定標籤時,那麼這個latest就是默認的標籤,一個倉庫能夠打多個標籤,每一個標籤和倉庫組成一組你想要的數據,當你須要獲取指定標籤的時候只須要這樣寫docker run hello-world:latest
  3. IMAGE ID:這個鏡像的id,也是惟一的id,好比你每次打個標籤它的值都會變,咱們啓動的時候也可使用docker run fce289e99eb9 來啓動。
  4. CREATED:這裏是表明這個鏡像的建立時間距離如今已通過去了多久。
  5. SIZE:顧名思義,這就是這個鏡像的大小。

查看容器

這裏咱們來看下由hello-world生成的容器是什麼樣的 執行命令

docker ps  -a
複製代碼

響應結果

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS                      PORTS               NAMES
e91d9159c248        hello-world                     "/hello"                 2 hours ago         Exited (0) 2 hours ago                          adoring_mestorf
[root@localhost ~]# 
複製代碼

分析字段

  1. CONTAINER ID:容器id
  2. IMAGE:容器所屬的鏡像
  3. COMMAND:啓動時的默認執行命令,這裏的命令時hello的緣由時由於鏡像的基礎鏡像在生成的時候指定的,由於咱們時從服務器上面拿下來的。
  4. CREATED:容器建立的時間距離下限的時間
  5. STATUS:這個容器如今的狀態,能夠看到容器如今已經時退出了
  6. PORTS:這個容器啓動的時候映射在本機上面的端口,因爲咱們沒有設置就沒有值,能夠經過 -p 宿主機端口:容器端口 來設置
  7. NAMES:容器的名字,能夠經過--name 容器名 來設置

啓動容器

咱們能夠經過docker start 容器id 來啓動一個容器 執行命令:

docker start 你的容器id
複製代碼

可是能夠看到咱們的容器時什麼都沒有輸出,這是爲何呢?剛開始不是輸出來hello world嗎?由於在docker裏面容器啓動的時候只會輸出啓動的id,想要看詳細信息只有進入容器查看,可使用docker ps命令來查看運行的容器, 可是咱們執行docker ps爲何也沒有任何信息?這是由於咱們沒有後臺運行,必需要在運行(執行run的時候)容器的時候加上-d 參數。

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost ~]# 
複製代碼

搜索鏡像

因爲咱們的hello-world的例子並不支持後臺,咱們能夠從新獲取一個鏡像來執行,咱們能夠先執行docker search 鏡像名來查找你想要的鏡像, 咱們這裏來下載一個Centos的鏡像 執行命令

docker search centos
複製代碼

響應結果

[root@localhost ~]# docker search centos
INDEX       NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/centos                             The official build of CentOS.                   5261      [OK]       
docker.io   docker.io/ansible/centos7-ansible            Ansible on Centos7                              121                  [OK]
docker.io   docker.io/jdeathe/centos-ssh                 CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x...   109                  [OK]
docker.io   docker.io/consol/centos-xfce-vnc             Centos container with "headless" VNC sessi...   83                   [OK]
docker.io   docker.io/imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              52                   [OK]
docker.io   docker.io/centos/mysql-57-centos7            MySQL 5.7 SQL database server                   49                   
docker.io   docker.io/tutum/centos                       Simple CentOS docker image with SSH access      44                   
docker.io   docker.io/gluster/gluster-centos             Official GlusterFS Image [ CentOS-7 +  Glu...   40                   [OK]
docker.io   docker.io/openshift/base-centos7             A Centos7 derived base image for Source-To...   39                   
docker.io   docker.io/centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relationa...   37                   
docker.io   docker.io/kinogmt/centos-ssh                 CentOS with SSH                                 26                   [OK]
docker.io   docker.io/centos/httpd-24-centos7            Platform for running Apache httpd 2.4 or b...   22                   
docker.io   docker.io/centos/php-56-centos7              Platform for building and running PHP 5.6 ...   20                   
docker.io   docker.io/openshift/jenkins-2-centos7        A Centos7 based Jenkins v2.x image for use...   20                   
docker.io   docker.io/pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag nam...   10                   
docker.io   docker.io/openshift/wildfly-101-centos7      A Centos7 based WildFly v10.1 image for us...   6                    
docker.io   docker.io/openshift/jenkins-1-centos7        DEPRECATED: A Centos7 based Jenkins v1.x i...   4                    
docker.io   docker.io/darksheer/centos                   Base Centos Image -- Updated hourly             3                    [OK]
docker.io   docker.io/pivotaldata/centos                 Base centos, freshened up a little with a ...   2                    
docker.io   docker.io/pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile...   2                    
docker.io   docker.io/blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                    [OK]
docker.io   docker.io/openshift/wildfly-81-centos7       A Centos7 based WildFly v8.1 image for use...   1                    
docker.io   docker.io/pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated ...   1                    
docker.io   docker.io/jameseckersall/sonarr-centos       Sonarr on CentOS 7                              0                    [OK]
docker.io   docker.io/smartentry/centos                  centos with smartentry                          0                    [OK]
[root@localhost ~]#
複製代碼
  1. INDEX:這個鏡像的索引
  2. NAME:鏡像名稱
  3. DESCRIPTION:鏡像描述
  4. STARS:這個鏡像有多少我的在使用,能夠更具這個數量來選取本身須要的鏡像
  5. OFFICIAL:是不是官方發佈的鏡像
  6. AUTOMATED:是否支持自動化

獲取鏡像

上面咱們已經使用docker search centos來搜索到了不少的驚醒,如今咱們來使用docker pull 鏡像名來獲取鏡像吧 執行命令

docker pull docker.io/centos
複製代碼

響應結果

[root@localhost ~]# docker pull docker.io/centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ... 
latest: Pulling from docker.io/library/centos
8ba884070f61: Pull complete 
Digest: sha256:8d487d68857f5bc9595793279b33d082b03713341ddec91054382641d14db861
Status: Downloaded newer image for docker.io/centos:latest
[root@localhost ~]#
複製代碼

能夠從響應結果來看到咱們的驚喜那個已經成功的獲取下來了,執行docker images來查看咱們的鏡像 執行命令

docker images
複製代碼

響應結果

[root@localhost ~]# docker images
REPOSITORY                          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos                    latest              9f38484d220f        6 days ago          202 MB
docker.io/hello-world               latest              fce289e99eb9        2 months ago        1.84 kB
[root@localhost ~]#
複製代碼

從上面的響應結果咱們能夠看到咱們多了一個centos的鏡像,如今來啓動這個鏡像吧,使用docker run 鏡像名來啓動鏡像 執行命令

docker run -d -ti --name docker.io/centos /bin/bash
複製代碼

參數描述

  1. -d:讓啓動的這個容器之後臺方式執行
  2. -ti:可讓容器接受和控制檯交互還有能夠在控制檯輸出信息
  3. /bin/bash:讓容器啓動時執行該命令

帶上參數的緣由是Docker中系統鏡像的缺省命令是 bash,若是不加 -ti bash 命令執行了自動會退出。這是由於若是沒有銜接輸入流,自己就會立刻結束。加-ti 後docker命令會爲容器分配一個僞終端,並接管其stdin/stdout支持交互操做,這時候bash命令不會自動退出。

響應結果

[root@localhost ~]# docker run -d -ti docker.io/centos /bin/bash
9687356aaf8e8b7ce24be22af28c6d6b1d9bc130c34e0211f6da05fc7a244d2b
複製代碼

如今容器已經啓動起來了,咱們能夠上面學習的docker ps -a命令來查看全部的容器 執行命令

docker ps -a
複製代碼

響應結果

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS                      PORTS               NAMES
9687356aaf8e        docker.io/centos                "/bin/bash"              2 minutes ago       Up 2 minutes                                    youthful_mccarthy
e91d9159c248        hello-world                     "/hello"                 2 hours ago         Exited (0) 36 minutes ago                       adoring_mestorf
[root@localhost ~]#
複製代碼

從上面能夠看到已經有兩個運行中的容器了,因爲咱們剛纔啓動容器的時候使用來一些額外的參數,如今咱們在來看是否有一個運行中的容器。 執行命令

docker ps
複製代碼

響應結果

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
9687356aaf8e        docker.io/centos    "/bin/bash"         5 minutes ago       Up 5 minutes                            youthful_mccarthy
[root@localhost ~]# 
複製代碼

能夠從響應結果裏面看到咱們的容器已經在運行了

進入容器

容器是啓動了,可是咱們無法操做呀,這裏介紹一下怎麼進入容器,可使用該命令進入容器docker exec -ti 容器id bash 執行命令

docker exec -ti 9687356aaf8e bash
複製代碼

響應結果

[root@localhost ~]# docker exec -ti 9687356aaf8e bash
[root@9687356aaf8e /]# 
複製代碼

能夠從響應結果來看咱們登錄的服務器id變成來容器id,也就是所咱們進入了容器了,如今你就能夠在容器裏面作你想要作的事了

退出容器

咱們如今已經在容器裏面了,可是想要退出出來怎麼辦呢?能夠執行exit命令來退出,還能夠按鍵盤上的ctrl + p + q來退出,這兩種退出方式都不會讓容器中止

中止容器

咱們如今已經成功的啓動了一個容器了,如今介紹一下如何中止一個容器,可使用命令docker stop 容器id來中止容器 執行命令

docker stop 9687356aaf8e
複製代碼

響應結果

[root@localhost ~]# docker stop 9687356aaf8e
9687356aaf8e
[root@localhost ~]# 
複製代碼

能夠看到中止容器時會返回一個容器的id

刪除容器

當容器過多或者容器無用的時候咱們就須要將容器刪除,使用如下命令能夠刪除一個容器docker rm 你的容器id 執行命令

docker rm 9687356aaf8e
複製代碼

響應結果

[root@localhost ~]# docker rm 9687356aaf8e
9687356aaf8e
[root@localhost ~]#
複製代碼

能夠看到仍是返回了一個被刪除的容器id,咱們執行如下docker ps -a 來看下容器到底有沒有被刪除掉(想要刪除容器必須讓容器先中止) 執行命令

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                           COMMAND                  CREATED             STATUS                      PORTS               NAMES
e91d9159c248        hello-world                     "/hello"                 3 hours ago         Exited (0) 52 minutes ago                       adoring_mestorf
[root@localhost ~]# 
複製代碼

刪除鏡像

當咱們生成來無用鏡像的時候就須要將這個鏡像刪除掉,可是須要注意的是,刪除鏡像的時候這個鏡像不能有容器,也就是說必需要先將容器刪除掉才行,能夠執行docker rmi 鏡像id來刪除一個鏡像 執行命令

docker rmi 9f38484d220f
複製代碼

響應結果

[root@localhost ~]# docker rmi 9f38484d220f
Untagged: docker.io/centos:latest
Untagged: docker.io/centos@sha256:8d487d68857f5bc9595793279b33d082b03713341ddec91054382641d14db861
Deleted: sha256:9f38484d220fa527b1fb19747638497179500a1bed8bf0498eb788229229e6e1
Deleted: sha256:d69483a6face4499acb974449d1303591fcbb5cdce5420f36f8a6607bda11854
[root@localhost ~]#
複製代碼

能夠看到這裏是返回了多個信息,由於開頭咱們就介紹了鏡像是一層一層的相互繼承的,想要刪除這個鏡像必須一層一層的刪除,因此這裏會有多個刪除記錄。

相關文章
相關標籤/搜索