docker 常用命令整理

1. 查看docker 版本信息

[root@localhost ~]# 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
.......此处省略

显示docker 系统信息

[root@localhost ~]# docker info 
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 1
Server Version: 18.09.0
Storage Driver: overlay2
 Backing Filesystem: xfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
......此处省略

2. 对image操作(search 、pull、images、rmi、history)

#检索 images
例 :检索nginx 镜像
[root@localhost ~]# docker search nginx
NAME                                                   DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                                                  Official build of Nginx.                        10672               [OK]                
jwilder/nginx-proxy                                    Automated Nginx reverse proxy for docker con…   1498                                    [OK]
richarvey/nginx-php-fpm                                Container running Nginx + PHP-FPM capable of…   672                                     [OK]
jrcs/letsencrypt-nginx-proxy-companion                 LetsEncrypt container to use with nginx as p…   463                                     [OK]
kong                                                   Open-source Microservice & API Management la…   276                 [OK]                
webdevops/php-nginx                                    Nginx with PHP-FPM                              119                                     [OK]
kitematic/hello-world-nginx                            A light-weight nginx container that demonstr…   117                                     
zabbix/zabbix-web-nginx-mysql                          Zabbix frontend based on Nginx web-server wi…   84                                      [OK]
bitnami/nginx                                          Bitnami nginx Docker Image                      59                                      [OK]
.......此处省略

下载 image

例: 下载httpd  镜像
[root@localhost ~]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
a5a6f2f73cd8: Already exists 
ac13924397e3: Pull complete 
91b81769f14a: Pull complete 
fec7170426de: Pull complete 
992c7790d5f3: Pull complete 
Digest: sha256:1a25dda4141b143ca018490fb4f64ce9aa8acb16c2660a7aa395db9fe4ae1793
Status: Downloaded newer image for httpd:latest

列出下载的镜像列表

[root@localhost ~]# docker images
 REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              02256cfb0e4b        10 days ago         109MB
httpd               latest              2a51bb06dc8b        7 weeks ago         132MB

删除一个或者多个镜像

[root@localhost ~]#  docker rmi nginx    //  -f  强制,这里可以用镜像名称也可以用镜像ID 
Untagged: nginx:latest
Untagged: nginx@sha256:333a036f4f8cd2314bdf339a5c72440e011071831699abefe03b73442d9b0045
Deleted: sha256:02256cfb0e4bd19bdb74a077cee8ab26dd14d2e54402878a848e172a85a5cec7
Deleted: sha256:ac016a366692f4540270a300262445d81bd2fbfc66f536b71d292fa57f7e20eb
Deleted: sha256:b40dc7b9491a10b0439a24601f48bb92a54157aab577117cd0fda1c7d67d1e82

#删除所有容器
[root@localhost ~]#  docker rm $(docker ps -a -q)
e1c388091f07
93383e694bf0
f8cf89e7231a
866022fffaf5
0c0236bede03
e71ff8990f11
97544d5f8ba6
486bd01398ab
b6e641ee1208

2. 启动容器(run)
docker 可以理解为在沙盒中运行的进程,这个沙盒包含了该进程运行所必须的资源,包括文件系统、系统类库、shell环境等等。但是这个沙盒默认是不会运行任何程序的,你需要在沙盒中运行一个进程来启动某一个容器,这个进程是该容器的唯一进程,所以当该进程结束时,容器也会全部停止

#在容器中运行  echo  命令,输出 "hello  word"
[root@localhost ~]# docker run 2a51bb06dc8b echo "hello word"
hello word

#交互式进入容器
[root@localhost ~]# docker  run -i -t  2a51bb06dc8b /bin/bash 
root@01c7b20948fa:/usr/local/apache2#                      //提示符发生改变

#在容器中安装新的程序        //也可以直接进入到容器里面安装
 [root@localhost ~]# docker run centos yum install net-tools
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: mirrors.cqu.edu.cn
 * extras: mirrors.cqu.edu.cn
 * updates: mirrors.cqu.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.24.20131004git.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package         Arch         Version                          Repository  Size
================================================================================
Installing:
 net-tools       x86_64       2.0-0.24.20131004git.el7         base       306 k

Transaction Summary
================================================================================
Install  1 Package

#在容器中执行这个程序     
[root@localhost ~]# docker run centos ping baidu.com
PING baidu.com (220.181.57.216) 56(84) bytes of data.
64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=1 ttl=127 time=74.2 ms
64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=2 ttl=127 time=73.7 ms
64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=3 ttl=127 time=142 ms
64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=4 ttl=127 time=99.5 ms

3. 查看容器

#列出当前所有正在运行的容器
[root@localhost ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ff257582c804        centos              "/bin/bash"         6 seconds ago       Up 5 seconds                            kind_dhawan


#列出所有容器,包括未运行的
[root@localhost ~]# docker ps -a 
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                            PORTS               NAMES
ff257582c804        centos              "/bin/bash"              About a minute ago   Up About a minute                                     kind_dhawan
2309b66ac44b        centos              "/bin/bash"              4 minutes ago        Exited (127) About a minute ago                       angry_germain
732aa7d912f6        centos              "ping baidu.com"         9 minutes ago        Exited (0) 9 minutes ago                              objective_rubin

#列出最近一次启动的容器
[root@localhost ~]# docker ps -l
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ff257582c804        centos              "/bin/bash"         2 minutes ago       Up 2 minutes                            kind_dhawan

5. 保存对容器的修改
当你对某一个容器做了修改之后(通过在容器中运行某一个命令),可以把对容器的修改保存下来,这样下次可以从保存后的最新状态运行该容器

[root@localhost ~]# docker commit  ff257582c804  centos:server          
sha256:b2efc126f814fc571a418a206ca50fb3e8f6318f5158c1664ae87017435c07a1

[root@localhost ~]# docker images      //查看保存后的镜像
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              server              b2efc126f814        2 minutes ago       202MB

6. 对容器的操作(stop、start、kill、logs、diff、top、cp、restart、attach)
停止、启动、杀死一个容器

[root@localhost ~]# docker stop f491f0b3b182       //停止容器
f491f0b3b182

[root@localhost ~]# docker start f491f0b3b182      //启动容器
f491f0b3b182

[root@localhost ~]# docker kill f491f0b3b182       //杀死容器
f491f0b3b182

从一个容器中取出日志

[root@localhost ~]# docker logs f491f0b3b182

显示一个运行容器里面的进程

[root@localhost ~]# docker top f491f0b3b182
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                5257                5240                0                   03:37               pts/0               00:00:00            /bin/bash

列出一个容器里面被改变的文件或者目录,list列表会显示出三种事件,A. 增加的、B. 删除的、C. 被改变的

[root@localhost tmp]# docker diff e0252b888fe2
C /usr
C /usr/lib
C /usr/lib/systemd
C /usr/lib/systemd/system
A /usr/lib/systemd/system/arp-ethers.service
C /usr/sbin
A /usr/sbin/mii-diag
A /usr/sbin/iptunnel
A /usr/sbin/slattach

把本地的文件/目录拷贝到容器的一个路径

[root@localhost ~]# docker cp /tmp/chens e0252b888fe2:/opt/chens.txt
[root@e0252b888fe2 tmp]# cd /opt/
[root@e0252b888fe2 opt]# ls                  //容器里面已经存在
chens.txt          

# 把容器里面的文件拷贝到本地的一个路径、
[root@localhost ~]# docker cp e0252b888fe2:/tmp/yum.log  /opt/
[root@localhost ~]# ls /opt/
 yum.log

重启一个正在运行的容器

[root@localhost ~]# docker restart e0252b888fe2
e0252b888fe2

7. 保存和加载镜像(save、load)

方法一: docker  save   image_name   -o  file_path
例:保存这个镜像到tar 包
[root@localhost ~]# docker save centos -o /home/centos.tar 
[root@localhost ~]# ls /home/
centos.tar

#导入这个镜像
[root@nfs-client opt]# docker load -i centos.tar 
Loaded image: centos:latest
Loaded image: centos:server

方法二:
 例:保存这个镜像到tar 包
[root@localhost ~]# docker save b2efc126f814 > /opt/chens.tar
[root@localhost ~]# ls /opt/
chens.tar 

#导入这镜像
[root@localhost ~]# docker load </opt/chens.tar 
071d8bd76517: Loading layer [==================================================>]  210.2MB/210.2MB
Loaded image ID: sha256:b2efc126f814fc571a418a206ca50fb3e8f6318f5158c1664ae87017435c07a1

发布images (push)

[root@localhost ~]# docker push  02256cfb0e4b 
The push refers to repository [docker.io/library/02256cfb0e4b]
An image does not exist locally with the tag: 02256cfb0e4b

根据Dockefile 构建出一个容器

docker  build   -t  image_name   Dockefile_path

获取容器的IP地址
docker inspect 容器ID |grep IPAddress

[root@localhost html]# docker inspect 906d2dedb434 |grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.4",
                    "IPAddress": "172.17.0.4",
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值