• Docker知识总结 (三) Docker 常用命令


    帮助命令

     docker version # 显示版本信息
     docker info #显示docker的系统信息,包括镜像和容器的数量
     docker --help  #帮助命令
    
    • 1
    • 2
    • 3

    镜像命令

    docker images (查看所有本地主机上的镜像)

    在这里插入图片描述
    options:

      -a, --all             Show all images (default hides intermediate images)
          --digests         Show digests
      -f, --filter filter   Filter output based on conditions provided
          --format string   Pretty-print images using a Go template
          --no-trunc        Don't truncate output
      -q, --quiet           Only show image IDs
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    docker search (搜索镜像)

    语法:

    docker search [OPTIONS] TERM
    
    • 1

    options:

     -f, --filter filter   Filter output based on conditions provided  
          --format string   Pretty-print search using a Go template   
          --limit int       Max number of search results (default 25)  # 限制搜索的结果数
          --no-trunc        Don't truncate output  #不要截断式输出,输出完整信息
    
    • 1
    • 2
    • 3
    • 4

    测试:
    在这里插入图片描述

    [root@VM-4-17-centos /]# docker search --filter=stars=4800 mysql  #搜索srars >= 4800 的镜像
    NAME      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
    mysql     MySQL is a widely used, open-source relation…   12456     [OK]       
    mariadb   MariaDB Server is a high performing open sou…   4800      [OK] 
    
    • 1
    • 2
    • 3
    • 4

    docker pull 下载镜像

    语法:

    docker pull [OPTIONS] NAME[:TAG|@DIGEST]
    
    • 1

    如果不指定tag,默认会下载最新版:

    [root@VM-4-17-centos docker]# docker pull mysql
    Using default tag: latest
    latest: Pulling from library/mysql
    72a69066d2fe: Pull complete  # 分层下载 docker image的核心  联合文件系统
    93619dbc5b36: Pull complete 
    99da31dd6142: Pull complete 
    626033c43d70: Pull complete 
    37d5d7efb64e: Pull complete 
    ac563158d721: Pull complete 
    d2ba16033dad: Pull complete 
    688ba7d5c01a: Pull complete 
    00e060b6d11d: Pull complete 
    1c04857f594f: Pull complete 
    4d7cfa90e6ea: Pull complete 
    e0431212d27d: Pull complete 
    Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 #签名
    Status: Downloaded newer image for mysql:latest
    docker.io/library/mysql:latest  # 真实地址
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    下载指定版本:
    在这里插入图片描述

    docker rmi 删除镜像

    语法:

    docker rmi [OPTIONS] IMAGE [IMAGE...]
    
    • 1

    Options:

      -f, --force      Force removal of the image
          --no-prune   Do not delete untagged parents
    
    • 1
    • 2

    删除指定的镜像:

    [root@VM-4-17-centos docker]# docker rmi -f 镜像id
    
    [root@VM-4-17-centos docker]# docker rmi -f 镜像id 镜像id 镜像id  #删除多个指定的镜像
    
    • 1
    • 2
    • 3

    删除所有的镜像:

    [root@VM-4-17-centos docker]# docker rmi -f $(docker images -aq)
    
    • 1

    docker image COMMAND (管理镜像)

    docker image build

    容器命令 (有了镜像才可以创建容器)

    下载一个镜像:

    docker pull centos
    
    • 1

    docker run (启动容器)

    语法:

    docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
    
    • 1

    options:

    --name="容器名"   		# 容器名字,用来区分
    -d,--detach             # 后台方式运行
    -it              		# 交互方式运行
    -p,--publish            # 指定容器端口  -p 8080:8080  (主机端口:容器端口)
    						#			  -p 容器端口
    -P                      # 随机指定容器端口
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    测试:

    [root@VM-4-17-centos docker]# docker run -it centos /bin/bash  #启动并进入容器
    [root@2dbdb4cbd153 /]# 
    [root@2dbdb4cbd153 /]# ls  # 查看容器内的centos,基础版本,很多命令都是不完善的
    bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
    [root@2dbdb4cbd153 /]# exit  #停止并退出容器
    exit
    
    [root@2dbdb4cbd153 /]# Ctrl + p + q  # 容器不停止退出
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    后台启动容器:

    [root@VM-4-17-centos /]# docker run -d centos
    63b4b860e303cb48d4ced551a40e7cbfecba6f225e6b327a5984c7295bd1e739
    [root@VM-4-17-centos /]# docker ps  # 问题:centos停止了
    CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
    [root@VM-4-17-centos /]# docker ps -a  # 显示曾经确实启动过
    CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
    63b4b860e303   centos    "/bin/bash"   9 seconds ago    Exited (0) 8 seconds ago              sweet_kapitsav
    
    # docker容器使用后台运行,就必须要有要一个前台进程,docker发现没有应用,就会自动停止
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    docker ps (查看容器)

    语法:

    docker ps [OPTIONS]
    
    • 1

    options:

    -a,--all		Show all containers (default shows just running)
    -n,--last 	-1	Show n last created containers (includes all states)
    
    • 1
    • 2

    测试:

    [root@VM-4-17-centos docker]# docker ps  # 默认查看运行中的容器
    CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
    
    [root@VM-4-17-centos docker]# docker ps -aq   # 只显示容器的编号
    2dbdb4cbd153
    89a25ebdd481
    a94930222559
    9e5cdbd9a2dc
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    docker rm (删除容器)

    测试:

    docker rm 容器id                      # 删除容器
    docker rm -f $(docker ps -aq)        # 删除所有容器
    docker ps -a -q|xargs docker rm      # 删除所有容器
    
    
    • 1
    • 2
    • 3
    • 4

    容器的启动,重启,停止,强制停止

    docker start 容器id
    docker restart 容器id
    docker stop 容器id
    docker kill 强制停止当前容器
    
    • 1
    • 2
    • 3
    • 4

    常用其他命令

    docker logs (查看日志)

    语法:

    docker logs [OPTIONS] CONTAINER
    
    • 1

    Options:

          --details        Show extra details provided to logs
      -f, --follow         Follow log output
          --since string   Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
      -n, --tail string    Number of lines to show from the end of the logs (default "all")
      -t, --timestamps     Show timestamps
          --until string   Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    docker logs -f -t 容器id

    docker top (显示容器的运行进程)

    语法:

    docker top CONTAINER [ps OPTIONS]
    
    • 1

    测试:

    [root@VM-4-17-centos ~]# docker ps
    CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS          PORTS     NAMES
    49ef775ebe56   centos    "/bin/bash"   10 minutes ago   Up 10 minutes             sharp_hermann
    [root@VM-4-17-centos ~]# docker top 49ef775ebe56 
    UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
    root                2168                2148                0                   21:49               pts/0               00:00:00            /bin/bash
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    docker inspect (Return low-level information on Docker objects)

    语法:

    docker inspect [OPTIONS] NAME|ID [NAME|ID...]
    
    • 1

    测试:

    [root@VM-4-17-centos ~]# docker inspect 49ef775ebe56 
    [
        {
            "Id": "49ef775ebe56f57c6cbd5755a9143afdc042ab0fecdbe8b0559a5604d7f45250",
            "Created": "2022-04-25T13:49:29.897962545Z",
            "Path": "/bin/bash",
            "Args": [],
            "State": {
                "Status": "running",
                "Running": true,
                "Paused": false,
                "Restarting": false,
                "OOMKilled": false,
                "Dead": false,
                "Pid": 2168,
                "ExitCode": 0,
                "Error": "",
                "StartedAt": "2022-04-25T13:49:30.21242609Z",
                "FinishedAt": "0001-01-01T00:00:00Z"
            },
    	......]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    进入运行中的容器

    进入正在运行的容器:

    1. 方法一
     docker exec -it 257b2852ccde /bin/bash   # 进入正在运行的容器   进入容器后,启动一个新的终端
    
    • 1
    1. 方法二
    [root@VM-4-17-centos ~]# docker attach b121d7816628   #进入容器正在执行的终端,不会启动新的进程
    [root@b121d7816628 /]# 
    
    • 1
    • 2

    docker cp (在容器和本地文件系统之间复制文件/文件夹)

    语法:

    docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
    docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH
    
    • 1
    • 2

    测试:

    [root@VM-4-17-centos home]# docker cp b121d7816628:/home/zhx.java /home
    [root@VM-4-17-centos home]# ls
    lighthouse  zhx.java
    
    
    • 1
    • 2
    • 3
    • 4

    Docker 安装示例

    Docker 安装nginx

    1. 搜索镜像(docker hub上搜索)
    2. 下载镜像
    3. 运行测试
    # -d 后台运行
    # --name 给容器命名
    # -p宿主机端口:容器内部端口
    [root@VM-4-17-centos home]# docker run -d --name nginx01 -p 3344:80 nginx
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 通过外网访问服务器的3344端口:
      在这里插入图片描述

    Docker 安装 tomcat

    docker run -it --rm tomcat:9.0   # 添加 --rm 之后,容器用完即删,一般用来测试
    
    • 1
    1. 搜索镜像
    2. 下载镜像
    docker pull tomcat:9.0
    
    • 1
    1. 运行测试
    [root@VM-4-17-centos ~]# docker run -d -p 3355:8080 --name tomcat01 tomcat
    3502c81df5b84f562012fac37fd72883973da4ce0a98dc9f26a3538501309314
    
    • 1
    • 2
    1. 通过外网访问3355端口
      在这里插入图片描述
    2. 进入容器
    [root@VM-4-17-centos ~]# docker exec -it tomcat01 /bin/bash   #进入容器
    root@3502c81df5b8:/usr/local/tomcat# ls
    BUILDING.txt  CONTRIBUTING.md  LICENSE	NOTICE	README.md  RELEASE-NOTES  RUNNING.txt  bin  conf  lib  logs  native-jni-lib  temp  webapps  webapps.dist  work
    root@3502c81df5b8:/usr/local/tomcat# ls -al
    total 176
    drwxr-xr-x 1 root root  4096 Dec 22 17:07 .
    drwxr-xr-x 1 root root  4096 Dec 22 17:00 ..
    -rw-r--r-- 1 root root 18994 Dec  2 22:01 BUILDING.txt
    -rw-r--r-- 1 root root  6210 Dec  2 22:01 CONTRIBUTING.md
    -rw-r--r-- 1 root root 60269 Dec  2 22:01 LICENSE
    -rw-r--r-- 1 root root  2333 Dec  2 22:01 NOTICE
    -rw-r--r-- 1 root root  3378 Dec  2 22:01 README.md
    -rw-r--r-- 1 root root  6905 Dec  2 22:01 RELEASE-NOTES
    -rw-r--r-- 1 root root 16517 Dec  2 22:01 RUNNING.txt
    drwxr-xr-x 2 root root  4096 Dec 22 17:07 bin
    drwxr-xr-x 1 root root  4096 May  9 12:33 conf
    drwxr-xr-x 2 root root  4096 Dec 22 17:06 lib
    drwxrwxrwx 1 root root  4096 May  9 12:33 logs
    drwxr-xr-x 2 root root  4096 Dec 22 17:07 native-jni-lib
    drwxrwxrwx 2 root root  4096 Dec 22 17:06 temp
    drwxr-xr-x 2 root root  4096 Dec 22 17:06 webapps
    drwxr-xr-x 7 root root  4096 Dec  2 22:01 webapps.dist
    drwxrwxrwx 2 root root  4096 Dec  2 22:01 work
    root@3502c81df5b8:/usr/local/tomcat# cd webapps
    root@3502c81df5b8:/usr/local/tomcat/webapps# ls  #发现webapps是空的
    root@3502c81df5b8:/usr/local/tomcat/webapps#
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    发现问题:webapps中是空的 。

    解决方案: 将webapps.dist中存在的文件复制到webapps中

    oot@3502c81df5b8:/usr/local/tomcat# cp -r ./webapps.dist/* ./webapps
    root@3502c81df5b8:/usr/local/tomcat# cd webapps
    root@3502c81df5b8:/usr/local/tomcat/webapps# ls
    ROOT  docs  examples  host-manager  manager
    
    • 1
    • 2
    • 3
    • 4
    1. 再次访问
      在这里插入图片描述

    Docker 部署elasticsearch + kibana

    1. 下载并启动elasticsearch
    # --net somenetwork  网络配置
    docker run -d --name elasticsearch  -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2
    
    • 1
    • 2
    1. 访问测试
    [root@VM-4-17-centos ~]# curl localhost:9200
    {
      "name" : "1017d2e1ac76",
      "cluster_name" : "docker-cluster",
      "cluster_uuid" : "rPAns8Q9TMOvN3awtcxi-Q",
      "version" : {
        "number" : "7.6.2",
        "build_flavor" : "default",
        "build_type" : "docker",
        "build_hash" : "ef48eb35cf30adf4db14086e8aabd07ef6fb113f",
        "build_date" : "2020-03-26T06:34:37.794943Z",
        "build_snapshot" : false,
        "lucene_version" : "8.4.0",
        "minimum_wire_compatibility_version" : "6.8.0",
        "minimum_index_compatibility_version" : "6.0.0-beta1"
      },
      "tagline" : "You Know, for Search"
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    1. 查看容器资源占用情况
      在这里插入图片描述
      问题:内存占用很大

    2. 停止elasticsearch,增加内存限制

    [root@VM-4-17-centos ~]# docker stop 1017d2e1ac76
    1017d2e1ac76
    
    • 1
    • 2

    再次启动:

    [root@VM-4-17-centos ~]# docker run -d --name elasticsearch02  -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xms512m" elasticsearch:7.6.2
    8ec0bb7da1a4b8f3f260e55839c7f9c4852f8246fb4ecb0ed914f8dd54755be7
    
    • 1
    • 2

    查看容器资源占用情况:
    在这里插入图片描述

  • 相关阅读:
    Redis主从复制流程
    被疫情占据的上半年,你还好么?| 2022年中总结
    ubuntu 外置相机使用记录
    基于区域的图像分割
    优雅处理Golang中的异常
    (附源码)计算机毕业设计Java大学生学科竞赛报名管理系统
    Linux下git维护
    第十八章《JDBC》第1节:JDBC简介
    golang指针学习
    MongoDB——入门篇(介绍)
  • 原文地址:https://blog.csdn.net/weixin_45773632/article/details/126140046