15组运维常用的docker命令

1. docker信息与版本号

[root@localhost ~]# docker info
Containers: 3
 Running: 0
 Paused: 0
 Stopped: 3
Images: 2
Server Version: 18.09.6
Storage Driver: overlay2
 Backing Filesystem: xfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host macvlan null overlay
 Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: bb71b10fd8f58240ca47fbb579b9d1028eea7c84
runc version: 2b18fe1d885ee5083ef9f0838fee39b62d653e30
init version: fec3683
Security Options:
 seccomp
  Profile: default
Kernel Version: 3.10.0-693.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 3.686GiB
Name: localhost.localdomain
ID: XPLK:V2FP:GCCB:XTQW:KKND:MNBY:A3BC:J65J:WHZY:PRYB:4ECO:SMJQ
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
Registry Mirrors:
 http://f1361db2.m.daocloud.io/
Live Restore Enabled: false
Product License: Community Engine

[root@localhost ~]# docker version
Client:
 Version:           18.09.6
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        481bc77156
 Built:             Sat May  4 02:34:58 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.6
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.8
  Git commit:       481bc77
  Built:            Sat May  4 02:02:43 2019
  OS/Arch:          linux/amd64
  Experimental:     false

2. docker镜像命令

1) 搜索镜像 search

命令参数

--filter=stars=10  只显示收藏数≥10的镜像
--filter=is-automated=true  只列出 automated build类型的镜像
[root@localhost ~]# docker search --filter=stars=10 nginx
NAME                         DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                        Official build of Nginx.                        11523               [OK]                
jwilder/nginx-proxy          Automated Nginx reverse proxy for docker con…   1611                                    [OK]
richarvey/nginx-php-fpm      Container running Nginx + PHP-FPM capable of…   718                                     [OK]
bitnami/nginx                Bitnami nginx Docker Image                      67                                      [OK]
linuxserver/nginx            An Nginx container, brought to you by LinuxS…   63                                      
tiangolo/nginx-rtmp          Docker image with Nginx using the nginx-rtmp…   47                                      [OK]
nginx/nginx-ingress          NGINX Ingress Controller for Kubernetes         20                                      
arm64v8/nginx                Official build of Nginx.                        16                                      
schmunk42/nginx-redirect     A very simple container to redirect HTTP tra…   16                                      [OK]
nginxdemos/hello             NGINX webserver that serves a simple page co…   14                                      [OK]
blacklabelops/nginx          Dockerized Nginx Reverse Proxy Server.          12                                      [OK]
crunchgeek/nginx-pagespeed   Nginx with PageSpeed + GEO IP + VTS + more_s…   12                                      
centos/nginx-18-centos7      Platform for running nginx 1.8 or building n…   10                                      


[root@localhost ~]# docker search --filter=stars=10 --filter=is-automated=true nginx
NAME                       DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
jwilder/nginx-proxy        Automated Nginx reverse proxy for docker con…   1611                                    [OK]
richarvey/nginx-php-fpm    Container running Nginx + PHP-FPM capable of…   718                                     [OK]
bitnami/nginx              Bitnami nginx Docker Image                      67                                      [OK]
tiangolo/nginx-rtmp        Docker image with Nginx using the nginx-rtmp…   47                                      [OK]
schmunk42/nginx-redirect   A very simple container to redirect HTTP tra…   16                                      [OK]
nginxdemos/hello           NGINX webserver that serves a simple page co…   14                                      [OK]
blacklabelops/nginx        Dockerized Nginx Reverse Proxy Server.          12                                      [OK]

stars表示被收藏的次数
official表示是否是官方镜像
automated表示构建镜像的方式是否为自动构建

2)拉取镜像 pull

[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
fc7181108d40: Pull complete 
c4277fc40ec2: Pull complete 
780053e98559: Pull complete 
Digest: sha256:1e371c53dccfe7169dfb7bd2723d4e7d42738b6722a26e4b0559f9714a934713
Status: Downloaded newer image for nginx:latest

3)上传镜像 push

[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
fc7181108d40: Pull complete 
c4277fc40ec2: Pull complete 
780053e98559: Pull complete 
Digest: sha256:1e371c53dccfe7169dfb7bd2723d4e7d42738b6722a26e4b0559f9714a934713
Status: Downloaded newer image for nginx:latest
[root@localhost ~]# docker push nginx
The push refers to repository [docker.io/library/nginx]
d7acf794921f: Layer already exists 
d9569ca04881: Layer already exists 
cf5b3c6798f7: Layer already exists 
errors:
denied: requested access to the resource is denied
unauthorized: authentication required

4)登陆镜像仓库 login

留在架构二讲harbor的时候说

# docker login -u 用户名 -p 密码

5) 打标签 tag

语法: docker tag 镜像名 仓库ip:port/镜像名:标签
作用: 标记本地镜像,将其归入某一仓库。

# docker tag nginx 192.168.20.110/nginx:latest

6) 查看本机已有镜像

[root@localhost ~]# docker images
REPOSITORY             TAG                 IMAGE ID            CREATED             SIZE
192.168.20.110/nginx   latest              719cd2e3ed04        2 hours ago         109MB
nginx                  latest              719cd2e3ed04        2 hours ago         109MB
centos                 latest              9f38484d220f        2 months ago        202MB

7)删除镜像

语法:docker rmi -f 镜像名或镜像id编号
-f 表示强制删除

[root@localhost ~]# docker rmi -f 192.168.20.110/nginx
Untagged: 192.168.20.110/nginx:latest
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              719cd2e3ed04        2 hours ago         109MB
centos              latest              9f38484d220f        2 months ago        202MB

8)创建镜像 build

将dockerfile的时候具体说,此处不作说明

9)查看指定镜像的创建历史 history

# docker history centos
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
9f38484d220f        2 months ago        /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           2 months ago        /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B                  
<missing>           2 months ago        /bin/sh -c #(nop) ADD file:074f2c974463ab38c…   202MB               
这里看起来不明显,因为这个不是我们通过dockerfile逐层创建的

10)保存镜像成文件,便于备份和转移 save export

-o 表示output 指定输出的位置和文件名
# docker save -o /tmp/nginx.tar nginx
# ll -h /tmp/nginx.tar 
-rw------- 1 root root 108M Jun 11 09:52 /tmp/nginx.tar

# docker export -o /tmp/nginx.tar nginx(此处写的是容器名,表示将容器文件系统归档)

11)导入使用docker export导出的镜像

语法:docker import file 给导入的镜像一个名字

[root@localhost ~]# docker rmi nginx
Untagged: nginx:latest
Untagged: nginx@sha256:1e371c53dccfe7169dfb7bd2723d4e7d42738b6722a26e4b0559f9714a934713
Deleted: sha256:719cd2e3ed04781b11ed372ec8d712fac66d5b60a6fb6190bf76b7d18cb50105
Deleted: sha256:e9b6506fb887de50972aefd99d7c5eb56b1a8e757ed953cdfecb86b5359bcb22
Deleted: sha256:55d9d9692a9615a28d183a42bc3881a72a39393feba3664e669e7affb78daa76
Deleted: sha256:cf5b3c6798f77b1f78bf4e297b27cfa5b6caa982f04caeb5de7d13c255fd7a1e
[root@localhost ~]# docker import /tmp/nginx.tar #没给名字的话,查看镜像时名字是none
sha256:c041777310660ad351b17ea80ec9eceadbeb87f3c644b9627aecc70516dace43
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              c04177731066        11 seconds ago      113MB
centos              latest              9f38484d220f        2 months ago        202MB
[root@localhost ~]# docker import /tmp/nginx.tar nginx
sha256:03509898f9f6b80f61bc564b38c74a6a735e53c43d620f770b6a9370170d1805
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              03509898f9f6        2 seconds ago       113MB
<none>              <none>              c04177731066        23 seconds ago      113MB
centos              latest              9f38484d220f        2 months ago        202MB

12)导入使用docker save命令保存的镜像

语法格式: docker load -i file
[root@localhost ~]# docker rmi nginx
Untagged: nginx:latest
Deleted: sha256:03509898f9f6b80f61bc564b38c74a6a735e53c43d620f770b6a9370170d1805
Deleted: sha256:7f7fae475f149037e0538dcdd9016a53dbfdde91d459dfdedc40bea13237c819
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              9f38484d220f        2 months ago        202MB
[root@localhost ~]# docker load -i /tmp/nginx.tar 
cf5b3c6798f7: Loading layer [==================================================>]  58.45MB/58.45MB
d9569ca04881: Loading layer [==================================================>]  54.52MB/54.52MB
d7acf794921f: Loading layer [==================================================>]  3.584kB/3.584kB
Loaded image: nginx:latest
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              719cd2e3ed04        2 hours ago         109MB
centos              latest              9f38484d220f        2 months ago        202MB

3. docker容器命令

1)创建容器(但不启动) create

[root@localhost ~]# docker create --name nginxtest nginx
b8aa0673301033259383c28cc6cc29b9a7c3c2e926d0742dd051329cdc4355d2

nginxtest为给容器起的名字
nginx是镜像名字

2)容器的开启、关闭与重启 start/stop/restart

[root@localhost ~]# docker start nginxtest
nginxtest
[root@localhost ~]# docker stop nginxtest
nginxtest
[root@localhost ~]# docker restart nginxtest
nginxtest

3)暂停/恢复容器中的所有进程(相当于挂起) pause/unpause

[root@localhost ~]# docker pause nginxtest
nginxtest
[root@localhost ~]# docker unpause nginxtest
nginxtest

4)杀掉一个运行中的容器

[root@localhost ~]# docker kill nginxtest
nginxtest

5)创建容器并运行一条命令 或 创建并运行容器 run

语法:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

OPTIONS说明:

-a stdin: 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项;
-d: 后台运行容器,并返回容器ID;
-i: 以交互模式运行容器,通常与 -t 同时使用;
-p: 指定端口映射,格式为:主机(宿主)端口:容器端口
-P: 随机端口映射,容器内部端口随机映射到主机的高端口
-t: 为容器重新分配一个伪输入终端,通常与 -i 同时使用;
--name="nginx-lb": 为容器指定一个名称;
--dns 8.8.8.8: 指定容器使用的DNS服务器,默认和宿主一致;
--dns-search example.com: 指定容器DNS搜索域名,默认和宿主一致;
-h "mars": 指定容器的hostname;
--cpuset="0-2" or --cpuset="0,1,2": 绑定容器到指定CPU运行;
-m :设置容器使用内存最大值;
--net="bridge": 指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型;
-v 进行目录映射,主机目录映射容器目录
--expose=[]: 开放一个端口或一组端口;
[root@localhost ~]# mkdir /tmp/nginx
[root@localhost ~]# echo 1 > /tmp/nginx/index.html
[root@localhost ~]# docker run -itd -p 80:80 --name nginx1 -v /tmp/nginx/:/usr/local/nginx/html/ nginx
48ac00ada61df9dfa01fff63524498b3d8a3935396ac7eb6cbf5ff9942b1f26a

6)查看容器

[root@localhost ~]# docker ps #查看正在运行的容器
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                NAMES
48ac00ada61d        nginx               "nginx -g 'daemon of…"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp   nginx1

[root@localhost ~]# docker ps -a #查看所有容器,包括已经停掉的
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS                NAMES
48ac00ada61d        nginx               "nginx -g 'daemon of…"   2 minutes ago       Up 2 minutes                  0.0.0.0:80->80/tcp   nginx1
1cb673916454        nginx               "/bin/bash"              5 minutes ago       Created                                            reverent_carson
ad284e2acd95        nginx               "nginx -g 'daemon of…"   7 minutes ago       Exited (0) 6 minutes ago                           laughing_bardeen
9f766cec8827        nginx               "nginx -g 'daemon of…"   7 minutes ago       Exited (0) 7 minutes ago                           vibrant_shannon
b8aa06733010        nginx               "nginx -g 'daemon of…"   30 minutes ago      Exited (137) 24 minutes ago                        nginxtest
26f12b85f402        centos              "/bin/bash"              2 hours ago         Exited (130) 2 hours ago                           awesome_payne
4af8535e5a2d        centos              "/bin/bash"              2 hours ago         Created                                            eloquent_lederberg
e963cd14fe71        centos              "/bin/bash"              24 hours ago        Exited (0) 24 hours ago                            hardcore_spence

7)进入容器 或在运行的容器中执行一条命令 exec

语法格式:
docker exec -it 容器名或容器id  注:容器id通过ps查看
[root@localhost ~]# docker exec -it nginx1 ls /
bin   dev  home  lib64	mnt  proc  run	 srv  tmp  var
boot  etc  lib	 media	opt  root  sbin  sys  usr

[root@localhost ~]# docker exec -it nginx1 /bin/bash
root@48ac00ada61d:/# ls /usr/local/nginx/html/
index.html
root@48ac00ada61d:/# cat /usr/local/nginx/html/index.html  #可以看出映射的目录,内容相同
1
root@48ac00ada61d:/# exit
exit

8)查看容器中运行的进程信息

[root@localhost ~]# docker top nginx1
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                15919               15901               0                   10:32               pts/0               00:00:00            nginx: master process nginx -g daemon off;
101                 15952               15919               0                   10:32               pts/0               00:00:00            nginx: worker process

9)连接正在运行的容器 attach

docker attach可以attach到一个已经在运行的容器的stdin,然后进行命令执行的动作。
但是需要注意的是,如果直接从这个stdin中exit(ctrl+d),会导致容器的停止。非常不建议使用此命令操作容器。
如果不小心退出了,再次启动容器,进入容器启动服务即可。
正确的退出姿势 --> 组合键:Ctrl+P+Q  这样退出后容器仍在运行
[root@localhost ~]# docker run -itd --name mycentos centos
d626629e5e8d1952c4005d5ffa3fa173e7a57dffc8be0a9f843d6588fdbfe3d9
[root@localhost ~]# docker attach mycentos
[root@d626629e5e8d /]# ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@d626629e5e8d /]# echo 1 > test.txt
[root@d626629e5e8d /]# ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  test.txt  tmp  usr  var
[root@d626629e5e8d /]# exit
exit
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost ~]# 

[root@localhost ~]# docker start mycentos
mycentos
[root@localhost ~]# docker attach mycentos
[root@d626629e5e8d /]# ls
anaconda-post.log  bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  test.txt  tmp  usr  var
[root@d626629e5e8d /]# read escape sequence
[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
d626629e5e8d        centos              "/bin/bash"         2 minutes ago       Up 36 seconds                           mycentos

10)删除容器 rm

[root@localhost ~]# docker rm -f nginx1
nginx1

11)获取容器/镜像的元数据

[root@localhost ~]# docker inspect centos #查看centos镜像的元数据
[
    {
        "Id": "sha256:9f38484d220fa527b1fb19747638497179500a1bed8bf0498eb788229229e6e1",
        "RepoTags": [
            "centos:latest"
        ],
             "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]

[root@localhost ~]# docker inspect nginx1 #查看容器元数据
[
  
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "214f5babe52c66c7021e737e0c7c42dc7863b5dadada95eb5736e477576f5ce6",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "80"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/214f5babe52c",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "50dec7dc1e707714e4e68770f6257f07edf03ab0731b244771e3a690ddbfb7d9",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.2",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:02",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "f514b2d4652c1a445cfbca46db82eb6ab7c5ace363c5aecba09850ab84477059",
                    "EndpointID": "50dec7dc1e707714e4e68770f6257f07edf03ab0731b244771e3a690ddbfb7d9",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:02",
                    "DriverOpts": null
                }
            }
        }
    }
]

12)获取容器的日志 logs

[root@localhost ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
48ac00ada61d        nginx               "nginx -g 'daemon of…"   28 minutes ago      Up 2 minutes        0.0.0.0:80->80/tcp   nginx1
[root@localhost ~]# docker logs nginx1
^C^C^C[root@localhost ~]# 
之前执行过终止指令

13)列出指定容器的端口映射

[root@localhost ~]# docker port nginx1
80/tcp -> 0.0.0.0:80

14)将容器当前状态保存成一个镜像

语法格式:
docker commit 选项 容器名或id 要保存成的镜像名

OPTIONS说明:

-a :提交的镜像作者;
-c :使用Dockerfile指令来创建镜像;
-m :提交时的说明文字;
-p :在commit时,将容器暂停。
[root@localhost ~]# docker commit -a "guo" -m "test" nginx1 nginxnginx
sha256:f648b78936c0df288139b0c17bd40cd282f9eef062445b77aba2df177642881e
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginxnginx          latest              f648b78936c0        5 seconds ago       109MB
nginx               latest              719cd2e3ed04        3 hours ago         109MB
centos              latest              9f38484d220f        2 months ago        202MB

15)主机与容器之间的数据拷贝

[root@localhost ~]# echo 2 > /tmp/nginx/index1.html
[root@localhost ~]# docker cp /tmp/nginx/index1.html nginx1:/usr/local/nginx/html/
[root@localhost ~]# docker exec -it nginx1 ls /usr/local/nginx/html
index.html  index1.html
[root@localhost ~]# docker exec -it nginx1 cat /usr/local/nginx/html/index1.html
2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值