繼續學習入門知識。前面已經安裝了dokcer,如今來看看 docker image的相關命令nginx
[ec2-user@ip-172-16-1-4 ~]$ sudo docker image Usage: docker image COMMAND Manage images Commands: build Build an image from a Dockerfile history Show the history of an image import Import the contents from a tarball to create a filesystem image inspect Display detailed information on one or more images load Load an image from a tar archive or STDIN ls List images prune Remove unused images pull Pull an image or a repository from a registry push Push an image or a repository to a registry rm Remove one or more images save Save one or more images to a tar archive (streamed to STDOUT by default) tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
下面按順序簡單的過一遍docker
build是建立鏡像,這個後面會有具體介紹bash
history 能夠看見鏡像的歷史。docker的鏡像是一個多層的結構,好比說,這個裏面的結構其實都是咱們在建立docker鏡像到時候輸入的命令tcp
127MB [ec2-user@ip-172-16-1-4 ~]$ sudo docker image history nginx IMAGE CREATED CREATED BY SIZE COMMENT 8cf1bfb43ff5 2 days ago /bin/sh -c #(nop) CMD ["nginx" "-g" "da emon… 0B <missing> 2 days ago /bin/sh -c #(nop) STOPSIGNAL SIGTERM 0B <missing> 2 days ago /bin/sh -c #(nop) EXPOSE 80 0B <missing> 2 days ago /bin/sh -c #(nop) ENTRYPOINT ["/docker- entr… 0B <missing> 2 days ago /bin/sh -c #(nop) COPY file:0fd5fca330dc d6a7… 1.04kB <missing> 2 days ago /bin/sh -c #(nop) COPY file:1d0a4127e78a 26c1… 1.96kB <missing> 2 days ago /bin/sh -c #(nop) COPY file:e7e183879c35 719c… 1.2kB <missing> 2 days ago /bin/sh -c set -x && addgroup --syst em -… 63.3MB <missing> 2 days ago /bin/sh -c #(nop) ENV PKG_RELEASE=1~bus ter 0B <missing> 2 days ago /bin/sh -c #(nop) ENV NJS_VERSION=0.4.2 0B <missing> 2 days ago /bin/sh -c #(nop) ENV NGINX_VERSION=1.1 9.1 0B <missing> 2 days ago /bin/sh -c #(nop) LABEL maintainer=NGIN X Do… 0B <missing> 2 days ago /bin/sh -c #(nop) CMD ["bash"] 0B <missing> 2 days ago /bin/sh -c #(nop) ADD file:6ccb3bbcc69b0 d44c… 69.2MB
import 是從一個tar文件裏面導入容器的文件系統來生成一個鏡像,對應的咱們能夠從docker container的命令裏面export一個容器的文件系統到一個tar文件裏面。ide
inspect 是一個通用的命令, 主要是顯示一些常見docker對象的信息學習
[root@ip-172-16-1-4 ec2-user]# docker inspect --help Usage: docker inspect [OPTIONS] NAME|ID [NAME|ID...] Return low-level information on Docker objects Options: -f, --format string Format the output using the given Go template -s, --size Display total file sizes if the type is container --type string Return JSON for specified type
好比 我查看一下nginx這個鏡像的信息ui
root@ip-172-16-1-4 ec2-user]# docker inspect nginx [ { "Id": "sha256:8cf1bfb43ff5d9b05af9b6b63983440f137c6a08320fa7592197c1474ef30241", "RepoTags": [ "nginx:latest" ], "RepoDigests": [ "nginx@sha256:0e188877aa60537d1a1c6484b8c3929cfe09988145327ee47e8e91ddf6f76f5c" ], "Parent": "", "Comment": "", "Created": "2020-07-22T03:23:26.691138735Z", "Container": "072f077fe42b39ffb92ca368fa5d79be975faa97b33bffb5b5383eb6abfb4838", "ContainerConfig": { "Hostname": "072f077fe42b", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "80/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "NGINX_VERSION=1.19.1", "NJS_VERSION=0.4.2", "PKG_RELEASE=1~buster" ], "Cmd": [ "/bin/sh",
ls 就是顯示當前有哪些鏡像spa
[root@ip-172-16-1-4 ec2-user]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 8cf1bfb43ff5 2 days ago 132MB nginx 1.17 9beeba249f3e 2 months ago 127MB
save 和load 相對應,一個是把鏡像和相關的資源打包到一個tar文件裏面,一個是把這些資源再加載回來。他們的使用場景能夠用於備份,或者把鏡像拷貝到其餘地方使用。3d
[root@ip-172-16-1-4 ec2-user]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 8cf1bfb43ff5 2 days ago 132MB nginx 1.17 9beeba249f3e 2 months ago 127MB [root@ip-172-16-1-4 ec2-user]# docker image save 8cf1bfb43ff5 > nginx.tar [root@ip-172-16-1-4 ec2-user]# du nginx.tar -h 131M nginx.tar
生成以後,我來嘗試刪除一下現有的鏡像,他會提示報錯,由於我有個容器關聯着這個鏡像code
[root@ip-172-16-1-4 ec2-user]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 8cf1bfb43ff5 2 days ago 132MB nginx 1.17 9beeba249f3e 2 months ago 127MB [root@ip-172-16-1-4 ec2-user]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 34eef288fd4b nginx "/docker-entrypoint.…" 23 hours ago Up 23 hours 0.0.0.0:8080->80/tcp focused_grothendieck [root@ip-172-16-1-4 ec2-user]# docker image rm 8cf1bfb43ff5 Error response from daemon: conflict: unable to delete 8cf1bfb43ff5 (cannot be forced) - image is being used by running container 34eef288fd4b [root@ip-172-16-1-4 ec2-user]#
強制關掉正在運行的容器
[root@ip-172-16-1-4 ec2-user]# docker stop $(docker ps -a -q) 34eef288fd4b
如今就能夠刪除鏡像了
[root@ip-172-16-1-4 ec2-user]# docker image rm 8cf1bfb43ff5 Error response from daemon: conflict: unable to delete 8cf1bfb43ff5 (must be forced) - image is being used by stopped container 34eef288fd4b [root@ip-172-16-1-4 ec2-user]# docker image rm 8cf1bfb43ff5 -f Untagged: nginx:latest Untagged: nginx@sha256:0e188877aa60537d1a1c6484b8c3929cfe09988145327ee47e8e91ddf6f76f5c Deleted: sha256:8cf1bfb43ff5d9b05af9b6b63983440f137c6a08320fa7592197c1474ef30241 [root@ip-172-16-1-4 ec2-user]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx 1.17 9beeba249f3e 2 months ago 127MB
pull 和 push 相對,一個是下載鏡像,一個是上傳鏡像。當咱們直接調用 docker run的時候,他會嘗試從本地找對應的鏡像來建立容器,若是本地沒有,那麼會自動下載。固然咱們也能夠 用 pull先下載以後,在建立容器。
[root@ip-172-16-1-4 ec2-user]# docker image pull nginx Using default tag: latest latest: Pulling from library/nginx 6ec8c9369e08: Already exists d3cb09a117e5: Already exists 7ef2f1459687: Already exists e4d1bf8c9482: Already exists 795301d236d7: Already exists Digest: sha256:0e188877aa60537d1a1c6484b8c3929cfe09988145327ee47e8e91ddf6f76f5c Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest
prune
這個單詞的本意是修剪枝葉,他的做用也差很少。 有的時候,鏡像可能會出現問題,致使名字變成none,這樣的鏡像是有問題的,prune能夠直接刪掉。有的時候,鏡像沒有鏈接到任何容器,爲了節約空間,咱們也能夠把這些鏡像自動批量刪掉。
[root@ip-172-16-1-4 ec2-user]# docker image prune -a WARNING! This will remove all images without at least one container associated to them. Are you sure you want to continue? [y/N] y Deleted Images: untagged: nginx:1.17 untagged: nginx@sha256:6fff55753e3b34e36e24e37039ee9eae1fe38a6420d8ae16ef37c92d1eb26699 deleted: sha256:9beeba249f3ee158d3e495a6ac25c5667ae2de8a43ac2a8bfd2bf687a58c06c9 deleted: sha256:8fb6373b4cca3383756d7fd7843dd92f95827e5f2913609e09a9621dcddb3752 deleted: sha256:8b09841626797a03a9fe5e73aa38aeacf9ff0ce85a3004236ff35234eec3b35c deleted: sha256:ffc9b21953f4cd7956cdf532a5db04ff0a2daa7475ad796f1bad58cfbaf77a07 Total reclaimed space: 126.8MB