1. 程式人生 > >2、Docker 基礎安裝和基礎使用

2、Docker 基礎安裝和基礎使用

tab ant 如果 eve creat nes earch mage containe

基礎環境

本次環境使用Centos 7.x版本系統,最小化安裝,系統基礎優化配置請查看 Centos 7.x 系統基礎優化

安裝

使用命令:yum install docker-io -y

[root@node ~]# yum install docker-io -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * elrepo: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package docker.x86_64 2:1.13.1-68.gitdded712.el7.centos will be installed
--> Processing Dependency: docker-common = 2:1.13.1-68.gitdded712.el7.centos for package: 2:docker-1.13.1-6
8.gitdded712.el7.centos.x86_6
..........................................................................
..........................................................................
Installed:
  docker.x86_64 2:1.13.1-68.gitdded712.el7.centos                                                          

Dependency Installed:
  audit-libs-python.x86_64 0:2.8.1-3.el7                                                                   
  checkpolicy.x86_64 0:2.5-6.el7                                                                           
  container-selinux.noarch 2:2.66-1.el7                                                                    
  container-storage-setup.noarch 0:0.10.0-1.gitdf0dcd5.el7                                                 
  docker-client.x86_64 2:1.13.1-68.gitdded712.el7.centos                                                   
  docker-common.x86_64 2:1.13.1-68.gitdded712.el7.centos                                                   
  libcgroup.x86_64 0:0.41-15.el7                                                                           
  libsemanage-python.x86_64 0:2.5-11.el7                                                                   
  oci-register-machine.x86_64 1:0-6.git2b44233.el7                                                         
  oci-systemd-hook.x86_64 1:0.1.16-1.git05bd9a0.el7                                                        
  oci-umount.x86_64 2:2.3.3-3.gite3c9055.el7                                                               
  policycoreutils-python.x86_64 0:2.5-22.el7                                                               
  python-IPy.noarch 0:0.75-6.el7                                                                           
  setools-libs.x86_64 0:3.3.8-2.el7                                                                        
  skopeo-containers.x86_64 1:0.1.31-1.dev.gitae64ff7.el7.centos                                            
  yajl.x86_64 0:2.0.4-4.el7                                                                                

Complete!

啟動關閉docker

啟動docker:systemctl start docker
關閉docker:systemctl stop docker
設置開機自啟:systemctl enable docker

[root@node ~]# systemctl start docker
[root@node ~]# systemctl enable docker
[root@node ~]# 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 Tue 2018-08-21 12:13:47 CST; 3s ago
     Docs: http://docs.docker.com
 Main PID: 1375 (dockerd-current)
   CGroup: /system.slice/docker.service
           ├─1375 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-cur...
           └─1383 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-cont...

Aug 21 12:13:46 node dockerd-current[1375]: time="2018-08-21T12:13:46.884994505+08:00" level=warning...tem"
Aug 21 12:13:46 node dockerd-current[1375]: time="2018-08-21T12:13:46.958221978+08:00" level=info ms...nds"
Aug 21 12:13:46 node dockerd-current[1375]: time="2018-08-21T12:13:46.959354596+08:00" level=info ms...rt."
Aug 21 12:13:47 node dockerd-current[1375]: time="2018-08-21T12:13:47.017988947+08:00" level=info ms...rue"
Aug 21 12:13:47 node dockerd-current[1375]: time="2018-08-21T12:13:47.196687154+08:00" level=info ms...ess"
Aug 21 12:13:47 node dockerd-current[1375]: time="2018-08-21T12:13:47.343822834+08:00" level=info ms...ne."
Aug 21 12:13:47 node dockerd-current[1375]: time="2018-08-21T12:13:47.712563117+08:00" level=info ms...ion"
Aug 21 12:13:47 node dockerd-current[1375]: time="2018-08-21T12:13:47.712714528+08:00" level=info ms...13.1
Aug 21 12:13:47 node dockerd-current[1375]: time="2018-08-21T12:13:47.720577957+08:00" level=info ms...ock"
Aug 21 12:13:47 node systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.

這樣,docker就安裝完成了。

常規使用

docker構建了一個類似於github一樣的倉庫

搜索鏡像

搜索centos鏡像:docker search Docker-name

[root@node ~]# docker search centos
INDEX       NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/centos                             The official build of CentOS.                   4585      [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.io   docker.io/consol/centos-xfce-vnc             Centos container with "headless" VNC sessi...   61                   [OK]
docker.io   docker.io/imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              44                   [OK]
docker.io   docker.io/tutum/centos                       Simple CentOS docker image with SSH access      43                   
docker.io   docker.io/centos/mysql-57-centos7            MySQL 5.7 SQL database server                   38                   
docker.io   docker.io/gluster/gluster-centos             Official GlusterFS Image [ CentOS-7 +  Glu...   32                   [OK]
docker.io   docker.io/openshift/base-centos7             A Centos7 derived base image for Source-To...   31                   
docker.io   docker.io/centos/python-35-centos7           Platform for building and running Python 3...   28                   
docker.io   docker.io/centos/postgresql-96-centos7       PostgreSQL is an advanced Object-Relationa...   26                   
docker.io   docker.io/kinogmt/centos-ssh                 CentOS with SSH                                 22                   [OK]
docker.io   docker.io/centos/php-56-centos7              Platform for building and running PHP 5.6 ...   14                   
docker.io   docker.io/openshift/jenkins-2-centos7        A Centos7 based Jenkins v2.x image for use...   14                   
docker.io   docker.io/pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag nam...   7                    
docker.io   docker.io/openshift/mysql-55-centos7         DEPRECATED: A Centos7 based MySQL v5.5 ima...   6                    
docker.io   docker.io/openshift/jenkins-1-centos7        DEPRECATED: A Centos7 based Jenkins v1.x i...   4                    
docker.io   docker.io/openshift/wildfly-101-centos7      A Centos7 based WildFly v10.1 image for us...   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/jameseckersall/sonarr-centos       Sonarr on CentOS 7                              0                    [OK]
docker.io   docker.io/pivotaldata/centos-gcc-toolchain   CentOS with a toolchain, but unaffiliated ...   0                    
docker.io   docker.io/smartentry/centos                  centos with smartentry                          0                    [OK]

說明:

列名 說明
INDEX 索引
NAME 鏡像名稱
DESCRIPTION 描述
STARS 星級,受歡迎的程度
OFFICIAL 是否是官方的
AUTOMATED 是否是自動構建的

需要註意的:NAME,官方的,就是Centos,而剩下的,是用戶名/名稱

獲取鏡像

命令:docker pull Docker-name

[root@node ~]# docker pull centos
Using default tag: latest
Trying to pull repository docker.io/library/centos ... 
latest: Pulling from docker.io/library/centos
256b176beaff: Pull complete 
Digest: sha256:6f6d986d425aeabdc3a02cb61c02abb2e78e57357e92417d6d58332856024faf
Status: Downloaded newer image for docker.io/centos:lates

查看鏡像

命令:docker images

[root@node ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos    latest              5182e96772bf        2 weeks ago         200 MB
字段名 解釋
REPOSITORY 鏡像來自於那個倉庫
TAG 鏡像的標簽
IMAGE ID 鏡像的ID,每個鏡像都有一個唯一的ID
CREATED 創建的時間
VIRTUAL SIZE 鏡像的大小

此時,鏡像也pull下來了,準備工作都做好了,下面就開始讓docker做一些簡單的事情了。

讓docker輸出點東西出來

命令:docker run centos /bin/echo ‘Hello World.‘

意思是:運行centos容器,並/bin/echo 輸出‘Hello World.‘

[root@node ~]# docker run centos /bin/echo ‘Hello World.‘
Hello World.

查看運行或者停止的所有容器

命令:docker ps -a

[root@node ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
99ceccfc37ac        centos              "/bin/echo ‘Hello ..."   2 minutes ago       Exited (0) 2 minutes ago                       elegant_gates
字段名 解釋
CONTAINER ID 容器 ID
IMAGE 鏡像
COMMAND 運行的內容
CREATED 創建時間
STSTUA 容器狀態
PORTS 其他
NAMES 容器名,如果不手動指定,則在名字庫中隨機獲取一個

docker容器也能當做正常的操作系統來使用,雖然不建議

命令:docker run --name 隨意指定一個docker名稱 -it 鏡像名 /bin/bash

-i 表示進入到容器的輸入終端
-t 表示開啟一個偽終端tty綁定到表輸入上

[root@node ~]# docker run --name mydocker -it centos /bin/bash
# 這裏看到運行後,提示符都變了,這個提示符是該容器的ID值
[root@7c9a7f01acb4 /]# ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
# 執行ps aux 只可以看到兩個進程
[root@7c9a7f01acb4 /]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.0  11832  3036 ?        Ss   05:52   0:00 /bin/bash
root         15  0.0  0.0  51720  3512 ?        R+   05:53   0:00 ps aux
# 可以創建一個文件或目錄
[root@7c9a7f01acb4 /]# cd /tmp/
[root@7c9a7f01acb4 tmp]# mkdir mydocker
[root@7c9a7f01acb4 tmp]# ls -ld mydocker/
drwxr-xr-x 2 root root 6 Aug 22 05:57 mydocker/
# 查看該容器的ip地址
[root@7c9a7f01acb4 tmp]# yum install net-tools
[root@7c9a7f01acb4 tmp]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe11:2  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 3748  bytes 12494964 (11.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2696  bytes 149466 (145.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
# 使用exit退出容器
[root@7c9a7f01acb4 tmp]# exit
exit
# 退出後,可以看到命令提示符也變回原來的了。
[root@node ~]#

在執行上述命令的過程都幹啥了?

  1. 會檢查你當前的鏡像中有沒有centos鏡像,如果有則啟動,如果沒有,會自動幫你pull一個下來
  2. 然後會給容器分配一個文件系統,放在鏡像的上面一層,讓它可寫
  3. 然後給容器分配一個IP地址
  4. docker會運行一個docker指定的應用程序,如果應用程序退出了,則該daocker容器就會退出。

在容器中可以正常操作,只是有很多命令都沒有

退出後,再次查看一下容器狀態

[root@node ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
7c9a7f01acb4        centos              "/bin/bash"              10 minutes ago      Exited (127) 44 seconds ago                       mydocker
99ceccfc37ac        centos              "/bin/echo ‘Hello ..."   35 minutes ago      Exited (0) 35 minutes ago                         elegant_gates

可以看到容器名是我們指定的,並且已經退出了,生命周期已經結束了。

啟動一個停止的容器

命令:docker start 容器ID

# 通過docker ps -a 查看容器ID
[root@node ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS               NAMES
7c9a7f01acb4        centos              "/bin/bash"              10 minutes ago      Exited (127) 44 seconds ago                       mydocker
99ceccfc37ac        centos              "/bin/echo ‘Hello ..."   35 minutes ago      Exited (0) 35 minutes ago                         elegant_gates
# 運行
[root@node ~]# docker start 99ceccfc37ac
99ceccfc37ac
#再次查看
[root@node ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
7c9a7f01acb4        centos              "/bin/bash"              13 minutes ago      Exited (127) 3 minutes ago                       mydocker
99ceccfc37ac        centos              "/bin/echo ‘Hello ..."   38 minutes ago      Exited (0) 2 seconds ago                         elegant_gates

這裏看到運行後查看也退出了,因為我們運行的容器只是輸出了一個‘Hello World.‘程序就退出了,容器也就結束了。

停止一個容器

命令:docker stop 容器ID

這裏就不演示了。

刪除一個容器

命令:docker rm 容器ID

正常只能刪除已經停止的容器,如果是啟動狀態,會報錯,除非增加-f 參數強制刪除。

[root@node ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                       PORTS               NAMES
7c9a7f01acb4        centos              "/bin/bash"              13 minutes ago      Exited (127) 3 minutes ago                       mydocker
99ceccfc37ac        centos              "/bin/echo ‘Hello ..."   38 minutes ago      Exited (0) 2 seconds ago                         elegant_gates
[root@node ~]# docker rm 7c9a7f01acb4
7c9a7f01acb4
# 這裏可以看到已經被刪除了。
[root@node ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
99ceccfc37ac        centos              "/bin/echo ‘Hello ..."   45 minutes ago      Exited (0) 7 minutes ago                       elegant_gates

2、Docker 基礎安裝和基礎使用