本文共 12307 字,大约阅读时间需要 41 分钟。
本机版本3.10.0-693.el7.x86_64 直接执行yum install docker -y centos7
若centos版本在centos6,则需要更新内核版本才可以安装docker。
使用docker pull Name[:TAG] 安装指定镜像的名称和版本号
安装centos最近版本的镜像,从docker默认镜像库进行下载 命令: sudo docker pull centosUsing default tag: latestTrying to pull repository docker.io/library/centos ... latest: Pulling from docker.io/library/centosd9aaf4d82f24: Pull complete Digest: sha256:eba772bac22c86d7d6e72421b4700c3f894ab6e35475a34014ff8de74c10872e创建成功之后,我们就开始使用该镜像创建一个容器,并在该容器中进行操作。
docker run -it centos bash[root@localhost sysconfig]# docker run -it centos bash [root@fc99abc4213f /]# lsanaconda-post.log dev home lib64 media opt root sbin sys usrbin 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 ms64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.062 ms64 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 2000msrtt min/avg/max/mdev = 0.062/0.083/0.105/0.019 ms[root@fc99abc4213f /]# exit exit
2.2.1 使用命令 docker images 查看当前镜像信息
[root@localhost sysconfig]# sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/redis 3.2 4ae3b93617bd 2 weeks ago 99.67 MBdocker.io/centos latest 196e0ce0c9fb 5 weeks ago 196.6 MBimages 子命令主要支持如下的选项:
(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 SIZEdocker.io/redis 3.2 4ae3b93617bd 2 weeks ago 99.67 MBmyredis 0.1 4ae3b93617bd 2 weeks ago 99.67 MBdocker.io/centos latest 196e0ce0c9fb 5 weeks ago 196.6 MB2.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
docker search redis -a 10
[root@localhost ~]# sudo docker search redis -s 10 Flag --stars has been deprecated, use --filter=stars=3 insteadINDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATEDdocker.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
删除镜像分为两种方式进行删除,分别为:
a.使用标签进行删除 使用标签进行删除时,如果存在由该镜像创建的多个标签的镜像,删除其中的一个并不会删除该镜像,只有该镜像只有唯一的一个标签时,删除该标签将会导致该镜像彻底删除。[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/redis 3.2 4ae3b93617bd 2 weeks ago 99.67 MBmyredis 0.2 4ae3b93617bd 2 weeks ago 99.67 MBmyredis 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.3Untagged: docker.io/redis@sha256:b15e3fabba806a6ee7f14774df0c2dc3036f752969bcdac022f0aa96d5cfc954[root@localhost ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/redis 3.2 4ae3b93617bd 2 weeks ago 99.67 MB[root@localhost ~]# docker rmi redisError response from daemon: No such image: redis:latest[root@localhost ~]# docker rmi redis:3.2Untagged: redis:3.2Deleted: sha256:4ae3b93617bdb7cc7559c021cd57fec2db465daf94e717b61282406b74493941Deleted: sha256:23434bfcd3a31cd975c6384253ba687fbcbd895b3e1a54af27824af9ed937591Deleted: sha256:0d30a0d1a42e43f5dc11264673fd4ee56a03095dbcd3da72924870de4df577aaDeleted: sha256:3a6d079caad238a31ea4283d4fc3f443d6e75bb1d3ce199cd916dc49627c5931Deleted: sha256:5b3c3d58e5f9d9460b356d01329f9016fbb1959bf1522f357fb81a2db362908bDeleted: sha256:a895b72388eb73dd9fb4406a318ee67fe48c7e70e01190c8f2ac4310dc529245Deleted: sha256:29d71372a4920ec230739a9e2317e7e9b18644edb10f78cde85df85e6ab85fc2[root@localhost ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEb.使用id进行删除
在删除镜像时,若该镜像创建了一个容器。且该容器存在时,则无法对该镜像进行删除。如若希望强制删除,则使用命令docker rmi -f [image][root@localhost ~]# docker rmi 196e0ce0c9fbError response from daemon: conflict: unable to delete 196e0ce0c9fb (must be forced) - image is being used by stopped container fc99abc4213f[root@localhost ~]# docker rmi -f 196e0ce0c9fbUntagged: docker.io/centos:latestDeleted: sha256:196e0ce0c9fbb31da595b893dd39bc9fd4aa78a474bbdc21459a3ebe855b7768[root@localhost ~]#
创建镜像主要有三种方式
a.使用容器创建镜像。[root@localhost ~]# docker run -it centos bashWARNING: 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创建镜像
镜像的写出
docker save -o centos.01.tar centos:01 镜像的载入 docker load –input centos.01.tar[augus@localhost ~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZEmycentos 01 a154ae24d66d 30 minutes ago 196.9 MBdocker.io/centos latest 196e0ce0c9fb 6 weeks ago 196.6 MB[augus@localhost ~]$ docker save -o mycentos.01.tar mycentos:01[augus@localhost ~]$ lsmycentos.01.tar precreated[augus@localhost ~]$ docker load --input mycentos.01.tar Loaded image: mycentos:01[augus@localhost ~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZEmycentos 01 a154ae24d66d 32 minutes ago 196.9 MBdocker.io/centos latest 196e0ce0c9fb 6 weeks ago 196.6 MB[augus@localhost ~]$ docker rmi mycentos:01 Untagged: mycentos:01Deleted: sha256:a154ae24d66d16201ba1bed9112e2cab11714644364b5bccef0d2ea2b1b3c443Deleted: sha256:5d58cde1df9c759667826244365052df54e7920391e72f8cdd1a3d1d35bc5bba[augus@localhost ~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZEdocker.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 kBLoaded image: mycentos:01[augus@localhost ~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZEmycentos 01 a154ae24d66d 33 minutes ago 196.9 MBdocker.io/centos latest 196e0ce0c9fb 6 weeks ago 196.6 MB[augus@localhost ~]$