docker入門1:使用docker映象

千狼發表於2017-10-26
版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/qq_26654727/article/details/78360027

持續更新中……….

1.安裝docker

centos7 核心版本在3.10.0-693.el7.x86_64之上的直接執行: yum install docker -y 即可

本機版本3.10.0-693.el7.x86_64 直接執行yum install docker -y centos7
若centos版本在centos6,則需要更新核心版本才可以安裝docker。

2.使用docker映象

2.1 安裝docker映象

使用docker pull Name[:TAG] 安裝指定映象的名稱和版本號
安裝centos最近版本的映象,從docker預設映象庫進行下載 命令: sudo docker pull centos

Using default tag: latest
Trying to pull repository docker.io/library/centos ... 
latest: Pulling from docker.io/library/centos
d9aaf4d82f24: Pull complete 
Digest: sha256:eba772bac22c86d7d6e72421b4700c3f894ab6e35475a34014ff8de74c10872e

建立成功之後,我們就開始使用該映象建立一個容器,並在該容器中進行操作。
docker run -it centos bash

[root@localhost sysconfig]# docker run -it centos bash 
[root@fc99abc4213f /]# ls
anaconda-post.log  dev  home  lib64       media  opt   root  sbin  sys  usr
bin                etc  lib   lost+found  mnt    proc  run   srv   tmp  var
[root@fc99abc4213f /]# ping localhost 
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.105 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.062 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.083 ms
^C
--- localhost ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.062/0.083/0.105/0.019 ms
[root@fc99abc4213f /]# exit 
exit
2.2 檢視映象資訊

2.2.1 使用命令 docker images 檢視當前映象資訊

[root@localhost sysconfig]# sudo docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/redis     3.2                 4ae3b93617bd        2 weeks ago         99.67 MB
docker.io/centos    latest              196e0ce0c9fb        5 weeks ago         196.6 MB

images 子命令主要支援如下的選項:
(1) -a ,–all=true|false 列出所有的映象檔案
(2) –digests=true|false 列出映象的數字摘要值,預設為否
(3) -f –filter=[] 過濾列出的映象

2.2.2 使用tag命令新增映象標籤

[root@localhost sysconfig]# docker tag redis:3.2 myredis:0.1
[root@localhost sysconfig]# sudo docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/redis     3.2                 4ae3b93617bd        2 weeks ago         99.67 MB
myredis             0.1                 4ae3b93617bd        2 weeks ago         99.67 MB
docker.io/centos    latest              196e0ce0c9fb        5 weeks ago         196.6 MB

2.2.3 使用inspect 命令檢視詳細資訊
檢視詳細資訊

[root@localhost sysconfig]# docker inspect myredis:0.1
[
    {
        "Id": "sha256:4ae3b93617bdb7cc7559c021cd57fec2db465daf94e717b61282406b74493941",
      "RepoTags": [
          "docker.io/redis:3.2",
          "myredis:0.1"
      ],
      "RepoDigests": [
          "docker.io/redis@sha256:b15e3fabba806a6ee7f14774df0c2dc3036f752969bcdac022f0aa96d5cfc954"
       ],
       "Parent": "",
       "Comment": "",
       "Created": "2017-10-10T02:50:23.955973925Z",
       "Container": "13285f8661db6019c0916f3252c76c17bad6508905a319358c85f7acf7967365",
       "ContainerConfig": {
           "Hostname": "13285f8661db",
           "Domainname": "",
           "User": "",
           "AttachStdin": false,
           "AttachStdout": false,
           "AttachStderr": false,
           "ExposedPorts": {
               "6379/tcp": {}
           },
           "Tty": false,
           "OpenStdin": false,
           "StdinOnce": false,
           "Env": [
               "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.10",
                "REDIS_VERSION=3.2.11",
               "REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-3.2.11.tar.gz",
               "REDIS_DOWNLOAD_SHA=31ae927cab09f90c9ca5954aab7aeecc3bb4da6087d3d12ba0a929ceb54081b5"
           ],
           "Cmd": [
               "/bin/sh",
               "-c",
               "#(nop) ",
               "CMD ["redis-server"]"
           ],
           "ArgsEscaped": true,
           "Image": "sha256:0d4214b1bf00a587daa1d4f36421bd99e4b2c316249280675bec34faaa6d6e0d",
           "Volumes": {
               "/data": {}
           },
           "WorkingDir": "/data",
           "Entrypoint": [
               "docker-entrypoint.sh"
           ],
           "OnBuild": [],
           "Labels": {}
       },
       "DockerVersion": "17.06.2-ce",
       "Author": "",
       "Config": {
           "Hostname": "",
           "Domainname": "",
           "User": "",
           "AttachStdin": false,
           "AttachStdout": false,
           "AttachStderr": false,
           "ExposedPorts": {
               "6379/tcp": {}
           },
           "Tty": false,
           "OpenStdin": false,
           "StdinOnce": false,
           "Env": [
               "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
               "GOSU_VERSION=1.10",
               "REDIS_VERSION=3.2.11",
               "REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-3.2.11.tar.gz",
               "REDIS_DOWNLOAD_SHA=31ae927cab09f90c9ca5954aab7aeecc3bb4da6087d3d12ba0a929ceb54081b5"
           ],
           "Cmd": [
               "redis-server"
           ],
           "ArgsEscaped": true,
           "Image": "sha256:0d4214b1bf00a587daa1d4f36421bd99e4b2c316249280675bec34faaa6d6e0d",
           "Volumes": {
               "/data": {}
           },
           "WorkingDir": "/data",
           "Entrypoint": [
               "docker-entrypoint.sh"
           ],
           "OnBuild": [],
           "Labels": null
       },
       "Architecture": "amd64",
       "Os": "linux",
       "Size": 99668649,
       "VirtualSize": 99668649,
       "GraphDriver": {
           "Name": "devicemapper",
           "Data": {
               "DeviceId": "10",
               "DeviceName": "docker-253:0-33690427-dea7d2d0030dc44a3edf0fcc80077d715d5ccf4825a3db161da84436cf1dcdc7",
               "DeviceSize": "10737418240"
           }
       },
       "RootFS": {
           "Type": "layers",
           "Layers": [
               "sha256:29d71372a4920ec230739a9e2317e7e9b18644edb10f78cde85df85e6ab85fc2",
               "sha256:f5ccc3ab98cc45041bcf1f2cf49afb7e5046316af795c88ef6be50ed149cc3a4",
               "sha256:3fae9b7c819afb850f999670dc88cc3f646a146c379103c8947df99c03498ebe",
               "sha256:7044a5153c6481a7284e181703432930f392aa39fb12982c9a2d8cb2f2448cb0",
               "sha256:7768d1f84ecca49f4ca1005047f7d5d8a3a009dfb3e0213cdbbb3856f7e4c115",
               "sha256:327ce591d4be258dd33151003eebd5cc362fd6caed83f9a5512b7970a8f5facb"
           ]
       }
   }
]

檢視某一個引數資訊 “docker inspect -f {{“.Os”}} myredis:0.1 ”
“`
[root@localhost sysconfig]# docker inspect -f {{“.Os”}} myredis:0.1
linux
[root@localhost sysconfig]# docker inspect -f {{“.Size”}} myredis:0.1
99668649

2.3搜尋映象

docker search redis -a 10

[root@localhost ~]# sudo docker search redis -s 10 
Flag --stars has been deprecated, use --filter=stars=3 instead
INDEX       NAME                                        DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/redis                             Redis is an open source key-value store th...   4359      [OK]       
docker.io   docker.io/bitnami/redis                     Bitnami Redis Docker Image                      59                   [OK]
docker.io   docker.io/sameersbn/redis                                                                   59                   [OK]
docker.io   docker.io/tenstartups/redis-commander                                                       29                   [OK]
docker.io   docker.io/kubeguide/redis-master            redis-master with "Hello World!"                19                   
docker.io   docker.io/joshula/redis-sentinel            A container for Redis Sentinel                  18                   
docker.io   docker.io/kubeguide/guestbook-redis-slave   Guestbook redis slave                           14                   
docker.io   docker.io/tutum/redis                       Base docker image to run a Redis server         10     

2.4刪除映象

刪除映象分為兩種方式進行刪除,分別為:
a.使用標籤進行刪除
使用標籤進行刪除時,如果存在由該映象建立的多個標籤的映象,刪除其中的一個並不會刪除該映象,只有該映象只有唯一的一個標籤時,刪除該標籤將會導致該映象徹底刪除。

[root@localhost ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/redis     3.2                 4ae3b93617bd        2 weeks ago         99.67 MB
myredis             0.2                 4ae3b93617bd        2 weeks ago         99.67 MB
myredis             0.3                 4ae3b93617bd        2 weeks ago         99.67 MB
[root@localhost ~]# docker rmi myredis:0.2 
Untagged: myredis:0.2
[root@localhost ~]# docker rmi myredis:0.3 
Untagged: myredis:0.3
Untagged: docker.io/redis@sha256:b15e3fabba806a6ee7f14774df0c2dc3036f752969bcdac022f0aa96d5cfc954
[root@localhost ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/redis     3.2                 4ae3b93617bd        2 weeks ago         99.67 MB
[root@localhost ~]# docker rmi redis
Error response from daemon: No such image: redis:latest
[root@localhost ~]# docker rmi redis:3.2
Untagged: redis:3.2
Deleted: sha256:4ae3b93617bdb7cc7559c021cd57fec2db465daf94e717b61282406b74493941
Deleted: sha256:23434bfcd3a31cd975c6384253ba687fbcbd895b3e1a54af27824af9ed937591
Deleted: sha256:0d30a0d1a42e43f5dc11264673fd4ee56a03095dbcd3da72924870de4df577aa
Deleted: sha256:3a6d079caad238a31ea4283d4fc3f443d6e75bb1d3ce199cd916dc49627c5931
Deleted: sha256:5b3c3d58e5f9d9460b356d01329f9016fbb1959bf1522f357fb81a2db362908b
Deleted: sha256:a895b72388eb73dd9fb4406a318ee67fe48c7e70e01190c8f2ac4310dc529245
Deleted: sha256:29d71372a4920ec230739a9e2317e7e9b18644edb10f78cde85df85e6ab85fc2
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

b.使用id進行刪除
在刪除映象時,若該映象建立了一個容器。且該容器存在時,則無法對該映象進行刪除。如若希望強制刪除,則使用命令docker rmi -f [image]

[root@localhost ~]# docker rmi 196e0ce0c9fb
Error response from daemon: conflict: unable to delete 196e0ce0c9fb (must be forced) - image is being used by stopped container fc99abc4213f
[root@localhost ~]# docker rmi -f 196e0ce0c9fb
Untagged: docker.io/centos:latest
Deleted: sha256:196e0ce0c9fbb31da595b893dd39bc9fd4aa78a474bbdc21459a3ebe855b7768
[root@localhost ~]# 

2.5建立映象

建立映象主要有三種方式
a.使用容器建立映象。

[root@localhost ~]# docker run -it centos bash
WARNING: IPv4 forwarding is disabled. Networking will not work.
[root@1ed5befe7051 /]# useradd nrs 
[root@1ed5befe7051 /]# exit  
exit
[root@localhost ~]# docker commit -m "add user nrs" -a "augus" 1ed5befe7051 mycentos:01 
sha256:a154ae24d66d16201ba1bed9112e2cab11714644364b5bccef0d2ea2b1b3c443
[root@localhost ~]# 

b.使用本地模板建立映象
c.使用dockerFile建立映象

2.6映象的載入和寫出

映象的寫出
docker save -o centos.01.tar centos:01
映象的載入
docker load –input centos.01.tar

[augus@localhost ~]$ docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mycentos            01                  a154ae24d66d        30 minutes ago      196.9 MB
docker.io/centos    latest              196e0ce0c9fb        6 weeks ago         196.6 MB
[augus@localhost ~]$  docker save -o mycentos.01.tar mycentos:01
[augus@localhost ~]$ ls
mycentos.01.tar  precreated
[augus@localhost ~]$ docker load --input mycentos.01.tar 
Loaded image: mycentos:01
[augus@localhost ~]$ docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mycentos            01                  a154ae24d66d        32 minutes ago      196.9 MB
docker.io/centos    latest              196e0ce0c9fb        6 weeks ago         196.6 MB
[augus@localhost ~]$ docker rmi mycentos:01 
Untagged: mycentos:01
Deleted: sha256:a154ae24d66d16201ba1bed9112e2cab11714644364b5bccef0d2ea2b1b3c443
Deleted: sha256:5d58cde1df9c759667826244365052df54e7920391e72f8cdd1a3d1d35bc5bba
[augus@localhost ~]$ docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/centos    latest              196e0ce0c9fb        6 weeks ago         196.6 MB
[augus@localhost ~]$ docker load --input mycentos.01.tar 
edfd6667dbbb: Loading layer [==================================================>] 312.8 kB/312.8 kB
Loaded image: mycentos:01
[augus@localhost ~]$ docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mycentos            01                  a154ae24d66d        33 minutes ago      196.9 MB
docker.io/centos    latest              196e0ce0c9fb        6 weeks ago         196.6 MB
[augus@localhost ~]$ 


相關文章