鏡像操做python
鏡像(image)是Docker三大核心概念中最爲重要的,自Docker誕生之日起「鏡像」就是相關社區最爲熱門的關鍵詞。 Docker運行容器前須要本地存在對應的鏡像,若是鏡像沒保存在本地,Docker會嘗試先從默認鏡像倉庫下載(默認使用Docker Hub公共註冊服務器的倉庫),用戶也能夠經過配置,使用自定義的鏡像倉庫。linux
一、鏡像搜索
git
經過docker search image_name命令搜索鏡像。一般較爲經常使用的參數有
github
--automated=true|false:僅顯示自動建立的鏡像,默認爲否; --no-trunc=true|false:輸出信息不截斷顯示,默認爲否; -s, --stars=X:根據鏡像的星級進行搜索
好比搜索centos鏡像,星級爲10或者以上。從左到右,顯示依次爲:鏡像名、描述、星級、是否爲官方、是否爲自動建立。docker
二、拉取鏡像
apache
經過docker pull images_name:tag便可拉取須要的鏡像。默認的tag(標籤是latest)json
如拉取一個httpd的鏡像(這裏我不指定tag標籤,默認就是latest,會顯示拉取的進度)centos
執行docker images能夠查看到現有的鏡像(裏面有剛剛獲取的httpd鏡像)
bash
三、查看鏡像信息服務器
經過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的架構
四、刪除鏡像
使用docker rmi images_name命令
第一條命令沒有執行成功,由於基於這個鏡像上存在一個容器。經過-f選項可強制刪除這個鏡像,強制刪除後,容器也將被刪除,因此-f選項需謹慎使用。
五、建立一個鏡像
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建立模板(建議作法)
詳細操做請關注後面的博客
六、存出和載入鏡像
存出
經過docker save命令能夠存出一個鏡像,經過這個命令能夠將一樣的環境給開發、運維、測試使用,避免了一些由於工做環境帶了的麻煩
載入
爲了驗證效果我將原有的jdeathe/centos-ssh刪除了,而後載入剛剛存出的centos.tar。載入的命令爲docker load < images.tar
七、上傳鏡像到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選項能夠強制刪除一個運行的鏡像。
經常使用操做方法小結
一、docker run -itd --name centos001 jdeathe/centos-ssh /bin/bash
該命令可讓一個容器後臺運行並未這個起名叫作centos001。
二、docker run -it --name centos002
該命令能夠運行一個容器,可是不是後臺守護態。因此會直接鏈接進入該容器,可直接進行操做,直接執行exit退出該容器後,該容器就會中止。若是想退出,可是不中止容器,能夠執行ctrl+p,而後緊跟着執行ctrl+q,讓容器在退出後保持運行。