Centos7——docker入門(筆記)

docker 入門(筆記)

1、Docker是什麼?

  官方原話: Docker provides a way to run applications securely isolated in a container, packaged with all its dependencies and libraries.  linux

  Docker Documentation: https://docs.docker.com/get-started/nginx

2、Docker應用場景:

  在很是低的額外開銷的狀況下,打造一個一致性的環境。 git

  • 簡化配置
  • 代碼流水線(Code Pipeline)管理
  • 提升開發效率
  • 隔離應用
  • 整合服務器
  • 調試能力Docker
  • 多租戶環境
  • 快速部署

3、Docker改變了什麼

  • 面向產品:產品交付
  • 面向開發:簡化環境配置
  • 面向測試:多版本測試
  • 面向運維:環境一致性
  • 面向架構:自動化擴容(微服務)

4、環境準備

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 
[root@localhost ~]# uname -r
3.10.0-862.11.6.el7.x86_64

5、安裝docker

[root@localhost ~]# yum install -y docker Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Package 2:docker-1.13.1-74.git6e3bb8e.el7.centos.x86_64 already installed and latest version Nothing to do [root@localhost ~]# systemctl start docker [root@localhost ~]# systemctl enable docker [root@localhost ~]# docker version Client: Version: 1.13.1 API version: 1.26 Package version: docker-1.13.1-74.git6e3bb8e.el7.centos.x86_64 Go version: go1.9.4 Git commit: 6e3bb8e/1.13.1 Built: Tue Aug 21 15:23:37 2018 OS/Arch: linux/amd64 Server: Version: 1.13.1 API version: 1.26 (minimum version 1.12) Package version: docker-1.13.1-74.git6e3bb8e.el7.centos.x86_64 Go version: go1.9.4 Git commit: 6e3bb8e/1.13.1 Built: Tue Aug 21 15:23:37 2018 OS/Arch: linux/amd64 Experimental: false [root@localhost ~]# systemctl start docker.service

6、Docker鏡像管理

# 搜索全部centos的docker鏡像docker

[root@localhost ~]# docker search centos
INDEX       NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/centos                             The official build of CentOS.                   4642      [OK]       
docker.io   docker.io/ansible/centos7-ansible            Ansible on Centos7                              115                  [OK]
docker.io   docker.io/jdeathe/centos-ssh                 CentOS-6 6.10 x86_64 / CentOS-7 7.5.1804 x...   99                   [OK]

 #可使用docker pull命令來從倉庫獲取所須要的鏡像。下面的例子將從Docker Hub倉庫下載一個Centos操做系統的鏡像。centos

[root@localhost ~]# docker pull centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ... 
latest: Pulling from docker.io/library/centos
Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
Status: Image is up to date for docker.io/centos:latest
[root@localhost ~]# docker run -it centos /bin/bash 
# 完成後可使用該鏡像建立一個容器 
[root@2a1b7476db10 /]# ls  
anaconda-post.log  bin  dev etc home  lib lib64  media mnt opt proc root run  sbin  srv sys  tmp  usr var 

# 鏡像的ID惟一標識了鏡像,若是ID相同,說明是同一鏡像。TAG信息來區分不一樣發行版本,若是不指定具體標記,默認使用latest標記信息。bash

[root@2a1b7476db10 /]# exit
exit
# 查看docker鏡像
[root@localhost ~]# docker images
REPOSITORY(來自那個倉庫) TAG(標籤)           IMAGE ID(惟一ID)    CREATED(建立時間)   SIZE(大小)
docker.io/nginx          latest              71c43202b8ac        19 hours ago        109 MB
docker.io/centos         latest              5182e96772bf        3 weeks ago         200 MB
docker.io/tutum/centos   latest              99a633ad346f        2 years ago         297 MB
[root@localhost ~]# 

# 若是要移除本地的鏡像,可使用docker rmi命令(在刪除鏡像以前先用docker rm刪除依賴於這個鏡像的全部容器)。注意docker rm 命令是移除容器。服務器

[root@localhost ~]# docker rmi 99a633ad346f  # 刪除docker鏡像
Untagged: docker.io/tutum/centos:latest
Untagged: docker.io/tutum/centos@sha256:b4de18abfef698f6ae3a4921d6f58edef8fc770c6ee5da7636fa4ea51ab545c5
Deleted: sha256:99a633ad346ff9debae2e18ef28e36da72c4535d936500e497cd34377173d4b6
Deleted: sha256:e72d160abbd5585d403996036763c88e9624c70d9d80d6fb0e1ccb2c49b8a26d
Deleted: sha256:8f201733668a3ce8ce77a0dc8b2d7249cdc70b1022a7fc78650527a6f3ec09b6
Deleted: sha256:8fdcefb6d553194a6881387055c1d4110bc385d3dbf4d51b0aa07f1251e877ef
Deleted: sha256:8f98c28a55c2e54cf8b8815d843cbfaf1495dd0bf601774f27da1cbe80183225
Deleted: sha256:6163e9854471a9b6c817b9514d6b8d1cf7be7684fa0c52d76d42188e28785859
Deleted: sha256:85946201865d0456e3d2db4df8699ecc017f2a8df599a1c0b241d30e8f942ed6
Deleted: sha256:3b6d6efff38bc7552b4e42d78a142460dd122efe3f86a921298e82d60f770c85
Deleted: sha256:28508c96f9bffc44568bdcf1d0cd20a0357a106a18440302ba67ee4c68a3ccad
Deleted: sha256:33b3813290610f3880214f35c32d2175adfcc75500cae48dfdbcbcf31928a1ac
Deleted: sha256:ce3c560f1cfc18c6febc0a8ed4f8330d32df64e52fa0b34b82d42ad35406ac39
Deleted: sha256:cf032be00d9aedaf94363af683f83fda44140490759c1c048584dc9b3dd1f34e
Deleted: sha256:5f70bf18a086007016e948b04aed3b82103a36bea41755b6cddfaf10ace3c6ef
[root@localhost ~]# 

# 若是要導出鏡像到本地文件,可使用docker save命令。架構

[root@localhost ~]# docker save centos > /opt/centos.tar.gz  # 導出docker鏡像至本地
[root@localhost ~]# ls -l /opt/centos.tar.gz 
-rw-r--r--. 1 root root 208301056 Aug 30 06:47 /opt/centos.tar.gz
[root@localhost ~]# du -sh /opt/centos.tar.gz 
199M	/opt/centos.tar.gz
[root@localhost ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/nginx     latest              71c43202b8ac        19 hours ago        109 MB
docker.io/centos    latest              5182e96772bf        3 weeks ago         200 MB

 # 可使用docker load從本地文件中導入到本地docker鏡像庫app

[root@localhost ~]# docker load < /opt/centos.tar.gz # 導入本地鏡像到docker鏡像庫
Loaded image: docker.io/centos:latest
[root@localhost ~]# docker images			# 查看鏡像導入狀況
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/nginx     latest              71c43202b8ac        19 hours ago        109 MB
docker.io/centos    latest              5182e96772bf        3 weeks ago         200 MB
[root@localhost ~]# 

 7、Docker管理

Docker的容器實在過輕量級了,不少時候用戶都是隨時刪除和新建立容器。啓動容器有兩種方式:運維

  1. 基於鏡像新建一個容器並啓動;
  2. 將在終止狀態(stopped)的容器從新啓動。

# 新建容器並啓動

[root@localhost ~]# docker run centos /bin/echo 'Hello,World!' # 這跟在本地直接執行 /bin/echo ‘Hello,World’同樣  
Hello,World!
[root@localhost ~]# docker run --name mycentos -it centos /bin/bash # 啓動一個bash終端,容許用戶進行交互。
[root@e930e732fa58 /]# 
  • –name:給容器定義一個名稱
  • -i:則讓容器的標準輸入保持打開。
  • -t:讓Docker分配一個僞終端,並綁定到容器的標準輸入上
  • /bin/bash:執行一個命令

當利用docker run來建立容器時,Docker在後臺運行的標準操做包括:

  1. 檢查本地是否存在指定的鏡像,不存在就從公有倉庫下載
  2. 利用鏡像建立並啓動一個容器
  3. 分配一個文件系統,並在只讀的鏡像層外面掛在一層可讀寫層
  4. 從宿主主機配置的網橋接口中橋接一個虛擬接口到容器中去
  5. 從地址池配置一個ip地址給容器
  6. 執行用戶指定的應用程序
  7. 執行完畢後容器被終止

# 啓動已終止的容器

  能夠利用docker start命令,直接將一個已經終止的容器啓動運行。容器的核心爲所執行的應用程序,所須要的資源都是應用程序運行所必需的。除此以外,並無其餘的資源。能夠在僞終端中利用ps和top來查看進程信息。

[root@localhost ~]# docker start mycentos # 啓動一個終止的容器
mycentos
[root@localhost ~]# docker ps -a  # 查看是否啓動
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
e930e732fa58        centos              "/bin/bash"              8 minutes ago       Up 8 seconds                                    mycentos
19992e1d2669        centos              "/bin/echo Hello,W..."   9 minutes ago       Exited (0) 9 minutes ago                        distracted_borg
……
[root@localhost ~]# 

# 守護進程運行

  若是須要讓Docker容器在後臺以守護形式運行。此時能夠經過添加-d參數來實現。


[root@localhost ~]# docker run -d centos /bin/bash -c 'while true; do echo "Hello,World!"; sleep 1;done'
a017e98a7ce8e8675d077fff1237886f8de8a59ce6b24d4774de009621ce5449
[root@localhost ~]# docker ps # 列出容器
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
a017e98a7ce8        centos              "/bin/bash -c 'whi..."   18 seconds ago      Up 17 seconds                           mystifying_mclean
ff3c866aa8c8        docker.io/centos    "/bin/bash"              4 hours ago         Up 4 hours                              inspiring_torvalds
[root@localhost ~]# docker logs a017e98a7ce8 # 獲取容器輸出信息,經過dockerlogs命令
Hello,World!
Hello,World!
Hello,World!
Hello,World!
……
[root@localhost ~]# 

# 中止容器

  可使用docker stop來終止一個運行中的容器。此外,當Docker容器中指定的應用終結時,容器也自動終止。例如啓動一個終端的容器,用戶經過exit命令或者ctrl+d來退出終端時,所建立的容器馬上終止。

  終止狀態的容器能夠用docker ps -a命令看到,也能夠經過docker start ID 命令來啓動容器。


[root@localhost ~]# docker ps  # 列出容器
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
a017e98a7ce8        centos              "/bin/bash -c 'whi..."   3 minutes ago       Up 3 minutes                            mystifying_mclean
ff3c866aa8c8        docker.io/centos    "/bin/bash"              4 hours ago         Up 4 hours                              inspiring_torvalds
[root@localhost ~]# docker stop a017e98a7ce8 # 中止指定ID的容器 a017e98a7ce8
[root@localhost ~]# docker ps	# 指定ID的容器已經看不到了
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ff3c866aa8c8        docker.io/centos    "/bin/bash"         4 hours ago         Up 4 hours                              inspiring_torvalds
[root@localhost ~]#

 # 刪除容器


[root@localhost ~]# docker ps -a                # 查看全部容器當前狀態
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                        PORTS               NAMES
78e4523d92c3        centos              "/bin/bash -c 'whi..."   About a minute ago   Exited (137) 51 seconds ago                       relaxed_swirles
ff3c866aa8c8        docker.io/centos    "/bin/bash"              4 hours ago          Up 4 hours                                        inspiring_torvalds
[root@localhost ~]# docker rm `docker ps -a -q`        # 刪除全部的容器
78e4523d92c3
Error response from daemon: You cannot remove a running container ff3c866aa8c8e342a5418f8de9eaec6f7445a03c420cdc35b1b5a39a9c07e237. Stop the container before attempting removal or use -f [root@localhost ~]# docker ps -a                # 查看全部容器當前狀態,發現運行中的容器未刪除
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ff3c866aa8c8        docker.io/centos    "/bin/bash"         4 hours ago         Up 4 hours                              inspiring_torvalds
[root@localhost ~]# docker rm -f ff3c866aa8c8	        # 刪除正在運行的容器
ff3c866aa8c8
[root@localhost ~]# docker ps -a				# 查看全部的容器
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost ~]#

# 進入容器

  使用-d參數時,容器啓動後會進入後臺。某些時候須要進入容器進行操做,有不少種方法,包括使用docker attach命令或nsenter工具等。

一、docker attach是Docker自帶的命令:

[root@localhost ~]# docker ps -a        # 查看全部的容器當前狀態
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS       
3a67130228df        docker.io/centos    "/bin/bash -c 'whi..."   About a minute ago   Up About a minute               
[root@localhost ~]# docker attach 3a67130228df # 一直卡在這裏,不知道是否是由於沒有ssh訪問的緣由  

因而我從新pull一個帶ssh的centos鏡像


[root@localhost ~]# docker images
REPOSITORY               TAG                 IMAGE ID            CREATED             SIZE
docker.io/nginx          latest              71c43202b8ac        20 hours ago        109 MB
docker.io/centos         latest              5182e96772bf        3 weeks ago         200 MB
docker.io/tutum/centos   latest              99a633ad346f        2 years ago         297 MB # 就是它!
[root@localhost ~]# docker run -d --name newcentos docker.io/tutum/centos /bin/bash -c 'while true; do echo Hello,Wolrd;sleep 1;done' # 啓動容器
5c9d7d40e4d8e6654a4592681d97826558c2e3f30b7c264f8487a8f387dda30e
[root@localhost ~]# docker logs 5c9d7d40e4d8e6654a4592681d97826558c2e3f30b7c264f8487a8f387dda30e # 一直有輸出,我放心了
Hello,Wolrd
Hello,Wolrd
Hello,Wolrd
Hello,Wolrd
Hello,Wolrd
Hello,Wolrd
Hello,Wolrd
Hello,Wolrd
[root@localhost ~]# docker ps # 列出容器
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS               NAMES
5c9d7d40e4d8        docker.io/tutum/centos   "/bin/bash -c 'whi..."   12 seconds ago      Up 11 seconds       22/tcp              newcentos
3a67130228df        docker.io/centos         "/bin/bash -c 'whi..."   14 minutes ago      Up 14 minutes                           zen_golick
[root@localhost ~]# docker attach 5c9d7d40e4d8 # 進入容器
Hello,Wolrd
Hello,Wolrd
Hello,Wolrd
Hello,Wolrd

 可是使用attach命令有時候並不方便。當多個窗口同時attach到同一個容器的時候,全部的窗口都會同步顯示,當某個窗口因命令阻塞時,其餘窗口也沒法執行操做了。

二、nsenter命令

  nsenter能夠訪問另外一個進程的名字空間。nsenter須要有root權限。


[root@localhost ~]# docker ps            # 列出容器
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS   
5c9d7d40e4d8        docker.io/tutum/centos   "/bin/bash -c 'whi..."   6 minutes ago       Up 6 minutes        22/tcp  
3a67130228df        docker.io/centos         "/bin/bash -c 'whi..."   20 minutes ago      Up 20 minutes               
[root@localhost ~]#  yum install -y util-linux  # 安裝包中有須要用到的nsenter
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Package util-linux-2.23.2-52.el7_5.1.x86_64 already installed and latest version
Nothing to do
[root@localhost ~]# docker inspect --format "{{.State.Pid}}" 5c9d7d40e4d8 #找到容器的第一個進程PID
13333
[root@localhost ~]# nsenter -t 13333 -u -i -n -p # 經過這個PID鏈接到容器
[root@5c9d7d40e4d8 ~]# # 進入容器中

  方便起見,將這2條命令腳本化

[root@192.168.0.155]$~:> cat in.sh 
#!/bin/bash
# zuoyang@jd.com
PID=$(docker inspect --format 「{{.State.Pid}}」 $1)
nsenter -t $PID -u -i -n -p 
相關文章
相關標籤/搜索