镜像操作
镜像(image)是Docker三大核心概念中最为重要的,自Docker诞生之日起“镜像”就是相关社区最为热门的关键词。 Docker运行容器前需要本地存在对应的镜像,如果镜像没保存在本地,Docker会尝试先从默认镜像仓库下载(默认使用Docker Hub公共注册服务器的仓库),用户也可以通过配置,使用自定义的镜像仓库。
1、镜像搜索
通过docker search image_name命令搜索镜像。通常较为常用的参数有
--automated=true|false:仅显示自动创建的镜像,默认为否; --no-trunc=true|false:输出信息不截断显示,默认为否; -s, --stars=X:根据镜像的星级进行搜索
比如搜索centos镜像,星级为10或者以上。从左到右,显示依次为:镜像名、描述、星级、是否为官方、是否为自动创建。
2、拉取镜像
通过docker pull images_name:tag即可拉取需要的镜像。默认的tag(标签是latest)
如拉取一个httpd的镜像(这里我不指定tag标签,默认就是latest,会显示拉取的进度)
执行docker images可以查看到现有的镜像(里面有刚刚获取的httpd镜像)
3、查看镜像信息
通过docker inspect images_name可以查看某个镜像的信息
[root@centos7f ~]# docker inspect httpd [ { "Id": "sha256:11426a19f1a28d6491041aef1e1a7a2dcaa188d0165ae495de7d8fc1bf3e164f", "RepoTags": [ "httpd:latest" ], "RepoDigests": [ "httpd@sha256:8c84e065bdf72b4909bd55a348d5e91fe265e08d6b28ed9104bfdcac9206dcc8" ], "Parent": "", "Comment": "", "Created": "2018-07-31T16:49:48.488308431Z", "Container": "8d9716b97c51721cce2f7450eea9622ec9d97c8760098550f7f1cf8cddd94061", "ContainerConfig": { "Hostname": "8d9716b97c51", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "80/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/apache2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "HTTPD_PREFIX=/usr/local/apache2", "NGHTTP2_VERSION=1.18.1-1", "OPENSSL_VERSION=1.0.2l-1~bpo8+1", "HTTPD_VERSION=2.4.34", "HTTPD_SHA256=fa53c95631febb08a9de41fd2864cfff815cf62d9306723ab0d4b8d7aa1638f0", "HTTPD_PATCHES=", "APACHE_DIST_URLS=https://www.apache.org/dyn/closer.cgi?action=download&filename= \thttps://www-us.apache.org/dist/ \thttps://www.apache.org/dist/ \thttps://archive.apache.org/dist/" ], "Cmd": [ "/bin/sh", "-c", "#(nop) ", "CMD [\"httpd-foreground\"]" ], "ArgsEscaped": true, "Image": "sha256:c237180f28409d28401f1f634e784c940bb89d15e4de5db35ca5f320aed423b2", "Volumes": null, "WorkingDir": "/usr/local/apache2", "Entrypoint": null, "OnBuild": [], "Labels": {} }, "DockerVersion": "17.06.2-ce", "Author": "", "Config": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "80/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/apache2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "HTTPD_PREFIX=/usr/local/apache2", "NGHTTP2_VERSION=1.18.1-1", "OPENSSL_VERSION=1.0.2l-1~bpo8+1", "HTTPD_VERSION=2.4.34", "HTTPD_SHA256=fa53c95631febb08a9de41fd2864cfff815cf62d9306723ab0d4b8d7aa1638f0", "HTTPD_PATCHES=", "APACHE_DIST_URLS=https://www.apache.org/dyn/closer.cgi?action=download&filename= \thttps://www-us.apache.org/dist/ \thttps://www.apache.org/dist/ \thttps://archive.apache.org/dist/" ], "Cmd": [ "httpd-foreground" ], "ArgsEscaped": true, "Image": "sha256:c237180f28409d28401f1f634e784c940bb89d15e4de5db35ca5f320aed423b2", "Volumes": null, "WorkingDir": "/usr/local/apache2", "Entrypoint": null, "OnBuild": [], "Labels": null }, "Architecture": "amd64", "Os": "linux", "Size": 177559099, "VirtualSize": 177559099, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/e8a7d936a146f00002daad342da591eb4ff3c52b94361f62fe160949668df94f/diff:/var/lib/docker/overlay2/0497e7522cf60cc840d184f1f75a88e45fccedeb50f9685c72ba2f4e0334843b/diff:/var/lib/docker/overlay2/a71b868c28231bfcf73126a5df47157182b0414243f282a198770a22eea3713a/diff:/var/lib/docker/overlay2/e243b6e4b235b79969aff4fca1ba8521d1c7fc505c77d7d07d7ab52e95d960da/diff:/var/lib/docker/overlay2/dfa918fa4630779aaa319c0c833c9bf6b40d237e77a32b3e79574b843b78bff1/diff:/var/lib/docker/overlay2/5c0c1f37210838240c1e6aaa7a0ac2abee872450025d89df4a719cd2aeea21f4/diff", "MergedDir": "/var/lib/docker/overlay2/91f6e0aabf12bcdd5628447fabc919f0039f5e7bd7153382cad6ef413ccd5ed0/merged", "UpperDir": "/var/lib/docker/overlay2/91f6e0aabf12bcdd5628447fabc919f0039f5e7bd7153382cad6ef413ccd5ed0/diff", "WorkDir": "/var/lib/docker/overlay2/91f6e0aabf12bcdd5628447fabc919f0039f5e7bd7153382cad6ef413ccd5ed0/work" }, "Name": "overlay2" }, "RootFS": { "Type": "layers", "Layers": [ "sha256:dd1eb1fd7e08dc9bda0cbea31a89196c453cb218bea80ce64eeb19fadc98d262", "sha256:f6ff89d562dc5b83d425e3a8768a580f4f7cde78f4b73e1f0adb3fd417c0a846", "sha256:611673f0c323fcac4130e893672e60d6b53a6e7c5b5890139f734d7f6e9dd6bc", "sha256:74e07a1c6b076bad4d917bde97391a7a565409445ace712ff6f26ae4f04b5b33", "sha256:94519f51ae61e88862ce4cb142d17a2bf4cf2f4aa36b286e0933f423c1939cff", "sha256:dcaeabd68175a017bd22be178535663ba9576eca061067f252039523527f424b", "sha256:28eaf9ee6b41f5f72eecbef3f09474a496bcb5cfefd718a1865cf74733a6def0" ] }, "Metadata": { "LastTagTime": "0001-01-01T00:00:00Z" } } ]
注意:docker inspect输出的是json格式的信息
如:下面两条命令输出了httpd镜像开放了80端口、使用的是amd64的架构
4、删除镜像
使用docker rmi images_name命令
第一条命令没有执行成功,因为基于这个镜像上存在一个容器。通过-f选项可强制删除这个镜像,强制删除后,容器也将被删除,所以-f选项需谨慎使用。
5、创建一个镜像
commit方法:可根据一个容器创建一个镜像
下图中我首先启动了一个镜像,然后使用commit方法创建一个镜像叫httpd_server、tag是01。通过docker images可以查看到我创建的镜像
commit的参数:
-a指定创建镜像的author(作者)
-m提交消息
基于模板导入的方法
这里有很多docker的模板供下载http://download.openvz.org/template/precreated/(下载速度很慢,不是很建议这么做)
下载完了,可以执行
cat 模板 | docker import - images_name:tag
这样你就能通过模板创建了一个镜像。
书写Dockerfile创建模板(建议做法)
详细操作请关注后面的博客
6、存出和载入镜像
存出
通过docker save命令可以存出一个镜像,通过这个命令可以将同样的环境给开发、运维、测试使用,避免了一些因为工作环境带了的麻烦
载入
为了验证效果我将原有的jdeathe/centos-ssh删除了,然后载入刚刚存出的centos.tar。载入的命令为docker load < images.tar
7、上传镜像到docker hub
首先你需要注册有个docker hub的账号
执行docker login即可登录
想要上传到你的docker hub账户内,首先要给镜像打标签。记住标签打的是自己的hub账号名,然后跟上仓库名,tag。如果你的docker hub没有这个仓库,会自动创建该仓库。
然后直接push打了标签的镜像即可
这里给大家看一下结果
容器操作
查看你的容器
docker ps命令可以查看你当前运行的容器,而docker ps -a或者docker ps --all可以查看所有容器
创建容器
docker create images_name:tag可以创建一个容器。这里创建容器会随机给一个容器名称,很不利于管理,但是注意在创建容器时docker会返还给你一个容器的ID。
启动容器
docker start container_name/container_id
新建的同时启动容器
执行docker run images_name可以根据一个镜像创建一个容器并运行。
下图中,我用run命令基于jdeathe/centos-ssh运行了一个容器,然后让容器执行了echo "hello,I'm running"命令。然后容器会自动停止运行
守护态运行容器
我们在使用docker容器的时候大多时候都会让docker一直运行提供应用服务的,很少会让他运行执行一个命令就直接关闭。docker run -itd可以让容器在docker的回台运行,提供服务。
进入/连接容器
通过上面的命令让容器在后台运行了,所以连接容器执行命令是需要的
通过docker exec -it container_name /bin/bash 命令即可连接到正在运行的容器
显示容器的信息
[root@centos7f ~]# docker inspect 002b96bb30a9 [ { "Id": "002b96bb30a92e73b71b9430e9ddef168ed0e098a5a7bb460fb04b4ca0286c3b", "Created": "2018-08-18T13:31:18.963206183Z", "Path": "/bin/bash", "Args": [], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 114840, "ExitCode": 0, "Error": "", "StartedAt": "2018-08-18T13:31:19.305350229Z", "FinishedAt": "0001-01-01T00:00:00Z", "Health": { "Status": "unhealthy", "FailingStreak": 39, "Log": [ { "Start": "2018-08-18T21:31:40.692311294+08:00", "End": "2018-08-18T21:31:40.780482004+08:00", "ExitCode": 1, "Output": "" }, { "Start": "2018-08-18T21:31:41.301262289+08:00", "End": "2018-08-18T21:31:41.388515509+08:00", "ExitCode": 1, "Output": "" }, { "Start": "2018-08-18T21:31:41.910073234+08:00", "End": "2018-08-18T21:31:41.997176118+08:00", "ExitCode": 1, "Output": "" }, { "Start": "2018-08-18T21:31:42.518304445+08:00", "End": "2018-08-18T21:31:42.605189606+08:00", "ExitCode": 1, "Output": "" }, { "Start": "2018-08-18T21:31:43.127178763+08:00", "End": "2018-08-18T21:31:43.21354527+08:00", "ExitCode": 1, "Output": "" } ] } }, "Image": "sha256:563e45ee6e1243335785f558dbb566791e5ac5bd293aafc5c75e5b0743eaa888", "ResolvConfPath": "/var/lib/docker/containers/002b96bb30a92e73b71b9430e9ddef168ed0e098a5a7bb460fb04b4ca0286c3b/resolv.conf", "HostnamePath": "/var/lib/docker/containers/002b96bb30a92e73b71b9430e9ddef168ed0e098a5a7bb460fb04b4ca0286c3b/hostname", "HostsPath": "/var/lib/docker/containers/002b96bb30a92e73b71b9430e9ddef168ed0e098a5a7bb460fb04b4ca0286c3b/hosts", "LogPath": "/var/lib/docker/containers/002b96bb30a92e73b71b9430e9ddef168ed0e098a5a7bb460fb04b4ca0286c3b/002b96bb30a92e73b71b9430e9ddef168ed0e098a5a7bb460fb04b4ca0286c3b-json.log", "Name": "/cocky_ptolemy", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": [ "02039efcbf2bb4b4b8f1a88187144ef13fe3873035efe13dff00b3afcdeb1059" ], "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "shareable", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DiskQuota": 0, "KernelMemory": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": 0, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "MaskedPaths": [ "/proc/acpi", "/proc/kcore", "/proc/keys", "/proc/latency_stats", "/proc/timer_list", "/proc/timer_stats", "/proc/sched_debug", "/proc/scsi", "/sys/firmware" ], "ReadonlyPaths": [ "/proc/asound", "/proc/bus", "/proc/fs", "/proc/irq", "/proc/sys", "/proc/sysrq-trigger" ] }, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/3cda10e35c399bd1362edc91b9d1ae9b27a76c198aaf9407b06fe3ebc521d200-init/diff:/var/lib/docker/overlay2/53a5b0930c3c8500d36135247d0820d14ab9249c07df1e745dc61fdf9829f638/diff:/var/lib/docker/overlay2/c8e5e31d4c76e171e86a8ac1b88d7a76798d6588b45c9eee8f9430fa0eb72681/diff:/var/lib/docker/overlay2/6b67f4e966ab81cee54f1c2b2438ca1dcb49aaa600fa724acdf9fc2b32e2ad36/diff:/var/lib/docker/overlay2/27fa1c8e7cfc942ee5df37b210ce9020ef9dc1439bd114e915183e0a8a731b6a/diff:/var/lib/docker/overlay2/5b946c5b08c008f219caa0703770d3a0bae213b07346a30d00db4c3aaaaec3d0/diff:/var/lib/docker/overlay2/2f939ad9a322e1471b17693fa89936d341a04359dbc5e2ac155e888b15b5e0e9/diff:/var/lib/docker/overlay2/f47a7753c7d4889dc79fa7edf005733d816eafcdbb60d83f6e1fcb551117e408/diff:/var/lib/docker/overlay2/e5af324a45dc5c414841328ef55e3ee3aed7738c100de81a06887c9abfae43e8/diff:/var/lib/docker/overlay2/b992eefb82fb8035c96de66a66def764b09c8a24b4b977817cf6545476a6cc07/diff:/var/lib/docker/overlay2/b97cd8b38d9f15c08bcd4c1589e8a0c8f8d617429986d61713a7b96183cdcc25/diff:/var/lib/docker/overlay2/392aa202ea4b1eb42b229082ee607d827cc42933e2507f5dc50f6d67f84f9331/diff:/var/lib/docker/overlay2/bbb0e0b3b6a8da11ffb6c7825f87f87b18cf3310a7ab1d64020abb037c6d4807/diff:/var/lib/docker/overlay2/16d8286164ccebe3b1b43d266519ced560165833e33bbc49150ff4384306e39a/diff:/var/lib/docker/overlay2/b3ba97cbdaac36a329b4349e13a7875de424b24363b39f86ac84883db5199a2d/diff", "MergedDir": "/var/lib/docker/overlay2/3cda10e35c399bd1362edc91b9d1ae9b27a76c198aaf9407b06fe3ebc521d200/merged", "UpperDir": "/var/lib/docker/overlay2/3cda10e35c399bd1362edc91b9d1ae9b27a76c198aaf9407b06fe3ebc521d200/diff", "WorkDir": "/var/lib/docker/overlay2/3cda10e35c399bd1362edc91b9d1ae9b27a76c198aaf9407b06fe3ebc521d200/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "002b96bb30a9", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "22/tcp": {} }, "Tty": true, "OpenStdin": true, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "SSH_AUTHORIZED_KEYS=", "SSH_AUTOSTART_SSHD=true", "SSH_AUTOSTART_SSHD_BOOTSTRAP=true", "SSH_CHROOT_DIRECTORY=%h", "SSH_INHERIT_ENVIRONMENT=false", "SSH_SUDO=ALL=(ALL) ALL", "SSH_USER=app-admin", "SSH_USER_FORCE_SFTP=false", "SSH_USER_HOME=/home/%u", "SSH_USER_ID=500:500", "SSH_USER_PASSWORD=", "SSH_USER_PASSWORD_HASHED=false", "SSH_USER_SHELL=/bin/bash" ], "Cmd": [ "/bin/bash" ], "Healthcheck": { "Test": [ "CMD", "/usr/bin/healthcheck" ], "Interval": 500000000, "Timeout": 1000000000, "Retries": 5 }, "Image": "jdeathe/centos-ssh", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "build-date": "20170911", "install": "docker run --rm --privileged --volume /:/media/root jdeathe/centos-ssh:2.3.2 /usr/sbin/scmi install --chroot=/media/root --name=${NAME} --tag=2.3.2 --setopt='--volume {{NAME}}.config-ssh:/etc/ssh'", "license": "GPLv2", "maintainer": "James Deathe <james.deathe@gmail.com>", "name": "CentOS Base Image", "org.deathe.description": "CentOS-7 7.4.1708 x86_64 - SCL, EPEL and IUS Repositories / Supervisor / OpenSSH.", "org.deathe.license": "MIT", "org.deathe.name": "centos-ssh", "org.deathe.release": "jdeathe/centos-ssh:2.3.2", "org.deathe.url": "https://github.com/jdeathe/centos-ssh", "org.deathe.vendor": "jdeathe", "org.deathe.version": "2.3.2", "uninstall": "docker run --rm --privileged --volume /:/media/root jdeathe/centos-ssh:2.3.2 /usr/sbin/scmi uninstall --chroot=/media/root --name=${NAME} --tag=2.3.2 --setopt='--volume {{NAME}}.config-ssh:/etc/ssh'", "vendor": "CentOS" } }, "NetworkSettings": { "Bridge": "", "SandboxID": "a28e769b9df4b9bba2c502c27a363bf69c0dc0127186d4287325239ef60cd2ed", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "22/tcp": null }, "SandboxKey": "/var/run/docker/netns/a28e769b9df4", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "7458b19a71f455028c46a21b9b723639bea102556b1aa2e69a0964af2556671a", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "7d4f6f5d62534239375305abc5e17709845a269f4dbb52d36b115b559963f594", "EndpointID": "7458b19a71f455028c46a21b9b723639bea102556b1aa2e69a0964af2556671a", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02", "DriverOpts": null } } } } ]
导出导入容器
首先导出一个容器
将容器导入到镜像
删除一个容器
通过docker rm container_name可以删除一个未运行的镜像,加上-f选项可以强制删除一个运行的镜像。
常用操作方法小结
1、docker run -itd --name centos001 jdeathe/centos-ssh /bin/bash
该命令可以让一个容器后台运行并未这个起名叫做centos001。
2、docker run -it --name centos002
该命令可以运行一个容器,但是不是后台守护态。所以会直接连接进入该容器,可直接进行操作,直接执行exit退出该容器后,该容器就会停止。如果想退出,但是不停止容器,可以执行ctrl+p,然后紧跟着执行ctrl+q,让容器在退出后保持运行。