SpringCloud(第 052 篇)CentOS7 安裝 Docker 以及經常使用操做命令講解

SpringCloud(第 052 篇)CentOS7 安裝 Docker 以及經常使用操做命令講解

-php

1、大體介紹

本章節主要帶入你們初步進入 Docker,體驗一下docker的安裝步驟以及操做命令。

2、安裝步驟

2.1 Docker環境部署要求

一、Docker 須要運行在 64-bit 的操做系統上而且要求Linux 內核版本不小於 3.10,OS7知足這個要求;
二、其他低版本的可使用yum update 命令對操做系統內核進行升級! 
三、系統內核版本查看命令:uname -r

[root@svr01 ~]# uname -r
3.10.0-514.6.2.el7.x86_64

2.2 yum 命令安裝 Docker

[root@svr01 ~]# yum -y install docker

安裝完了會看到以下的打印信息:
Installed:
  docker.x86_64 2:1.12.6-68.gitec8512b.el7.centos                                                                                      

Dependency Installed:
  audit-libs-python.x86_64 0:2.7.6-3.el7                                checkpolicy.x86_64 0:2.5-4.el7                                 
  container-selinux.noarch 2:2.33-1.git86f33cd.el7                      container-storage-setup.noarch 0:0.8.0-3.git1d27ecf.el7        
  device-mapper-event.x86_64 7:1.02.140-8.el7                           device-mapper-event-libs.x86_64 7:1.02.140-8.el7               
  device-mapper-persistent-data.x86_64 0:0.7.0-0.1.rc6.el7              docker-client.x86_64 2:1.12.6-68.gitec8512b.el7.centos         
  docker-common.x86_64 2:1.12.6-68.gitec8512b.el7.centos                libcgroup.x86_64 0:0.41-13.el7                                 
  libseccomp.x86_64 0:2.3.1-3.el7                                       libsemanage-python.x86_64 0:2.5-8.el7                          
  lvm2.x86_64 7:2.02.171-8.el7                                          lvm2-libs.x86_64 7:2.02.171-8.el7                              
  oci-register-machine.x86_64 1:0-3.13.gitcd1e331.el7                   oci-systemd-hook.x86_64 1:0.1.14-1.git1ba44c6.el7              
  oci-umount.x86_64 2:2.3.0-1.git51e7c50.el7                            policycoreutils-python.x86_64 0:2.5-17.1.el7                   
  python-IPy.noarch 0:0.75-6.el7                                        setools-libs.x86_64 0:3.3.8-1.1.el7                            
  skopeo-containers.x86_64 1:0.1.26-2.dev.git2e8377a.el7.centos         yajl.x86_64 0:2.0.4-4.el7                                      

Dependency Updated:
  audit.x86_64 0:2.7.6-3.el7                        audit-libs.x86_64 0:2.7.6-3.el7        device-mapper.x86_64 7:1.02.140-8.el7       
  device-mapper-libs.x86_64 7:1.02.140-8.el7        libsemanage.x86_64 0:2.5-8.el7         policycoreutils.x86_64 0:2.5-17.1.el7       

Complete!

2.3 檢測 Docker 是否安裝成功

[root@svr01 ~]# docker images
Cannot connect to the Docker daemon. Is the docker daemon running on this host?

打印該信息說明docker已經安裝了,只是沒有啓動docker而已;

2.4 啓動 docker 後臺服務

[root@svr01 ~]# service docker start
Redirecting to /bin/systemctl start  docker.service

2.5 查看 docker 的一些相關信息

一、查看鏡像列表
[root@svr01 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

二、查看運行的鏡像列表
[root@svr01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

2.6 測試下載 hello-world 鏡像,本地沒有的話則會從docker.io的遠端鏡像庫下載

一、下載 hello-world 鏡像
[root@svr01 ~]# 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

ca4f61b1923c: Pull complete 
Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c

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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

二、再次查看 docker 現有的資源:
[root@svr01 ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world   latest              f2a91732366c        3 weeks ago         1.848 kB

[root@svr01 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

2.7 搜索鏡像文件

[root@svr01 ~]# docker search centos
INDEX       NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/centos                             The official build of CentOS.                   3876      [OK]       
docker.io   docker.io/ansible/centos7-ansible            Ansible on Centos7                              103                  [OK]
docker.io   docker.io/jdeathe/centos-ssh                 CentOS-6 6.9 x86_64 / CentOS-7 7.4.1708 x8...   90                   [OK]
docker.io   docker.io/consol/centos-xfce-vnc             Centos container with "headless" VNC sessi...   37                   [OK]
docker.io   docker.io/imagine10255/centos6-lnmp-php56    centos6-lnmp-php56                              34                   [OK]
docker.io   docker.io/tutum/centos                       Simple CentOS docker image with SSH access      34                   
docker.io   docker.io/gluster/gluster-centos             Official GlusterFS Image [ CentOS-7 +  Glu...   21                   [OK]
docker.io   docker.io/kinogmt/centos-ssh                 CentOS with SSH                                 17                   [OK]
docker.io   docker.io/centos/python-35-centos7           Platform for building and running Python 3...   14                   
docker.io   docker.io/openshift/base-centos7             A Centos7 derived base image for Source-To...   13                   
docker.io   docker.io/centos/php-56-centos7              Platform for building and running PHP 5.6 ...   10                   
docker.io   docker.io/openshift/jenkins-2-centos7        A Centos7 based Jenkins v2.x image for use...   7                    
docker.io   docker.io/openshift/mysql-55-centos7         DEPRECATED: A Centos7 based MySQL v5.5 ima...   6                    
docker.io   docker.io/darksheer/centos                   Base Centos Image -- Updated hourly             3                    [OK]
docker.io   docker.io/openshift/ruby-20-centos7          DEPRECATED: A Centos7 based Ruby v2.0 imag...   3                    
docker.io   docker.io/blacklabelops/centos               CentOS Base Image! Built and Updates Daily!     1                    [OK]
docker.io   docker.io/miko2u/centos6                     CentOS6 鏃ユ湰瑾炵挵澧                                  1                    [OK]
docker.io   docker.io/openshift/php-55-centos7           DEPRECATED: A Centos7 based PHP v5.5 image...   1                    
docker.io   docker.io/pivotaldata/centos-gpdb-dev        CentOS image for GPDB development. Tag nam...   1                    
docker.io   docker.io/pivotaldata/centos-mingw           Using the mingw toolchain to cross-compile...   1                    
docker.io   docker.io/jameseckersall/sonarr-centos       Sonarr on CentOS 7                              0                    [OK]
docker.io   docker.io/openshift/wildfly-101-centos7      A Centos7 based WildFly v10.1 image for us...   0                    
docker.io   docker.io/pivotaldata/centos                 Base centos, freshened up a little with a ...   0                    
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]

2.8 拉取鏡像文件

[root@svr01 ~]# docker pull docker.io/centos

2.9 運行剛剛下載的 docker.io/centos 鏡像

[root@svr01 ~]# docker run -i -t docker.io/centos /bin/bash
[root@9f053696bedb /]#

一、運行 docker run 命令後會看到已經進入的鏡像文件內部,所以會看到 [root@9f053696bedb /]# 這樣的命令展現信息。

二、進入鏡像隨便使用看看
[root@9f053696bedb /]# ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@9f053696bedb /]# cd /home/
[root@9f053696bedb home]# mkdir hmilyylimh
[root@9f053696bedb home]# mkdir -p hmilyylimh/docker
[root@9f053696bedb home]# cd hmilyylimh/docker/
[root@9f053696bedb docker]# 

三、退出鏡像
[root@9f053696bedb docker]# exit
exit
[root@svr01 ~]# 

四、參數說明:
    -a stdin: 指定標準輸入輸出內容類型,可選 STDIN/STDOUT/STDERR 三項;
    -d: 後臺運行容器,並返回容器ID;
    -i: 以交互模式運行容器,一般與 -t 同時使用;
    -t: 爲容器從新分配一個僞輸入終端,一般與 -i 同時使用;
    --name="nginx-lb": 爲容器指定一個名稱;
    --dns 8.8.8.8: 指定容器使用的DNS服務器,默認和宿主一致;
    --dns-search example.com: 指定容器DNS搜索域名,默認和宿主一致;
    -h "mars": 指定容器的hostname;
    -e username="ritchie": 設置環境變量;
    --env-file=[]: 從指定文件讀入環境變量;
    --cpuset="0-2" or --cpuset="0,1,2": 綁定容器到指定CPU運行;
    -m :設置容器使用內存最大值;
    --net="bridge": 指定容器的網絡鏈接類型,支持 bridge/host/none/container: 四種類型;
    --link=[]: 添加連接到另外一個容器;
    --expose=[]: 開放一個端口或一組端口;

2.10 在 docker.io/centos 鏡像中隨便裝個東西,舉例裝個json

[root@svr01 ~]# docker run -i -t docker.io/centos /bin/bash
[root@497905f140a6 /]# gem install json
bash: gem: command not found
[root@497905f140a6 /]# yum install gem
。。。。
Installed:
  rubygems.noarch 0:2.0.14.1-30.el7                                                                                                                                    

Dependency Installed:
  libyaml.x86_64 0:0.1.4-11.el7_0             ruby.x86_64 0:2.0.0.648-30.el7              ruby-irb.noarch 0:2.0.0.648-30.el7    ruby-libs.x86_64 0:2.0.0.648-30.el7   
  rubygem-bigdecimal.x86_64 0:1.2.0-30.el7    rubygem-io-console.x86_64 0:0.4.2-30.el7    rubygem-json.x86_64 0:1.7.7-30.el7    rubygem-psych.x86_64 0:2.0.0-30.el7   
  rubygem-rdoc.noarch 0:4.0.0-30.el7         

Complete!
[root@497905f140a6 /]# gem install json 
Fetching: json-2.1.0.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing json:
        ERROR: Failed to build gem native extension.

    /usr/bin/ruby extconf.rb
mkmf.rb can't find header files for ruby at /usr/share/include/ruby.h


Gem files will remain installed in /usr/local/share/gems/gems/json-2.1.0 for inspection.
Results logged to /usr/local/share/gems/gems/json-2.1.0/ext/json/ext/generator/gem_make.out

2.11 針對已經安裝json的docker.io/centos鏡像庫打包,tag命名爲v2

[root@497905f140a6 /]# docker commit -m ="add new image" -a="hmilyylimh" 497905f140a6 docker.io/centos:v2
bash: docker: command not found
[root@497905f140a6 /]# exit
exit
[root@svr01 ~]# docker commit -m ="add new image" -a="HEHUI231" 497905f140a6 docker.io/centos:v2
sha256:11efb35f320cec46c83bc4dcbc184c8d45dcb3e369105251d70e2336fd261c92
[root@svr01 ~]# docker images;
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos        v2                  11efb35f320c        17 seconds ago      307.5 MB
docker.io/centos        latest              3fa822599e10        2 weeks ago         203.5 MB
docker.io/hello-world   latest              f2a91732366c        3 weeks ago         1.848 kB

2.12 給剛剛打包的 docker.io/centos:v2 鏡像設置鏡像標籤

[root@svr01 ~]# docker tag 11efb35f320c docker.io/centos:v22
[root@svr01 ~]# docker images;
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos        v2                  11efb35f320c        36 minutes ago      307.5 MB
docker.io/centos        v22                 11efb35f320c        36 minutes ago      307.5 MB
docker.io/centos        latest              3fa822599e10        2 weeks ago         203.5 MB
docker.io/hello-world   latest              f2a91732366c        3 weeks ago         1.848 kB

2.13 刪除 docker.io/centos:v2 鏡像文件

[root@svr01 ~]# docker rmi docker.io/centos:v22
Untagged: docker.io/centos:v22
[root@svr01 ~]# docker images;
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos        v2                  11efb35f320c        37 minutes ago      307.5 MB
docker.io/centos        latest              3fa822599e10        2 weeks ago         203.5 MB
docker.io/hello-world   latest              f2a91732366c        3 weeks ago         1.848 kB

2.14 刪除單個已經中止的容器

[root@svr01 ~]# docker rm 47de3399a0ab
47de3399a0ab

[root@svr01 ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

2.15 刪除全部已經中止的容器

[root@svr01 ~]# docker rm $(docker ps -a -q)

ca94bd87299f
43a27d23f3ee
497905f140a6
fb9512c00a84
9f053696bedb
565b5d3e5139
9c2518ba47c8
1abaaef82836
e5b39fc724ab

[root@svr01 ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

2.16 殺掉單個正在執行的容器

[root@svr01 ~]# docker kill 47de3399a0ab
47de3399a0ab

[root@svr01 ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

2.17 殺掉全部正在執行的容器

[root@svr01 ~]# docker kill $(docker ps -q)

ca94bd87299f
43a27d23f3ee
497905f140a6
fb9512c00a84
9f053696bedb
565b5d3e5139
9c2518ba47c8
1abaaef82836
e5b39fc724ab

[root@svr01 ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

2.18 將image文件保存到磁盤目錄

[root@svr01 ~]# docker images;
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos                       v2                  11efb35f320c        5 hours ago         307.5 MB
docker.io/frolvlad/alpine-oraclejdk8   latest              4f03dc990224        12 days ago         170.1 MB
docker.io/centos                       latest              3fa822599e10        2 weeks ago         203.5 MB
docker.io/hello-world                  latest              f2a91732366c        3 weeks ago         1.848 kB
[root@svr01 ~]# docker save 4f03dc990224 > /home/install/alpine-oraclejdk8.tar
[root@svr01 ~]# ls /home/install/
alpine-oraclejdk8.tar          hive                     MySQL-5.6.27-1.el7.x86_64.rpm-bundle.tar  openssl-1.0.2l.tar.gz  redis-3.2.9.tar.gz
apache-flume-1.7.0-bin.tar.gz  httpd-2.4.26.tar.gz      mysql-5.7.18-1.el7.x86_64.rpm-bundle.tar  pcre-8.41.tar.gz       zlib-1.2.11.tar.gz
hadoop                         memcached-1.4.39.tar.gz  nginx-1.12.0.tar.gz                       php-5.6.31.tar.gz      zookeeper-3.4.10.tar.gz

2.19 將磁盤的鏡像文件導入到docker中,而且經過 docker tag 修更名稱和tag

[root@svr01 ~]# docker load < /home/install/centos-v2.tar
e9613a472968: Loading layer [==================================================>]   105 MB/105 MB
Loaded image ID: sha256:11efb35f320cec46c83bc4dcbc184c8d45dcb3e369105251d70e2336fd261c92
[root@svr01 ~]# docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
<none>                                 <none>              11efb35f320c        5 hours ago         307.5 MB
docker.io/frolvlad/alpine-oraclejdk8   latest              4f03dc990224        12 days ago         170.1 MB
docker.io/centos                       latest              3fa822599e10        2 weeks ago         203.5 MB
docker.io/hello-world                  latest              f2a91732366c        3 weeks ago         1.848 kB
[root@svr01 ~]# docker tag 11efb35f320c docker.io/centos:v2
[root@svr01 ~]# docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos                       v2                  11efb35f320c        5 hours ago         307.5 MB
docker.io/frolvlad/alpine-oraclejdk8   latest              4f03dc990224        12 days ago         170.1 MB
docker.io/centos                       latest              3fa822599e10        2 weeks ago         203.5 MB
docker.io/hello-world                  latest              f2a91732366c        3 weeks ago         1.848 kB

2.20 驗證剛剛加載的鏡像可否成功運行,能進入的話,而且成功exit則導入成功

[root@svr01 ~]# docker images
REPOSITORY                             TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos                       v2                  11efb35f320c        5 hours ago         307.5 MB
docker.io/frolvlad/alpine-oraclejdk8   latest              4f03dc990224        12 days ago         170.1 MB
docker.io/centos                       latest              3fa822599e10        2 weeks ago         203.5 MB
docker.io/hello-world                  latest              f2a91732366c        3 weeks ago         1.848 kB
[root@svr01 ~]# docker run -it docker.io/centos:v2
[root@0a87728d1798 /]# ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@0a87728d1798 /]# exit
exit
[root@svr01 ~]#

3、下載地址

https://gitee.com/ylimhhmily/SpringCloudTutorial.gitpython

SpringCloudTutorial交流QQ羣: 235322432mysql

SpringCloudTutorial交流微信羣: 微信溝通羣二維碼圖片連接linux

歡迎關注,您的確定是對我最大的支持!!!nginx

相關文章
相關標籤/搜索