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?本文將快速帶領你走進容器的世界。程序員
docker是什麼? 官方解釋:Docker 是一個開源的應用容器引擎,讓開發者能夠打包他們的應用以及依賴包到一個可移植的容器中,而後發佈到任何流行的 Linux 機器上,也能夠實現虛擬化。容器是徹底使用沙箱機制,相互之間不會有任何接口sql
我的大白話:或許你如今尚未用過docker,可是你可能用過虛擬機吧?docker和虛擬機的差異在哪兒呢?首先第一個,虛擬機給人的第一映像就是大和重,想要部署一個環境到虛擬機上面,就必須安裝一個操做系統才能作相關的操做,其次就是你無論安裝什麼你都得安裝一個操做系統才能運行。可是如今docker的出現就解決了這個問題,爲何docker就解決了這個問題呢?其實docker就是將這個軟件所須要的操做系統或者相關環境給精簡了,去除了全部你用不到的東西,只留下軟件最基本的依賴,這些依賴會一層依賴一層,這就會讓一個docker的鏡像很是小,比起整個操做系統輕便了很是多,並且最主要的是能夠將你的環境打包成一個鏡像發佈到dockerhub等地方,而後你就在各個流行的linux上面直接將鏡像下載下來就能夠直接使用了,能夠說就是一次部署,處處使用。docker
docker能幫咱們解決什麼問題? 衆所周知,每次咱們的項目代碼編寫完成事後,會將代碼發佈到服務器上,可是服務器必需要安裝一系列咱們須要的工具或者依賴包,亦或者是一些依賴的軟件,這個時候發佈項目的時間成本就過高了,若是碰到不熟悉的工做人員,這個時間可能會拉的更長,可是有了docker就不同了,咱們只須要簡單的會docker,會一些基礎的image和container相關的命令,就能完成這項工做,並且在不少太服務器的時候,也只須要一個docker就能幫咱們完成部署。ubuntu
爲何要使用docker? 可能你在想我都有一個Linux系統了還要docker來幹嗎?可是你有沒有想過當你係統的環境須要遷移的時候,好比說大家須要集羣,或者須要搭建測試服務器這些等等等等,你就須要花大量的時間來搭建環境,而後下載一些相應的包,可是這中間頗有可能出現各類各樣的坑,這些都是沒法避免的,可是docker就是一個容器,它無論在那個環境上面運行,它都依賴於本身的容器,而不會去依賴裝載容器的系統,這也就避免了由於系統的緣由致使安裝的步驟都不同。centos
仍是以程序員的靈魂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 ~]#
複製代碼
上面咱們已經運行了一個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
,分析一下這個鏡像的字段吧
docker run hello-world:latest
docker run fce289e99eb9
來啓動。這裏咱們來看下由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 ~]#
複製代碼
分析字段
-p 宿主機端口:容器端口
來設置--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 ~]#
複製代碼
上面咱們已經使用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
複製代碼
參數描述
帶上參數的緣由是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 ~]#
複製代碼
能夠看到這裏是返回了多個信息,由於開頭咱們就介紹了鏡像是一層一層的相互繼承的,想要刪除這個鏡像必須一層一層的刪除,因此這裏會有多個刪除記錄。