Docker镜像下载&Docker镜像操作常用命令

Docker系统简介

Docker系统有两个程序:docker服务端和docker客户端。其中docker服务端是一个服务进程,管理着所有的容器。docker客户端则扮演着docker服务端的远程控制器,可以用来控制docker的服务端进程。大部分情况下,docker服务端和客户端运行在一台机器上。

检查docker版本

检查docker的版本,这样可以用来确认docker服务在运行并可通过客户端链接。

命令如下:

[root@slave01 ~]# docker version
Client:
 Version:           18.09.0
 API version:       1.39
 Go version:        go1.10.4
 Git commit:        4d60db4
 Built:             Wed Nov  7 00:48:22 2018
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.0
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.4
  Git commit:       4d60db4
  Built:            Wed Nov  7 00:19:08 2018
  OS/Arch:          linux/amd64
  Experimental:     false

举个例子

搜索tutorial可用的镜像

[root@slave01 ~]# docker search tutorial
NAME                                          DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
learn/tutorial                                                                                40                                      
georgeyord/reactjs-tutorial                   This is the backend of the React comment box…   5                                       [OK]
microsoft/aci-tutorial-app                                                                    2                                       
fiware/tutorials.tourguide-app                FIWARE Tour Guide App sample application        1                                       [OK]
mhausenblas/kairosdb-tutorial                 GitHub fetcher for KairosDB tutorial            1                                       [OK]
microsoft/aci-tutorial-sidecar                                                                1                                       
muli/gluon-tutorials-zh                       https://github.com/mli/gluon-tutorials-zh       1                                       [OK]
chris24walsh/flask-aws-tutorial               Runs a simple flask webapp demo, with the op…   1                                       [OK]
colemurray/medium-facenet-tutorial            Container for medium-facenet-tutorial. Conta…   1                                       
fgraichen/concourse-tutorial-hello-world                                                      0                                       
camphor/python-tutorial                       camphor-/python-tutorial                        0                                       [OK]
activeeon/par-connector-tutorial              Do the par-connector tutorial with R. The tu…   0                                       [OK]
splicemachine/tutorial-spark-kafka-consumer   Spark Streaming Tutorial                        0                                       
krishnatest/docker-nodejs-tutorialkk          docker-nodejs-tutorialkk                        0                                       
rade95/concourse-tutorial-hello-world                                                         0                                       
helinwang/paddle-k8s-tutorial                                                                 0                                       
sahotay/concourse-tutorial-hello-world                                                        0                                       
kidikarus/concourse-tutorial-47-tasks                                                         0                                       
lukasheinrich/quickana-tutorial               Image for the analysis code built from https…   0                                       
starkandwayne/concourse-tutorial-ci                                                           0                                       
bluerdocker/concourse-tutorial-hello-world                                                    0                                       
dlws/tutorial-tensorflow-cpu                                                                  0                                       
joshuacox/rabbitmq-tutorials                  Rabbit MQ Tutorial Demos                        0                                       [OK]
cloudboost/tutorial                                                                           0                                       
starkandwayne/concourse-tutorial                                                              0                                       

搜索的docker镜像如上所示。

下载镜像

镜像learn/tutorial下载

使用docker pull命令即可。在docker的镜像索引网站上面,镜像都是按照用户名/镜像名的方式来存储的。有一组比较特殊的镜像,比如ubuntu这类基础镜像,经过官方的验证,值得信任,可以直接用镜像名来检索到。[毕竟docker是在ubuntu环境下搞出来的]

现在,下载一个镜像——learn/tutorial:

[root@slave01 docker]# docker pull learn/tutorial
Using default tag: latest
latest: Pulling from learn/tutorial
271134aeb542: Pull complete 
Digest: sha256:2933b82e7c2a72ad8ea89d58af5d1472e35dacd5b7233577483f58ff8f9338bd
Status: Downloaded newer image for learn/tutorial:latest

在容器中安装新的程序

接下来在容器中安装一个ping程序,上面步骤下载的tutorial镜像是基于ubuntu的,所以你可以使用ubuntu的apt-get命令来安装ping程序:apt-get install -y ping。

[root@slave01 docker]# docker run learn/tutorial apt-get install -y ping
Reading package lists...
Building dependency tree...
The following NEW packages will be installed:
  iputils-ping
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 56.1 kB of archives.
After this operation, 143 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ precise/main iputils-ping amd64 3:20101006-1ubuntu1 [56.1 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 56.1 kB in 1s (44.7 kB/s)
Selecting previously unselected package iputils-ping.
(Reading database ... 7545 files and directories currently installed.)
Unpacking iputils-ping (from .../iputils-ping_3%3a20101006-1ubuntu1_amd64.deb) ...
Setting up iputils-ping (3:20101006-1ubuntu1) ...

备注:apt-get 命令执行完毕之后,容器就会停止,但对容器的改动不会丢失。

保存对容器的修改

当你对某一个容器做了修改之后(通过在容器中运行某一个命令),可以把对容器的修改保存下来,这样下次可以从保存后的最新状态运行该容器。docker中保存状态的过程称之为committing,它保存的新旧状态之间的区别,从而产生一个新的版本。

那么如何保存对容器的更改?

首先使用如下命令获取安装完ping程序之后的容器id,然后将镜像改成learn/ping。

[root@slave01 docker]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
48e55917cc57        learn/ping          "apt-get ......"    17 minutes ago      Exited (0) 17 minutes ago                       pedantic_zhukovsky
[root@slave01 docker]# 

然后我们可以看到CONTAINER ID那一列的ID,类似于git提交一样,将之提交即可:

[root@slave01 docker]# docker commit 48e55917cc57 learn/ping
sha256:95874dc21dd46feb028773097fd9fe76abc74718d1966bdc724d4d88ecb3fa4a

此时运行查看docker的镜像列表:

[root@slave01 docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
learn/ping          latest              95874dc21dd4        33 minutes ago      140MB
hello-world         latest              fce289e99eb9        8 days ago          1.84kB
learn/tutorial      latest              a7876479f1aa        5 years ago         128MB

运行新的镜像

[root@slave01 docker]# docker run learn/ping ping baidu.com
PING baidu.com (123.125.115.110) 56(84) bytes of data.
64 bytes from 123.125.115.110: icmp_req=1 ttl=127 time=52.5 ms
64 bytes from 123.125.115.110: icmp_req=2 ttl=127 time=51.5 ms
64 bytes from 123.125.115.110: icmp_req=3 ttl=127 time=55.1 ms
64 bytes from 123.125.115.110: icmp_req=4 ttl=127 time=52.3 ms
64 bytes from 123.125.115.110: icmp_req=5 ttl=127 time=50.9 ms
--- baidu.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4005ms
rtt min/avg/max/mdev = 50.925/52.498/55.164/1.460 ms

检查运行中的镜像

docker镜像其他常见操作

查看docker安装以及提交过的容器

[root@slave01 docker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        8 days ago          1.84kB
learn/tutorial      latest              a7876479f1aa        5 years ago         128MB

导出docker镜像

[root@slave01 /]# docker image save hello-world > docker-hello-world.tar.gz
[root@slave01 /]# ll
total 142756
lrwxrwxrwx.   1 root root         7 Nov  2 02:53 bin -> usr/bin
dr-xr-xr-x.   5 root root      4096 Nov  2 02:59 boot
drwxr-xr-x.  20 root root      3240 Jan  8 21:41 dev
-rw-r--r--.   1 root root     12800 Jan  9 06:30 docker-hello-world.tar.gz
-rw-r--r--.   1 root root 146142208 Jan  9 06:17 docker-learn-ping.tar.gz

删除docker镜像

[root@slave01 system]# docker image rm -f learn/tutorial
Untagged: learn/tutorial:latest
Untagged: learn/tutorial@sha256:2933b82e7c2a72ad8ea89d58af5d1472e35dacd5b7233577483f58ff8f9338bd

导入docker镜像

[root@slave01 /]# docker image load -i docker-hello-world.tar.gz 
Loaded image: hello-world:latest

删除镜像还可以根据IMAGE ID来进行删除:

[root@localhost yum.repos.d]# docker rmi -f fce289e99eb9
Untagged: hello-world:latest
Untagged: hello-world@sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3
[root@localhost yum.repos.d]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
tomcat              latest              1a51cb5e3006        13 days ago         462MB
mysql               latest              102816b1ee7d        13 days ago         486MB
centos              latest              1e1148e4cc2c        5 weeks ago         202MB

配置Docker的镜像加速

有时候我们会发现镜像下载的特别慢,但是跟网络又没关系,这时候可以采用国内的镜像地址:

vi /etc/docker/daemon.json
{
  "registry-mirrors": ["https://registry.docker-cn.com"]
}   

这样子就会使得镜像下载的速度加快。

镜像运行的状态

上面,我们简单介绍了如何运行一个镜像,现在docker的帮助命令进行一个说明:

[root@localhost yum.repos.d]# docker --help

Usage:    docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

Options:
      --config string      Location of client config files (default "/root/.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 "/root/.docker/ca.pem")
      --tlscert string     Path to TLS certificate file (default "/root/.docker/cert.pem")
      --tlskey string      Path to TLS key file (default "/root/.docker/key.pem")
      --tlsverify          Use TLS and verify the remote
  -v, --version            Print version information and quit

Management Commands:
  builder     Manage builds
  config      Manage Docker configs
  container   Manage containers
  engine      Manage the docker engine
  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.

当我们运行帮助命令的时候,我们会发现,类似linux的帮助命令,会罗列出一大堆的options和相关的命令。

比如这时候运行如下镜像:

[root@docker01 ~]# docker run -d -p 80:80 nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
e7bb522d92ff: Pull complete 
6edc05228666: Pull complete 
cd866a17e81f: Pull complete 
Digest: sha256:285b49d42c703fdf257d1e2422765c4ba9d3e37768d6ea83d7fe2043dad6e63d
Status: Downloaded newer image for nginx:latest
8d8f81da12b5c10af6ba1a5d07f4abc041cb95b01f3d632c3d638922800b0b4d

run:表示创建并且运行一个容器。

-d:表示放入后台。

-p:表示端口映射。

nginx:表示镜像名称。

坚壁清野

猜你喜欢

转载自www.cnblogs.com/hzhiping/p/10244744.html
今日推荐