七、docker基本命令

Docker 基本命令

docker的基本命令

docker version :查看docker的版本号,包括客户端、服务端、依赖的Go等

[root@centos7 ~]# docker version
Client:
 Version:      1.8.2-el7.centos
 API version:  1.20
 Package Version: docker-1.8.2-10.el7.centos.x86_64
 Go version:   go1.4.2
 Git commit:   a01dc02/1.8.2
 Built:        
 OS/Arch:      linux/amd64

Server:
 Version:      1.8.2-el7.centos
 API version:  1.20
 Package Version: 
 Go version:   go1.4.2
 Git commit:   a01dc02/1.8.2
 Built:        
 OS/Arch:      linux/amd64

docker info:查看系统(docker)层面信息,包括管理的images, containers数等

[root@centos7 ~]# docker info
Containers: 1
Images: 4
Storage Driver: devicemapper
 Pool Name: docker-8:3-36786088-pool
 Pool Blocksize: 65.54 kB
 Backing Filesystem: xfs
 Data file: /dev/loop0
 Metadata file: /dev/loop1
 Data Space Used: 2.059 GB
 Data Space Total: 107.4 GB
 Data Space Available: 12.93 GB
 Metadata Space Used: 1.765 MB
 Metadata Space Total: 2.147 GB
 Metadata Space Available: 2.146 GB
 Udev Sync Supported: true
 Deferred Removal Enabled: false
 Data loop file: /var/lib/docker/devicemapper/devicemapper/data
 Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
 Library Version: 1.02.107-RHEL7 (2015-10-14)
Execution Driver: native-0.2
Logging Driver: json-file
Kernel Version: 3.10.0-327.el7.x86_64
Operating System: CentOS Linux 7 (Core)
CPUs: 1
Total Memory: 977.9 MiB
Name: centos7
ID: BUKD:MUW2:5X2D:G7BF:6Y7G:SKIH:LD6K:VUAC:3QA4:JY5C:S3DG:LFT2
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled

 search 搜索镜像

[root@centos7 ~]# docker search ubuntu12.10
INDEX       NAME                                  DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/chug/ubuntu12.10x32         Ubuntu Quantal Quetzal 12.10 32bit  base i...   0                    
docker.io   docker.io/chug/ubuntu12.10x64         Ubuntu Quantal Quetzal 12.10 64bit  base i...   0                    
docker.io   docker.io/marcgibbons/ubuntu12.10                                                     0                    
docker.io   docker.io/mirolin/ubuntu12.10                                                         0                    
docker.io   docker.io/mirolin/ubuntu12.10_redis                                                   0         

pull 下载镜像

[root@centos7 ~]# docker pull ubuntu

run 使用镜像创建容器

[root@centos7 ~]# docker run ubuntu /bin/echo hello world

run 创建容器,并交互式的运行
这里会创建一个新的容器。

[root@centos7 ~]# docker run -i -t ubuntu /bin/bash
root@c43c7d102baa:/# cat /etc/issue
Ubuntu 14.04.3 LTS 
 l
# -t 选项让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上, -i 则让容器的标准输入保持打开

当利用 docker run 来创建容器时,Docker 在后台运行的标准操作包括:

  • 检查本地是否存在指定的镜像,不存在就从公有仓库下载
  • 利用镜像创建并启动一个容器
  • 分配一个文件系统,并在只读的镜像层外面挂载一层可读写层
  • 从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去
  • 从地址池配置一个 ip 地址给容器
  • 执行用户指定的应用程序
  • 执行完毕后容器被终止

run -d 守护态运行
更多的时候,需要让 Docker 容器在后台以守护态(Daemonized)形式运行。此时,可以通过添加 -d 参数来实现。
例如下面的命令会在后台运行容器。

[root@centos7 ~]# docker run -d ubuntu /bin/bash -c "while true;do echo hello world;sleep 1;done"

logs 查看容器的运行

以上个例子为前导。

[root@centos7 ~]# docker logs 4f34f95b6abc
hello world
hello world
hello world
hello world
hello world
hello world
hello world

ps 查看容器

[root@centos7 ~]# docker ps -h

Usage:  docker ps [OPTIONS]

List containers

  -a, --all=false       Show all containers (default shows just running)
  --before=             Show only container created before Id or Name
  -f, --filter=[]       Filter output based on conditions provided
  --format=             Pretty-print containers using a Go template
  --help=false          Print usage
  -l, --latest=false    Show the latest created container, include non-running
  -n=-1                 Show n last created containers, include non-running
  --no-trunc=false      Don't truncate output
  -q, --quiet=false     Only 
 numeric IDs
  -s, --size=false      Display total file sizes
  --since=              Show created since Id or Name, include non-running

attach 连接已经启动的容器 / start -i 启动并连接容器

[root@centos7 ~]# docker ps -a  #查看容器ID
[root@centos7 ~]# docker start <CONTAINER ID>   #启动容器
[root@centos7 ~]# docker attach <CONTAINER ID>  #连接容器,该容器必须是启动状态
或者
[root@centos7 ~]# docker start -i <CONTAINER ID>        #启动并连接容器

:但是使用 attach 命令有时候并不方便。当多个窗口同时 attach 到同一个容器的时候,所有窗口都会同步显示。当某个窗口因命令阻塞时,其他窗口也无法执行操作了。

commit 将容器的状态保存为镜像

[root@centos7 ~]# docker commit c43c7d102baa ubhttp
d47bbf8e50bace073de2b256b0360cfab029c11881f0d361fce7ae7464aa40ff
[root@centos7 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubhttp              latest              d47bbf8e50ba        54 seconds ago      248 MB
docker.io/ubuntu    latest              8693db7e8a00        7 days ago          187.9 MB
## 更为标准点的如下:
$ sudo docker commit -m "Added json gem" -a "Docker Newbee" 0b2616b0e5a8 ouruser/sinatra:v2
其中,-m 来指定提交的说明信息,跟我们使用的版本控制工具一样;-a 可以指定更新的用户信息;之后是用来创建镜像的容器的 ID;最后指定目标镜像的仓库名和 tag 信息。创建成功后会返回这个镜像的 ID 信息。

diff 命令查看容器内的文件变化

它可以列出容器内发生变化的文件和目录。这些变化包括添加(A-add)、删除(D-delete)、修改(C-change)

[root@centos7 ~]# docker diff c43c7d102baa

cp 命令拷贝文件

#从docker中往本地拷贝文件
[root@centos7 ~]# docker cp c43c7d102baa:/var/www/html/index.html /opt/   
[root@centos7 ~]# ls /opt/
index.html  rh
# 从本地往docker中拷贝文件
[root@centos7 ~]# docker cp aa c43c7d102baa:/var
[root@centos7 ~]# docker start -i c43c7d102baa
root@c43c7d102baa:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@c43c7d102baa:/# ls var/
aa  backups  cache  lib  local  lock  log  mail  opt  run  spool  tmp  www

inspect 收集有关容器和镜像的底层信息

Docker inspect命令可以收集有关容器和镜像的底层信息。这些信息包括:

  • 容器实例的IP地址
  • 端口绑定列表
  • 特定端口映射的搜索
  • 收集配置的详细信息

语法:

docker inspect container/image

kill 命令发送sigkill信号停止容器的主进程

语法:

docker kill [options] <container_id>

rmi 移除一个或多个镜像

docker rmi <image_id>
#注意:在删除镜像之前要先用 docker rm 删掉依赖于这个镜像的所有容器

wait 阻塞对指定容器的其它调用方法,直到容器停止后退出阻塞

docker wait <container_id>

tag 修改镜像的标签

[root@centos7 ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
<none>              <none>              f59c7e5b1817        18 hours ago        192 MB
docker.io/ubuntu    latest              8693db7e8a00        7 days ago          187.9 MB
[root@centos7 ~]# docker tag f59c7e5b1817 zwx/ub_mv:127 
[root@centos7 ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
zwx/ub_mv           127                 f59c7e5b1817        18 hours ago        192 MB
docker.io/ubuntu    latest              8693db7e8a00        7 days ago          187.9 MB

docker的导入导出操作

save 保存镜像为tar文件并发送到STDOUT:

[root@node2 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
zwx_ub              latest              f59c7e5b1817        7 seconds ago       192 MB
ubuntu              latest              8693db7e8a00        6 days ago          187.9 MB
[root@node2 ~]# docker save f59c7e5b1817 >zwx_ub.tar
# 我将zwx_ub这个镜像导出成tar包,并拷贝到centos7的测试机中导入,导入过程在下边。

load 从tar文件中载入镜像或仓库到STDIN:

[root@centos7 ~]# docker load -i zwx_ub.tar 
[root@centos7 ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubhttp              latest              d47bbf8e50ba        About an hour ago   248 MB
<none>              <none>              f59c7e5b1817        16 hours ago        192 MB
docker.io/ubuntu    latest              8693db7e8a00        7 days ago          187.9 MB
[root@centos7 ~]# docker run -it f59c7e5b1817
root@e17558664f8d:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@e17558664f8d:/# ls /mnt/
zwx
# 可以看出,我导入zwx_ub这个镜像后,镜像ID并没有变化,我创建个容器并进入,发现打包前我创建的文件都在。

import 从本地文件系统导入一个镜像

比如,先下载了一个 ubuntu-14.04 的镜像,之后使用以下命令导入
tar.gz的镜像可以在http://openvz.org/Download/template/precreated下载。

[root@centos7 ~]# cat ubuntu-14.04-x86_64-minimal.tar.gz  |docker import - ubuntu:zwx
23997a971195cdd826f16a50573e480e1be1679729636178146425cdd46d1b52
[root@centos7 ~]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu              zwx                 23997a971195        28 seconds ago      214.9 MB

export 容器的导出

[root@centos7 ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
16f568766019        ubuntu              "/bin/bash"         52 minutes ago      Up 45 minutes                           elegant_mcclintock
[root@centos7 ~]# docker export 16f568766019 >ubuntu.tar

import 容器的导入

可以将容器的tar文件再导入为镜像

$ cat ubuntu.tar | sudo docker import - test/ubuntu:v1.0
$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
test/ubuntu         v1.0                9d37a6082e97        About a minute ago   171.3 MB

此外,也可以通过指定 URL 或者某个目录来导入,例如

$sudo docker import http://example.com/exampleimage.tgz example/imagerepo

:用户既可以使用 docker load 来导入镜像存储文件到本地镜像库,也可以使用 docker import 来导入一个容器快照到本地镜像库。这两者的区别在于容器快照文件将丢弃所有的历史记录和元数据信息(即仅保存容器当时的快照状态),而镜像存储文件将保存完整记录,体积也要大。此外,从容器快照文件导入时可以重新指定标签等元数据信息。

转自:https://www.jianshu.com/p/4ab37ad30bd2

原文地址:https://www.cnblogs.com/Rcsec/p/8890059.html