• 第3章 docker镜像和容器管理的基本命令


    安装完的docker后,我们就要接触相应的基础命令,下面我们分镜像命令和容器命令进行相应的介绍。

    查看镜像的相关命令

    1标识镜像的方法 

    • 第一种:REPOSITORY :TAG 
    • 第二种:IMAGE ID (镜像id)

    • 		1、REPOSITORY :TAG
      			centos:latest
      		2、IMAGE ID  (镜像id)
              	0d120b6ccaa8 (sha256:64位的号码,默认只截取12位)

    2.查看镜像

    • docker image ls  查看镜像信息
    • docker image ls --no-trunc  详细查看镜像信息
    • [root@localhost ~]# docker image ls 
      REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
      nginx               latest              bc9a0695f571        8 days ago          133MB
      centos              7                   8652b9f0cb4c        2 weeks ago         204MB
      centos              7.9.2009            8652b9f0cb4c        2 weeks ago         204MB
      centos              7.6.1810            f1cb7c7d58b7        20 months ago       202MB
      [root@localhost ~]# docker image ls --no-trunc
      REPOSITORY          TAG                 IMAGE ID                                                                  CREATED             SIZE
      nginx               latest              sha256:bc9a0695f5712dcaaa09a5adc415a3936ccba13fc2587dfd76b1b8aeea3f221c   8 days ago          133MB
      centos              7                   sha256:8652b9f0cb4c0599575e5a003f5906876e10c1ceb2ab9fe1786712dac14a50cf   2 weeks ago         204MB
      centos              7.9.2009            sha256:8652b9f0cb4c0599575e5a003f5906876e10c1ceb2ab9fe1786712dac14a50cf   2 weeks ago         204MB
      centos              7.6.1810            sha256:f1cb7c7d58b73eac859c395882eec49d50651244e342cd6c68a5c7809785f427   20 months ago       202MB

       

    • 描述

    • docker image ls 列出映像
    • 用法

    • docker image ls [OPTIONS] [REPOSITORY[:TAG]]
    • 选项

    名字,速记 默认 描述
    --all , -a   Show all images (default hides intermediate images) -----显示所有映像(默认隐藏中间映像)
    --digests   Show digests -----显示摘要
    --filter , -f   Filter output based on conditions provided -----根据提供的条件过滤输出
    --format   Pretty-print images using a Go template -----使用Go模板的漂亮打印映像
    --no-trunc   Don’t truncate output -----不要截断输出
    --quiet , -q   Only show numeric IDs -----仅显示数字ID

    3.查看镜像详细信息

    • 1.通过id号进行查看(id号不需要全部复制)
      [root@localhost ~]# docker image inspect bc9a0695f5712dcaaa
      2.通过镜像名:tag进行查看
      [root@localhost ~]# docker image inspect nginx:lates

    4.image 的基础命令

    • 1.	 [root@localhost Carrie]# docker image --help
      Usage:	docker image COMMAND
      
      Manage images
      
      Commands:
        build       Build an image from a Dockerfile
        创建       从dockerfile生成一个镜像
        history     Show the history of an image
        历史       显示镜像的历史
        import      Import the contents from a tarball to create a filesystem image

                                   从tarball导入内容以创建文件系统映像。

        inspect     Display detailed information on one or more images
        检查       在一个或多个图像上显示详细信息
        load        Load an image from a tar archive or STDIN
        加载        从a tar archive or STDIN中加载一个镜像
        ls          List images
        list show    列出镜像
        prune       Remove unused images
        修剪        删除未使用的镜像
        pull        Pull an image or a repository from a registry
        拉取       从注册表中拉取镜像或存储库
        push        Push an image or a repository to a registry
        推          给注册表推镜像或存储库
        rm          Remove one or more images
        删除          删除一个或者多个镜像
        save        Save one or more images to a tar archive (streamed to STDOUT by default)
        保存        保存一个或者多个镜像到a tar archive (streamed to STDOUT by default)
        tag         Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE   
        标记       创建一个标志 (改名作用)
      

        

    查看docker信息的命令

    1.查看docker的版本信息

    •  docker version
    • [root@localhost docker]# docker version
      Client: Docker Engine - Community
       Version:           19.03.13
       API version:       1.40
       Go version:        go1.13.15
       Git commit:        4484c46d9d
       Built:             Wed Sep 16 17:03:45 2020
       OS/Arch:           linux/amd64
       Experimental:      false
      
      Server: Docker Engine - Community
       Engine:
        Version:          19.03.13
        API version:      1.40 (minimum version 1.12)
        Go version:       go1.13.15
        Git commit:       4484c46d9d
        Built:            Wed Sep 16 17:02:21 2020
        OS/Arch:          linux/amd64
        Experimental:     false
       containerd:
        Version:          1.3.7
        GitCommit:        8fba4e9a7d01810a393d5d25a3621dc101981175
       runc:
        Version:          1.0.0-rc10
        GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
       docker-init:
        Version:          0.18.0
        GitCommit:        fec3683
      

    2.查看docker的详细信息

    •  docker info
    • [root@localhost docker]# docker info
      Client:
       Debug Mode: false
      
      Server:
       Containers: 0
        Running: 0
        Paused: 0
        Stopped: 0
       Images: 1
       Server Version: 19.03.13
       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 ipvlan 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: 8fba4e9a7d01810a393d5d25a3621dc101981175
       runc version: dc9208a3303feef5b3839f4323d9beb36df0a9dd
       init version: fec3683
       Security Options:
        seccomp
         Profile: default
       Kernel Version: 3.10.0-1160.6.1.el7.x86_64
       Operating System: CentOS Linux 7 (Core)
       OSType: linux
       Architecture: x86_64
       CPUs: 2
       Total Memory: 3.682GiB
       Name: localhost.localdomain
       ID: HKP3:SX23:3TYR:OV22:A6HB:7GZW:L5IY:PMFF:Q7BS:4PZP:X2UB:OEHT
       Docker Root Dir: /var/lib/docker
       Debug Mode: false
       Registry: https://index.docker.io/v1/
       Labels:
       Experimental: false
       Insecure Registries:
        127.0.0.0/8
       Registry Mirrors:
        https://72idtxd8.mirror.aliyuncs.com/
       Live Restore Enabled: false
      

     

    镜像的基础操作

    1.查找镜像

    • docker search  nginx (从网上查找的镜像)
    • 如果想指定星级 可以加-s 星级数(docker search nginx -s 20) ----------->显示星级数高于20的镜像
    • [root@localhost docker]# docker search nginx
      NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
      名字 描述 星级 是否官方创建
      nginx Official build of Nginx. 14063 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1912 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 795 [OK] linuxserver/nginx An Nginx container, brought to you by LinuxS… 131 jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 115 tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 105 [OK] bitnami/nginx Bitnami nginx Docker Image 90 [OK] alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 80 [OK] jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 72 [OK] nginxdemos/hello NGINX webserver that serves a simple page co… 63 [OK] nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 45 privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 42 [OK] nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 21 schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 19 [OK] nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 15 centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 15 staticfloat/nginx-certbot Opinionated setup for automatic TLS certs lo… 14 [OK] raulr/nginx-wordpress Nginx front-end for the official wordpress:f… 13 [OK] centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13 mailu/nginx Mailu nginx frontend 8 [OK] bitwarden/nginx The Bitwarden nginx web server acting as a r… 7 flashspys/nginx-static Super Lightweight Nginx Image 7 [OK] bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 6 [OK] wodby/nginx Generic nginx 1 [OK] ansibleplaybookbundle/nginx-apb An APB to deploy NGINX

        

    2.从仓库中拉取镜像(下载镜像)

    • docker image pull nginx 
    • [root@localhost docker]# docker image ls -a
      REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
      [root@localhost docker]# docker pull nginx
      Using default tag: latest
      latest: Pulling from library/nginx
      852e50cd189d: Pull complete 
      571d7e852307: Pull complete 
      addb10abd9cb: Pull complete 
      d20aa7ccdb77: Pull complete 
      8b03f1e11359: Pull complete 
      Digest: sha256:6b1daa9462046581ac15be20277a7c75476283f969cb3a61c8725ec38d3b01c3
      Status: Downloaded newer image for nginx:latest
      docker.io/library/nginx:latest
      [root@localhost docker]#  docker images
      REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
      nginx               latest              bc9a0695f571        9 days ago          133MB
      

    3.从容器中导出镜像到宿主机

    • docker image save nginx > /Carrie/nginx  
    • [root@localhost Carrie]# docker image save nginx > /Carrie/nginx
      

    4.删除镜像

    • 删除一个镜像:docker image  rm nginx 
    • 删除所有镜像:docker image rmi -f  'docker image ls -q'
    • [root@localhost Carrie]# docker image rm nginx
      Untagged: nginx:latest
      Untagged: nginx@sha256:6b1daa9462046581ac15be20277a7c75476283f969cb3a61c8725ec38d3b01c3
      Deleted: sha256:bc9a0695f5712dcaaa09a5adc415a3936ccba13fc2587dfd76b1b8aeea3f221c
      Deleted: sha256:a6862ade3b91fdde2aa8a3d77fdcc95b1eb6c606be079c11b7f97f249d0e731d
      Deleted: sha256:32bcbe3740b68d0625744e774b404140366c0c4a2b2eadf32280d66ba001b4fb
      Deleted: sha256:2dc5e43f496e41a18c016904b6665454a53be22eb4dcc1b468d864b4e2d1f311
      Deleted: sha256:5fe6a7c579cd9fbcfa604810974c4c0c16893f4c40bc801545607ebd0accea74
      Deleted: sha256:f5600c6330da7bb112776ba067a32a9c20842d6ecc8ee3289f1a713b644092f8
      

    5.从宿主机中导入镜像

    • docker image tag nginx load -i /Carrie/nginx (-i 从tar包中读取文件内容)
    • [root@localhost Carrie]# docker image tag nginx load -i /Carrie/nginx 
      f5600c6330da: Loading layer [==================================================>]  72.52MB/72.52MB
      7ccabd267c9f: Loading layer [==================================================>]  64.54MB/64.54MB
      850c2400ea4d: Loading layer [==================================================>]  3.072kB/3.072kB
      f790aed835ee: Loading layer [==================================================>]  4.096kB/4.096kB
      7e914612e366: Loading layer [==================================================>]  3.584kB/3.584kB
      Loaded image: nginx:latest
      [root@localhost Carrie]# docker image ls
      REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
      nginx               latest              bc9a0695f571        12 hours ago        133MB
      centos              7                   8652b9f0cb4c        11 days ago         204MB
      centos              7.6.1810            f1cb7c7d58b7        20 months ago       202MB
      

    6.镜像命名

    •  docker image tag ID号  新名:TAG
    • [root@localhost docker]# docker images
      REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
      nginx               latest              bc9a0695f571        9 days ago          133MB
      centos              7                   8652b9f0cb4c        2 weeks ago         204MB
      [root@localhost docker]# docker image tag 8652b9f0cb4c centos7.0:7.0
      [root@localhost docker]# docker images
      REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
      nginx               latest              bc9a0695f571        9 days ago          133MB
      centos7.0           7.0                 8652b9f0cb4c        2 weeks ago         204MB
      centos              7                   8652b9f0cb4c        2 weeks ago         204MB
      

    7.镜像的上传(将镜像传到仓库中)

    •  
    •  
    •  

    容器的基础操作

    • 启动容器是要依赖镜像的
    • 容器一般有两种形式一种为交互式容器(工具类容器),另一种为守护式容器

    1.启动交互式容器

    • docker container run -it nginx 
    • 参数介绍如下:
    • –detach -d 在后台运行容器,并且打印容器id。
      –interactive -i 即使没有连接,也要保持标准输入保持打开状态,一般与 -t 连用。
      –tty -t 分配一个伪tty,一般与 -i 连用。
    •  docker容器运行之后,不会进入容器,只会提示容器已经运行
      [root@localhost docker]# docker container run -it nginx
      /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
      /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
      /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
      10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
      10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
      /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
      /docker-entrypoint.sh: Configuration complete; ready for start up
      
    • 如果想要进入容器,需要开启一个进程窗口,一般是/bin/bash
    • docker container run -it nginx /bin/bash
    • [root@localhost docker]# docker container run -it nginx /bin/bash
      root@36a432a211d4:/# 
      

    2.启动守护式容器

    •  docker run -d --name="container-nginx" nginx 
    •  --name='' ------容器名字
      [root@localhost docker]# docker run -d --name="container-nginx" nginx 
      83f20975051a756b756ed6cddfaea7b22d3e23a6d91faaeb06d0df4f47abdada

      这就是已经启动了nginx的服务,用curl命令可以直接看到nginx的页面

    • [root@localhost docker]# curl 192.168.2.222:8080
      <!DOCTYPE html>
      <html>
      <head>
      <title>Welcome to nginx!</title>
      <style>
          body {
               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>

    3.docker容器的网络访问

      默认情况下,容器之间是可以ping通的,但是容器无法和宿主机外部连接,所以无法在浏览器中显示nginx.

      解决这个问题只需要启动容器时需要指定映射。

    • 1.宿主机的8080端口映射到container的80端口    : -p 8080:80 映射端口
      [root@localhost docker]# docker run -d --name="container-nginx" -p 8080:80 nginx
      
    •  查看容器和宿主机是否连接
    •  2.指定某段地址的8080端口映射到container的80端口   
    • [root@localhost docker]# docker run -d --name="container-nginx" -p 192.168.2.0:8080:80 nginx
      

        

    • 3.随机某段地址的端口映射到container的80端口 (随机端口从32768开始)
    • [root@localhost docker]# docker run -d --name="container-nginx" -p 192.168.2.0::80 nginx
      

        

    • 4.随机端口映射到container的80端口 
    • [root@localhost docker]# docker run -d --name="container-nginx" -p 80 nginx
      

        

    4 .查看版本

    • cat /etc/redhat-release

    • [root@localhost docker]# cat /etc/redhat-release 
      CentOS Linux release 7.9.2009 (Core)
      

    5.查看容器的状态

    • docker container ls
    • [root@localhost docker]# docker container ls
      CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES

      容器的唯一标识 镜像 容器的命令 时间 状态 映射端口 名字
      1e42240498a7        nginx               "/docker-entrypoint.…"   About an hour ago   Up About an hour    0.0.0.0:8080->80/tcp   Carrie_nginx

    6.查看容器的状态信息

    • 第一种:docker container ls -a  显示所有容器
    • 第二种: docker container ps    显示开启的容器
    • [root@localhost ~]# docker container ls -a
      CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS                  NAMES
      83f20975051a        nginx               "/docker-entrypoint.…"   12 hours ago        Up 5 seconds                80/tcp                 container-nginx
      b95a2f892de1        nginx               "/docker-entrypoint.…"   12 hours ago        Exited (0) 12 hours ago                            brave_bell
      36a432a211d4        nginx               "/docker-entrypoint.…"   12 hours ago        Exited (130) 12 hours ago                          magical_kowalevski
      1577a37d246d        nginx               "/docker-entrypoint.…"   12 hours ago        Exited (0) 12 hours ago                            affectionate_poincare
      579c50bca513        nginx               "/docker-entrypoint.…"   12 hours ago        Exited (0) 12 hours ago                            awesome_neumann
      1e42240498a7        nginx               "/docker-entrypoint.…"   13 hours ago        Up 13 hours                 0.0.0.0:8080->80/tcp   Carrie_nginx
      [root@localhost ~]# docker container ps
      CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
      83f20975051a        nginx               "/docker-entrypoint.…"   12 hours ago        Up About a minute   80/tcp                 container-nginx
      1e42240498a7        nginx               "/docker-entrypoint.…"   13 hours ago        Up 13 hours         0.0.0.0:8080->80/tcp   Carrie_nginx
      

    7.启动容器

    • 当镜像变成容器启动时用run命令,当容器生成再进行启动时就需要用start命令
    • 启动到后台
    • [root@localhost ~]# docker container ls -a
      CONTAINER ID        IMAGE               COMMAND                  CREATED             
      1e42240498a7        nginx               "/docker-entrypoint.…"   13 hours ago        Up 13 hours                 0.0.0.0:8080->80/tcp   Carrie_nginx
      [root@localhost ~]# docker container start 1e42240498a7 
      1e42240498a7
      

    8.进入容器的命令

    • 1.docker run;创建和启动一个新的容器实例,操作对象是镜像,选项较多,如果你要创建和启动一个容器,只能用run;
    • 实例:docker run -d --name="container-nginx" nginx
    • [root@localhost docker]# docker run -d --name="container-nginx" nginx 
      83f20975051a756b756ed6cddfaea7b22d3e23a6d91faaeb06d0df4f47abdada
      

        

    • 2.docker exec: 在已运行的容器中,执行命令,操作对象是容器,如果你要进入已运行的容器,并且执行命令,用exec;用exec的方式进入容器,退出容器时不会关闭容器
    • 实例:docker container exec -it 1e42240498a7 /bin/bash
    • [root@localhost ~]# docker container start 1e4
      1e4
      [root@localhost ~]# docker container exec -it 1e42240498a7 /bin/bash
      root@1e42240498a7:/# ls -a
      .   .dockerenv	boot  docker-entrypoint.d   etc   lib	 media	opt   root  sbin  sys  usr
      ..  bin		dev   docker-entrypoint.sh  home  lib64  mnt	proc  run   srv   tmp  var
      

        

    • 3.docker attach: 同样操作的是已运行的容器,可以将本机标准输入(键盘输入)输到容器中,也可以将容器的输出显示在本机的屏幕上,如果你想查看容器运行过程中产生的标准输入输出,用attach,用attach的方式进入容器,但是退出容器时会关闭容器
    • 实例:docker container attach 1e42240498a7
    • [root@localhost ~]# docker container ps
      CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
      83f20975051a        nginx               "/docker-entrypoint.…"   13 hours ago        Up 4 seconds        80/tcp                 container-nginx
      1e42240498a7        nginx               "/docker-entrypoint.…"   14 hours ago        Up 35 seconds       0.0.0.0:8080->80/tcp   Carrie_nginx
      [root@localhost ~]# docker container attach 1e42240498a7
      

            

               了解进入容器的命令详解请查看下一章博客:https://www.cnblogs.com/hanjiali/p/14088844.html

    9.关闭容器 

    • docker container stop  1e42240498a7 
    • 停止容器
    • [root@localhost ~]# docker container stop  1e42240498a7 
      1e42240498a7
      

       

    10.容器前台转后台运行

    • curl+p,q  将容器丢到后台运行  
    • docker run后面追加-d=true或者-d,那么容器将会运行在后台模式。

    11.容器的导出

    • docker export 1e4 > /Carrie/nginx.tar
    • [root@localhost ~]# docker container ps
      CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
      1e42240498a7        nginx               "/docker-entrypoint.…"   15 hours ago        Up 8 minutes        0.0.0.0:8080->80/tcp   Carrie_nginx
      [root@localhost ~]# docker export 1e4 > /Carrie/nginx.tar
      [root@localhost ~]# ls /Carrie/nginx.tar 
      /Carrie/nginx.tar
      

    12.容器的导入

    • docker import /Carrie/nginx.tar
    • [root@localhost ~]# docker container ps
      CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
      1e42240498a7        nginx               "/docker-entrypoint.…"   15 hours ago        Up 8 minutes        0.0.0.0:8080->80/tcp   Carrie_nginx
      [root@localhost ~]# docker rm -f 1e4
      1e4
      [root@localhost ~]# docker container ls -a
      CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
      
      [root@localhost ~]# docker import /Carrie/nginx.tar
      sha256:7959eb583395d45ecb7d2e152061ade41742c3cf5f208d5f49c8d270ad9247bb
      
      [root@localhost ~]# docker images 
      REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
      <none>              <none>              7959eb583395        29 seconds ago      131MB
      [root@localhost ~]# docker tag 7959eb583395 nginx:Carrie
      [root@localhost ~]# docker images 
      REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
      nginx               Carrie              7959eb583395        3 minutes ago       131MB
      

        

  • 相关阅读:
    结构型模式(一) 适配器模式
    选择器
    CSS引入
    CSS语法
    CSS介绍
    HTML练习
    HTML标签嵌套规则(重点)
    HTML标签分类(重点)
    HTML标签属性
    body标签
  • 原文地址:https://www.cnblogs.com/hanjiali/p/14078404.html
Copyright © 2020-2023  润新知