先了解一个例子:
了解镜像、容器等在使用时的机制:
docker run hello-world
本地先看有没有hello-world的容器;如果没有该容器,再看本地有没有hello-world的镜像;如果没有该镜像,就到docker仓库中去pull一个镜像到本地。然后,以该镜像为模板产生容器实例运行。
VM和容器的差别,回忆一下:
Docker的帮助类命令
docker version
查看docker的版本信息。常常用来验证docker是否安装成功。
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)
docker info
查看docker容器等的相关信息,如正在运行的容器实例的个数,暂停的个数,一共有多少images。
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 --help
查询docker的命令帮助
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,在大海中的鲸鱼驮着许多集装箱。
大海——宿主机系统windows10。
鲸鱼——docker
集装箱——容器实例 from 我们的镜像模板
docker images
列出本地主机上的镜像
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
格式: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 [某个镜像的名字]
用途:下载某个镜像
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
格式: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:~>