Docker的常用命令

  • Post author:
  • Post category:其他




Docker的常用命令



帮助命令

docker version

docker info         显示docker的系统信息包括镜像和容器数量

docker 命令 --help



镜像命令

docker images

可选项:
Options:
  -a    Show all images 
  -q    Only show image IDs

[root@vm088141 ~]# docker images --all
REPOSITORY     TAG        IMAGE ID       CREATED         SIZE
hello-world    latest     feb5d9fea6a5   2 months ago    13.3kB
openjdk        11.0.9.1   1eec9f9fe101   10 months ago   628MB
jdk11-gisuni   latest     b243476ec00c   17 months ago   700MB
[root@vm088141 ~]# docker images -aq
feb5d9fea6a5
1eec9f9fe101
b243476ec00c

docker search 搜索镜像

https://hub.docker.com/

[root@vm088141 ~]# docker search mysql
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   11712     [OK]       
mariadb                           MariaDB Server is a high performing open sou…   4466      [OK]       
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   870                  [OK]

docker pull 下载镜像

[root@vm088141 ~]# docker pull mysql
Using default tag: latest #不指定版本默认下载最新版本
latest: Pulling from library/mysql
a10c77af2613: Pull complete 
b76a7eb51ffd: Pull complete 
258223f927e4: Pull complete 
2d2c75386df9: Pull complete 
63e92e4046c9: Pull complete 
f5845c731544: Pull complete 
bd0401123a9b: Pull complete 
3ef07ec35f1a: Pull complete 
c93a31315089: Pull complete 
3349ed800d44: Pull complete 
6d01857ca4c1: Pull complete 
4cc13890eda8: Pull complete 
Digest: sha256:aeecae58035f3868bf4f00e5fc623630d8b438db9d05f4d8c6538deb14d4c31b    #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest  #真实地址

docker pull mysql《==》docker pull docker.io/library/mysql:latest

#指定版本
[root@vm088141 ~]# docker pull mysql:5.7

docker rmi 删除镜像

docker rmi -f 镜像id
docker rmi -f $(docker images -aq) 删除全部镜像



容器命令

有了镜像才能创建容器,下载一个centos镜像进行测试

docker pull centos

新建容器并启动

docker run [可选参数] image

#参数说明
--name      容器名字
-d          后台方式运行
-it         使用交互方式运行 进入容器查看内容
-p          指定容器端口 -p 8080:8080(外网端口:容器端口)

#启动并进入容器
[root@vm088141 ~]# docker run -it centos /bin/bash
[root@73e1cd7aa4c6 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

退出:exit

列出所有容器

docker ps

[root@vm088141 ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED        STATUS        PORTS     NAMES
b2ab6be70497   jdk11-gisuni   "/bin/bash ./app/bin…"   8 months ago   Up 18 hours             worker
e06dd894ee61   jdk11-gisuni   "/bin/bash ./app/bin…"   8 months ago   Up 18 hours             data-transport
[root@vm088141 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED        STATUS                    PORTS     NAMES
73e1cd7aa4c6   centos         "/bin/bash"              17 hours ago   Exited (0) 13 hours ago             reverent_edison
a5e0849026df   hello-world    "/hello"                 18 hours ago   Exited (0) 18 hours ago             jolly_margulis
b2ab6be70497   jdk11-gisuni   "/bin/bash ./app/bin…"   8 months ago   Up 18 hours                         worker
e06dd894ee61   jdk11-gisuni   "/bin/bash ./app/bin…"   8 months ago   Up 18 hours                         data-transport

启动和停止

docker start    容器id
docker restart  容器id
docker stop     容器id
docker kill     容器id



其它常用命令

进入当前正在运行的容器

docker exec -it 容器id bashshell

[root@vm088141 ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS     NAMES
17190aa0f2a9   centos         "/bin/bash"              35 seconds ago   Up 35 seconds             musing_solomon
e06dd894ee61   jdk11-gisuni   "/bin/bash ./app/bin…"   8 months ago     Up 18 hours               data-transport
[root@vm088141 ~]# docker exec -it 17190aa0f2a9 /bin/bash
[root@17190aa0f2a9 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@17190aa0f2a9 /]#

#方式二:
[root@vm088141 ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS     NAMES
17190aa0f2a9   centos         "/bin/bash"              4 minutes ago   Up 4 minutes             musing_solomon
e06dd894ee61   jdk11-gisuni   "/bin/bash ./app/bin…"   8 months ago    Up 18 hours              data-transport
[root@vm088141 ~]# docker attach 17190aa0f2a9
[root@17190aa0f2a9 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@17190aa0f2a9 /]# 


方式一会新起一个终端,方式二不会,如果是执行命令方式二直接进入的是命令

从容器内拷贝文件到主机上

[root@vm088141 ~]# docker attach 17190aa0f2a9
[root@17190aa0f2a9 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@17190aa0f2a9 /]# touch test.scala
[root@17190aa0f2a9 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  test.scala  tmp	usr  var
[root@17190aa0f2a9 /]# exit
exit
[root@vm088141 ~]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                        PORTS     NAMES
17190aa0f2a9   centos         "/bin/bash"              11 minutes ago   Exited (127) 17 seconds ago             musing_solomon
[root@vm088141 ~]# docker cp 17190aa0f2a9:/test.scala ~/
[root@vm088141 ~]# ls
test.scala  zookeeper.out



测试安装过nginx

[root@vm088141 ~]# docker search nginx
NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
nginx                             Official build of Nginx.                        15844     [OK]       
jwilder/nginx-proxy               Automated Nginx reverse proxy for docker con…   2096                 [OK]
richarvey/nginx-php-fpm           Container running Nginx + PHP-FPM capable of…   819                  [OK]
jc21/nginx-proxy-manager          Docker container for managing Nginx proxy ho…   279                  
linuxserver/nginx                 An Nginx container, brought to you by LinuxS…   160                  
tiangolo/nginx-rtmp               Docker image with Nginx using the nginx-rtmp…   145                  [OK]
jlesage/nginx-proxy-manager       Docker container for Nginx Proxy Manager        143                  [OK]
alfg/nginx-rtmp                   NGINX, nginx-rtmp-module and FFmpeg from sou…   110                  [OK]
nginxdemos/hello                  NGINX webserver that serves a simple page co…   78                   [OK]
privatebin/nginx-fpm-alpine       PrivateBin running on an Nginx, php-fpm & Al…   60                   [OK]
nginx/nginx-ingress               NGINX and  NGINX Plus Ingress Controllers fo…   57                   
nginxinc/nginx-unprivileged       Unprivileged NGINX Dockerfiles                  54                   
staticfloat/nginx-certbot         Opinionated setup for automatic TLS certs lo…   25                   [OK]
nginxproxy/nginx-proxy            Automated Nginx reverse proxy for docker con…   25                   
nginx/nginx-prometheus-exporter   NGINX Prometheus Exporter for NGINX and NGIN…   22                   
schmunk42/nginx-redirect          A very simple container to redirect HTTP tra…   19                   [OK]
centos/nginx-112-centos7          Platform for running nginx 1.12 or building …   16                   
centos/nginx-18-centos7           Platform for running nginx 1.8 or building n…   13                   
bitwarden/nginx                   The Bitwarden nginx web server acting as a r…   11                   
flashspys/nginx-static            Super Lightweight Nginx Image                   11                   [OK]
webdevops/nginx                   Nginx container                                 9                    [OK]
mailu/nginx                       Mailu nginx frontend                            9                    [OK]
sophos/nginx-vts-exporter         Simple server that scrapes Nginx vts stats a…   7                    [OK]
ansibleplaybookbundle/nginx-apb   An APB to deploy NGINX                          3                    [OK]
wodby/nginx                       Generic nginx                                   1                    [OK]
[root@vm088141 ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
eff15d958d66: Pull complete 
1e5351450a59: Pull complete 
2df63e6ce2be: Pull complete 
9171c7ae368c: Pull complete 
020f975acd28: Pull complete 
266f639b35ad: Pull complete 
Digest: sha256:097c3a0913d7e3a5b01b6c685a60c03632fc7a2b50bc8e35bcaa3691d788226e
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@vm088141 ~]# docker images
REPOSITORY     TAG        IMAGE ID       CREATED         SIZE
nginx          latest     ea335eea17ab   6 days ago      141MB
[root@vm088141 ~]# docker run -d --name nginx01 -p 8899:80 nginx
78c6c0d1a115369f855cccdef44dfc4fbfb9e0220e4fad7ee6ca02dc61f5aee3
[root@vm088141 ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                                   NAMES
78c6c0d1a115   nginx          "/docker-entrypoint.…"   6 seconds ago   Up 5 seconds   0.0.0.0:8899->80/tcp, :::8899->80/tcp   nginx01
e06dd894ee61   jdk11-gisuni   "/bin/bash ./app/bin…"   8 months ago    Up 18 hours                                            data-transport
[root@vm088141 ~]# curl localhost:8899
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@vm088141 ~]# 

查询资源消耗
docker stats 容器id

[root@vm088141 ~]# docker stats 6d1783944b42

CONTAINER ID   NAME                CPU %     MEM USAGE / LIMIT     MEM %     NET I/O         BLOCK I/O     PIDS
6d1783944b42   hardcore_thompson   0.00%     9.652MiB / 31.26GiB   0.03%     308kB / 4.6MB   0B / 3.54MB   17



可视化

>portainer
docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer

http://192.168.88.141:8088/#/init/admin

>Rancher

欢迎关注公众号进行交流沟通!!!

在这里插入图片描述



版权声明:本文为wzj_wp原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。