瞭解鏡像、容器等在使用時的機制:node
docker run hello-worldpython
本地先看有沒有hello-world的容器;若是沒有該容器,再看本地有沒有hello-world的鏡像;若是沒有該鏡像,就到docker倉庫中去pull一個鏡像到本地。而後,以該鏡像爲模板產生容器實例運行。linux
VM和容器的差異,回憶一下:git
查看docker的版本信息。經常用來驗證docker是否安裝成功。github
neil@linux-famw:~> docker version Client: Version: 18.06.1-ce API version: 1.38 Go version: go1.10.7 Git commit: e68fc7a215d7 Built: Tue Dec 18 12:00:00 2018 OS/Arch: linux/amd64 Experimental: false Server: Engine: Version: 18.06.1-ce API version: 1.38 (minimum version 1.12) Go version: go1.10.7 Git commit: e68fc7a215d7 Built: Tue Dec 18 12:00:00 2018 OS/Arch: linux/amd64 Experimental: false neil@linux-famw:~>
(本文出自oschina博主happyBKs的博文:https://my.oschina.net/happyBKs/blog/3011159)redis
查看docker容器等的相關信息,如正在運行的容器實例的個數,暫停的個數,一共有多少images。docker
neil@linux-famw:~> docker info Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 2 Server Version: 18.06.1-ce Storage Driver: btrfs Build Version: Btrfs v4.15 Library Version: 102 Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog Swarm: inactive Runtimes: oci runc Default Runtime: runc Init Binary: docker-init containerd version: 468a545b9edcd5932818eb9de8e72413e616e86e runc version: 69663f0bd4b60df09991c08812a60108003fa340 init version: v0.1.3_catatonit (expected: fec3683b971d9c3ef73f284f176672c44b448662) Security Options: apparmor seccomp Profile: default Kernel Version: 4.12.14-lp150.12.45-default Operating System: openSUSE Leap 15.0 OSType: linux Architecture: x86_64 CPUs: 8 Total Memory: 7.66GiB Name: linux-famw ID: 6F47:5K4Z:Q6YA:X3FD:K736:I2VN:HUJ3:CU4M:CK5S:5YCO:XI4J:ZEEF Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false WARNING: No swap limit support neil@linux-famw:~>
查詢docker的命令幫助shell
neil@linux-famw:~> docker --help Usage: docker [OPTIONS] COMMAND A self-sufficient runtime for containers Options: --config string Location of client config files (default "/home/neil/.docker") -D, --debug Enable debug mode -H, --host list Daemon socket(s) to connect to -l, --log-level string Set the logging level ("debug"|"info"|"warn"|"error"|"fatal") (default "info") --tls Use TLS; implied by --tlsverify --tlscacert string Trust certs signed only by this CA (default "/home/neil/.docker/ca.pem") --tlscert string Path to TLS certificate file (default "/home/neil/.docker/cert.pem") --tlskey string Path to TLS key file (default "/home/neil/.docker/key.pem") --tlsverify Use TLS and verify the remote -v, --version Print version information and quit Management Commands: config Manage Docker configs container Manage containers image Manage images network Manage networks node Manage Swarm nodes plugin Manage plugins secret Manage Docker secrets service Manage services stack Manage Docker stacks swarm Manage Swarm system Manage Docker trust Manage trust on Docker images volume Manage volumes Commands: attach Attach local standard input, output, and error streams to a running container build Build an image from a Dockerfile commit Create a new image from a container's changes cp Copy files/folders between a container and the local filesystem create Create a new container diff Inspect changes to files or directories on a container's filesystem events Get real time events from the server exec Run a command in a running container export Export a container's filesystem as a tar archive history Show the history of an image images List images import Import the contents from a tarball to create a filesystem image info Display system-wide information inspect Return low-level information on Docker objects kill Kill one or more running containers load Load an image from a tar archive or STDIN login Log in to a Docker registry logout Log out from a Docker registry logs Fetch the logs of a container pause Pause all processes within one or more containers port List port mappings or a specific mapping for the container ps List containers pull Pull an image or a repository from a registry push Push an image or a repository to a registry rename Rename a container restart Restart one or more containers rm Remove one or more containers rmi Remove one or more images run Run a command in a new container save Save one or more images to a tar archive (streamed to STDOUT by default) search Search the Docker Hub for images start Start one or more stopped containers stats Display a live stream of container(s) resource usage statistics stop Stop one or more running containers tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE top Display the running processes of a container unpause Unpause all processes within one or more containers update Update configuration of one or more containers version Show the Docker version information wait Block until one or more containers stop, then print their exit codes Run 'docker COMMAND --help' for more information on a command. neil@linux-famw:~>
咱們看一下docker的logo,在大海中的鯨魚馱着許多集裝箱。json
大海——宿主機系統windows10。ubuntu
鯨魚——docker
集裝箱——容器實例 from 咱們的鏡像模板
列出本地主機上的鏡像
neil@linux-famw:~> docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest f3159377bac1 6 months ago 462MB centos latest 49f7960eb7e4 8 months ago 200MB neil@linux-famw:~>
同一個倉庫源Repository能夠有多個tag,表明這個倉庫源的不一樣版本,咱們使用REPOSITORY:TAG來定義不一樣的鏡像。
若是你不指定一個鏡像的版本標籤,例如你只使用ubuntu,docker將默認使用ubuntu:latest鏡像。
格式:docker search [某個鏡像的名字]
用途:去hub上超找某個鏡像
參數
-a 列出本地全部
-q只顯示鏡像id
neil@linux-famw:~> docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest f3159377bac1 6 months ago 462MB centos latest 49f7960eb7e4 8 months ago 200MB neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> docker images -q f3159377bac1 49f7960eb7e4 neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> docker images -qa f3159377bac1 49f7960eb7e4 neil@linux-famw:~>
--digests 顯示鏡像摘要信息
--no-trunc 顯示完整的鏡像信息
neil@linux-famw:~> docker images --digests REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE tomcat latest sha256:73371bc88ce89aab6568ac22ed40522526a568db9e33de4dd013003ba77e7ff0 f3159377bac1 6 months ago 462MB centos latest sha256:b67d21dfe609ddacf404589e04631d90a342921e81c40aeaf3391f6717fa5322 49f7960eb7e4 8 months ago 200MB neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> docker images --no-trunc REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest sha256:f3159377bac19e15489be5738b59e534879a7874d914314947e8f4a6e2f718ad 6 months ago 462MB centos latest sha256:49f7960eb7e4cb46f1a02c1f8174c6fac07ebf1eb6d8deffbcb5c695f1c9edd5 8 months ago 200MB neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> docker images --digests --no-trunc REPOSITORY TAG DIGEST IMAGE ID CREATED SIZE tomcat latest sha256:73371bc88ce89aab6568ac22ed40522526a568db9e33de4dd013003ba77e7ff0 sha256:f3159377bac19e15489be5738b59e534879a7874d914314947e8f4a6e2f718ad 6 months ago 462MB centos latest sha256:b67d21dfe609ddacf404589e04631d90a342921e81c40aeaf3391f6717fa5322 sha256:49f7960eb7e4cb46f1a02c1f8174c6fac07ebf1eb6d8deffbcb5c695f1c9edd5 8 months ago 200MB neil@linux-famw:~>
--no-truncate 顯示完整的鏡像信息。
-s 列出收藏數不小於指定值的鏡像。
--automated 只列出automated build類型的鏡像
neil@linux-famw:~> docker search tensorflow NAME DESCRIPTION STARS OFFICIAL AUTOMATED tensorflow/tensorflow Official Docker images for the machine learn… 1303 jupyter/tensorflow-notebook Jupyter Notebook Scientific Python Stack w/ … 113 xblaster/tensorflow-jupyter Dockerized Jupyter with tensorflow 52 [OK] tensorflow/serving Official images for TensorFlow Serving (http… 37 floydhub/tensorflow tensorflow 15 [OK] bitnami/tensorflow-serving Bitnami Docker Image for TensorFlow Serving 13 [OK] opensciencegrid/tensorflow-gpu TensorFlow GPU set up for OSG 7 hytssk/tensorflow tensorflow image with matplotlib.pyplot.imsh… 3 [OK] tensorflow/tf_grpc_test_server Testing server for GRPC-based distributed ru… 3 andreleoni/cnn-tensorflow Container for convlutional network with Pyt… 2 bitnami/tensorflow-inception Bitnami Docker Image for TensorFlow Inception 2 [OK] lablup/kernel-python-tensorflow TensorFlow container imager for Backend.Ai 2 mikebirdgeneau/r-tensorflow RStudio and Tensorflow 2 [OK] ibmcom/tensorflow-ppc64le Community supported ppc64le docker images fo… 1 abhishek404/tensorflow-gpu Tensorflow GPU image 1 spellrun/tensorflow-cpu-jupyter 0 spellrun/tensorflow 0 opensciencegrid/tensorflow TensorFlow image with some OSG additions 0 mpioperator/tensorflow-benchmarks TensorFlow benchmarks using MPI. 0 [OK] linkernetworks/tensorflow 0 djpetti/rpinets-tensorflow Tensorflow container that is ready to be use… 0 [OK] spellrun/tensorflow-cpu 0 mediadesignpractices/tensorflow Tensorflow w/ CUDA (GPU) + extras 0 [OK] kuberlab/tensorflow 0 tinymind/tensorflow TensorFlow performance-optimized images. 0 neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> docker search -s 50 tensorflow Flag --stars has been deprecated, use --filter=stars=3 instead NAME DESCRIPTION STARS OFFICIAL AUTOMATED tensorflow/tensorflow Official Docker images for the machine learn… 1303 jupyter/tensorflow-notebook Jupyter Notebook Scientific Python Stack w/ … 113 xblaster/tensorflow-jupyter Dockerized Jupyter with tensorflow 52 [OK] neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> docker search -s 50 --no-trunc tensorflow Flag --stars has been deprecated, use --filter=stars=3 instead NAME DESCRIPTION STARS OFFICIAL AUTOMATED tensorflow/tensorflow Official Docker images for the machine learning framework TensorFlow (http://www.tensorflow.org) 1303 jupyter/tensorflow-notebook Jupyter Notebook Scientific Python Stack w/ Tensorflow from https://github.com/jupyter/docker-stacks 113 xblaster/tensorflow-jupyter Dockerized Jupyter with tensorflow 52 [OK] neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> docker search -s 50 --no-trunc --automated tensorflow Flag --stars has been deprecated, use --filter=stars=3 instead Flag --automated has been deprecated, use --filter=is-automated=true instead NAME DESCRIPTION STARS OFFICIAL AUTOMATED xblaster/tensorflow-jupyter Dockerized Jupyter with tensorflow 52 [OK] neil@linux-famw:~>
格式:docker pull [某個鏡像的名字]
用途:下載某個鏡像
docker pull [某個鏡像的名字]:[tag]
若是省略標籤tag,拉的就是latest
neil@linux-famw:~> docker pull redis Using default tag: latest latest: Pulling from library/redis 6ae821421a7d: Pull complete e3717477b42d: Pull complete 8e70bf6cc2e6: Pull complete 0f84ab76ce60: Pull complete 0903bdecada2: Pull complete 492876061fbd: Pull complete Digest: sha256:dd5b84ce536dffdcab79024f4df5485d010affa09e6c399b215e199a0dca38c4 Status: Downloaded newer image for redis:latest neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis latest 0f55cf3661e9 11 days ago 95MB tomcat latest f3159377bac1 6 months ago 462MB centos latest 49f7960eb7e4 8 months ago 200MB neil@linux-famw:~>
格式:docker rmi [某個鏡像的名字]:[tag]
用途:刪除鏡像,能夠指定tag,不然就是latest
若是刪除的鏡像的實例正在運行等拒絕刪除,能夠用-f 來強制刪除。
neil@linux-famw:~> docker pull hello-world Using default tag: latest latest: Pulling from library/hello-world 1b930d010525: Pull complete Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535 Status: Downloaded newer image for hello-world:latest neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE redis latest 0f55cf3661e9 11 days ago 95MB hello-world latest fce289e99eb9 6 weeks ago 1.84kB tomcat latest f3159377bac1 6 months ago 462MB centos latest 49f7960eb7e4 8 months ago 200MB neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> docker rmi hello-world Untagged: hello-world:latest Untagged: hello-world@sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535 Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3 neil@linux-famw:~>
若是想刪除多個:
docker rmi -f hello-world:latest redis:latest
若是想刪除多個:
docker rmi -f hello-world:latest redis:latest
neil@linux-famw:~> docker rmi -f hello-world:latest redis:latest Untagged: hello-world:latest Untagged: hello-world@sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535 Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3 Untagged: redis:latest Untagged: redis@sha256:dd5b84ce536dffdcab79024f4df5485d010affa09e6c399b215e199a0dca38c4 Deleted: sha256:0f55cf3661e92cc44014f9d93e6f7cbd2a59b7220a26edcdb0828289cf6a361f Deleted: sha256:ed0c42950e7403f60c58781449dca388c4a94508c0d3b8791c2601a1d1125347 Deleted: sha256:de4918c467bfefdfb4941b5f0f0e6321eeefb473d1bec214f00a24fc1a6e9134 Deleted: sha256:412024e43555d2550abf946431ebf1f94394c3e2fe1d4433d4b634ecacf37d12 Deleted: sha256:98c514ae1a3222c9b19e95e992f6cea0a1bb3b0230d84f46db2331a4faa4714e Deleted: sha256:dbd46d8f44a2505eaa8215e1675efaccefa0f3d73c26d501e92dc3550d2f1114 Deleted: sha256:0a07e81f5da36e4cd6c89d9bc3af643345e56bb2ed74cc8772e42ec0d393aee3 neil@linux-famw:~>
除了上面按照鏡像名稱來刪除,還能夠按照image id來刪除。
neil@linux-famw:~> docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest fce289e99eb9 6 weeks ago 1.84kB tomcat latest f3159377bac1 6 months ago 462MB centos latest 49f7960eb7e4 8 months ago 200MB neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> docker rmi fce289e99eb9 Untagged: hello-world:latest Untagged: hello-world@sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535 Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3 neil@linux-famw:~> neil@linux-famw:~> neil@linux-famw:~> docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest f3159377bac1 6 months ago 462MB centos latest 49f7960eb7e4 8 months ago 200MB neil@linux-famw:~>
還能夠使用組合命令來刪除,須要用$()來引入子命令,能夠相似理解爲shell命令中的管道。
好比咱們要刪除全部的image:
neil@linux-famw:~> docker rmi -f $(docker images -qa) Untagged: tomcat:latest Untagged: tomcat@sha256:73371bc88ce89aab6568ac22ed40522526a568db9e33de4dd013003ba77e7ff0 Deleted: sha256:f3159377bac19e15489be5738b59e534879a7874d914314947e8f4a6e2f718ad Deleted: sha256:1e81a81543359f14776fd4e9caff6f4a6d2a39ad13559c739f3642ba796156f7 Deleted: sha256:57984ead2376b1838667aa35d2fd0d164a4b6e3cad64b85a45498031eb9e3877 Deleted: sha256:12d45a664a8940171b90320b6d651b40087d0adb136780f984ccdd98af20e03c Deleted: sha256:25f483f8d0fc367e8e4cccd710140357c36d377d70c5bf40e35c7e1ca1fd08c5 Deleted: sha256:8ea51af48bed15c74b777e20d66db2f6f9597a09e2680d58d548a4376508270e Deleted: sha256:0b4e382b389106133a8b58e284d0fe5a3c86f5120db365254b2c10af26de6b75 Deleted: sha256:de64f0582d703cff59616159ddfbad5ed3894b188553ed63b817cab0bee294d3 Deleted: sha256:3e8466a6857def9407c73ef710407b15f2035d2c015d725f414e8c447af1f09a Deleted: sha256:dc6a832c3c5750e5bcc39ddc9aee2064fbda663f19c004078c16fc05bac017a1 Deleted: sha256:b1ae7168c6f3e061aa3943740ec3ceaf8e582dc65feab31d2b56d464a5062d59 Deleted: sha256:4a495dbc04bd205c728297a08cf203988e91caeafe4b21fcad94c893a53d96dc Deleted: sha256:3b10514a95bec77489a57d6e2fbfddb7ddfdb643907470ce5de0f1b05c603706 Untagged: centos:latest Untagged: centos@sha256:b67d21dfe609ddacf404589e04631d90a342921e81c40aeaf3391f6717fa5322 Deleted: sha256:49f7960eb7e4cb46f1a02c1f8174c6fac07ebf1eb6d8deffbcb5c695f1c9edd5 Deleted: sha256:bcc97fbfc9e1a709f0eb78c1da59caeb65f43dc32cd5deeb12b8c1784e5b8237 neil@linux-famw:~>