Docker介绍以及实战教程

Docker简介

  • Docker为什么出现
    • 从事软件开发的朋友,可能经常会碰到以下场景:
    • 运维:你这程序有Bug啊,怎么跑不起来啊!
    • 开发:我机子上能跑啊,你会不会用啊
    • 究其原因还是开发环境与生产环境不同造成的,比如编译器,数据库版本不一致或者缺少某些组件。运维就得重新部署环境,费时费力。
    • 那么就有人想,能不能从根本上解决这个问题,软件可以带环境安装?也就是说,安装的时候,把原始环境一模一样地复制过来?当然可以,答案就是Docker。
    • Docker可以把开发环境除了系统外,所有的东西都打包成一个镜像,然后交给运维,运维在生产环境上安装这个镜像后,就能保证生产环境和开发环境完全一致,就可以完美解决环境不同导致程序无法正常运行的问题。并且部署到新机器上时,直接进行安装,不用再费时费力的进行环境的部署。
  • Docker是什么
    • Docker是基于Go语言实现的云开源项目。主要目标是 “Build, Ship and Run Any App, Anywhere”, 也就是通过对应用组件的封装、分发、部署、运行等生命周期的管理,使用户的APP及其运行环境能够做到 “一次镜像,处处运行”。
    • 解决了运行环境和配置问题的软件容器,方便做持续集成并有助于整体发布的容器虚拟化技术。
  • Docker能干什么
    • 传统的应用开发完成后,需要提供一堆安装程序和配置说明文档,安装部署后需根据配置文档进行繁琐的配置才能正常运行。Docker化之后只需交付少量容器镜像文件,在正式生产环境加载镜像并运行即可,应用安装配置在镜像里已经内置好,大大节省了部署配置和测试验证时间。

容器和虚拟机

  • 对于虚拟机技术来说,传统的虚拟机需要模拟整台机器包括硬件,每台虚拟机都需要有自己的操作系统,虚拟机一旦被开启,预分配给他的资源将全部被占用。每一个虚拟机包括应用,必要的二进制和库,以及一个完整的用户操作系统
  • 容器技术和我们的宿主机共享硬件资源及操作系统,可以实现资源的动态分配。容器包含应用和其所有的依赖包,但是与其他容器共享内核。容器在宿主机操作系统中,在用户空间以分离的进程运行。容器内没有自己的内核,也没有进行硬件虚拟。
    请添加图片描述

    虚拟机 容器
    启动速度 分钟级 秒级
    运行性能 5%左右损失 接近原生
    磁盘占用 GB MB
    隔离性 系统级 进程级
    封装程度 完整的操作系统 只打包代码和依赖环境,共享宿主机内核

Docker网站

Docker三要素

  • 镜像(image)
    • 镜像可以用来创建Docker容器,一个镜像可以创建多个容器。把镜像比作类,容器就是用类实例化出来的类对象。
  • 容器(container)
    • Docker利用容器独立运行一个或一组应用,应用程序或者服务运行在容器里面,容器就类似于一个虚拟化的运行环境,容器是用镜像创建的运行实例。容器为镜像提供了一个标准的和隔离的运行环境,它可以被启动、开始、停止、删除。每个容器都是相互隔离的、保证安全的平台。
  • 仓库(repository)
    • 集中存放镜像文件的地方

Docker安装

  • centos安装docker流程
  • 安装准备
    • centos安装docker要注意版本至少是centos7
  • 卸载旧版本
    • 如果已经安装docker,执行以下命令卸载
    •  sudo yum remove docker \
                 docker-client \
                 docker-client-latest \
                 docker-common \
                 docker-latest \
                 docker-latest-logrotate \
                 docker-logrotate \
                 docker-engine
      
  • 安装gcc
    •   yum -y install gcc
        yum -y install gcc-c++
      
  • 安装软件包
    •   sudo yum install -y yum-utils
      
  • 设置stable镜像仓库
    •   sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
      
  • 更新yum软件包索引
    •    yum makecache fast
      
  • 安装docker
    •   sudo yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
      
  • 卸载docker
    •   	systemctl stop docker
        	sudo yum remove docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin 
        	sudo rm -rf /var/lib/docker
        	sudo rm -rf /var/lib/containerd
      

Docker测试

  • 启动docker
    • sudo systemctl start docker
  • 查看docker版本
    • docker version
    • 可以查看到docker版本信息,就说明安装成功了

配置阿里云容器镜像

  • 配置阿里云镜像可以在拉取镜像时提高速度
  • 阿里云官网
  • 在产品这里搜索容器镜像服务
    在这里插入图片描述
    • 选择免费试用
      在这里插入图片描述
    • 按照这里去操作即可
      在这里插入图片描述

Docker常用命令

  • 帮助启动类命令
    • 启动docker:systemctl start docker
    • 停止docker:systemctl stop docker
    • 重启docker:systemctl restart docker
    • 查看docker状态:systemctl status docker
    • 开机启动:systemctl enable docker
    • 查看docker概要信息:docker info
    • 查看docker帮助文档:docker --help
    • 查看docker命令详细帮助文档:docker 命令 --help
  • 镜像命令
    • 列举镜像 : docker images
      • REPOSITORY:镜像仓库源
      • TAG:镜像标签版本号,latest表示最新版本
      • IMAGE ID :镜像ID
      • CREATED :镜像创建时间
      • SIZE : 镜像大小
      • 示例
      •   [root@localhost lingp]# docker images
          REPOSITORY               TAG       IMAGE ID       CREATED       SIZE
          chenlicn163/hello-word   latest    3ed90589c0b6   4 years ago   844kB
        
    • 搜索镜像:docker search 镜像
      • NAME :镜像名
      • DESCRIPTION:镜像说明
      • STARS:点赞数
      • OFFICIAL:是否官方认证
      • AUTOMATED:是否是自动构建的
      •   [root@localhost lingp]# docker search hello-word
          NAME                                    DESCRIPTION                   STARS     OFFICIAL   AUTOMATED
          alexstruk/hello-wordpress                                             0                    
          chenlicn163/hello-word                  hello-word                    0                    [OK]
          princechopra/hello-word                                               0                    
          vaibhavmehta/hello-word-node            hello-word-node               0                    
          lyvy/hello-word                         hello-word spring boot        0                    
          ilfaugno/hello-word                                                   0                    
          sjolicoeur/hello-word                                                 0                    
          abhayjava/hello-word                    The dummy short description   0                    
          txg5214/hello-word                      hello-word                    0                    
          sjolicoeur/hello-word2                                                0                    
          yueshide/hello-word                                                   0                    
          rahulnit/hello-word                     hello-word                    0                    
          szaier75/hello-word                     hello-word                    0                    
          etokarev/hello-word-python                                            0                    
          cpangam/hello-word                                                    0                    
          geekmc/hello-word                                                     0                    
          bc14f103c716/hello-word                                               0                    
          rghorpade80/hello-word-docker-valaxy4                                 0                    
          wattwatt/hello-word                                                   0                    
          luoyangc/hello-word                                                   0                    
          hubikz/hello-worda                                                    0                    
          mselender/hello-word                                                  0                    
          thitikornc/hello-word                                                 0                    
          never001/hello-word                                                   0                    
          manyyaks/hello-word                                                   0         
        
    • 拉取镜像:git pull [镜像名字:TAG]
      • TAG可以不写,不写TAG就拉取最新的
      •   [root@localhost lingp]# docker pull chenlicn163/hello-word
          Using default tag: latest
          latest: Pulling from chenlicn163/hello-word
          19223ab0feef: Pull complete 
          Digest: sha256:2b7b800ab2ef8f0c2c3331ee170af82c3613b167d20e56d1884dca4772fee6c3
          Status: Downloaded newer image for chenlicn163/hello-word:latest
          docker.io/chenlicn163/hello-word:latest
          [root@localhost lingp]# docker images
          REPOSITORY               TAG       IMAGE ID       CREATED       SIZE
          chenlicn163/hello-word   latest    3ed90589c0b6   4 years ago   844kB
        
    • 查看镜像所占空间 : docker system df
    • 删除镜像 : docker rmi [镜像名:TAG 或者 IMAGE ID]
    • 删除所有镜像:docker rmi -f $(docker images -qa)
      • 这个命令知道就可以,可别随便用
  • 容器命令
    • 前面已经介绍了,把镜像比作类,容器就是类对象。因此我们先要有一个镜像,我们先拉取一个ubuntu镜像
    •   [root@localhost lingp]# docker pull ubuntu
        Using default tag: latest
        latest: Pulling from library/ubuntu
        7b1a6ab2e44d: Pull complete 
        Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322
        Status: Downloaded newer image for ubuntu:latest
        docker.io/library/ubuntu:latest
        [root@localhost lingp]# docker images
        REPOSITORY               TAG       IMAGE ID       CREATED         SIZE
        ubuntu                   latest    ba6acccedd29   21 months ago   72.8MB
        chenlicn163/hello-word   latest    3ed90589c0b6   4 years ago     844kB
      
    • 新建启动容器 : docker run [OPTIONS] 镜像名:TAG COMMAND
      • OPTIONS可取以下值:
        • –name : 为容器指定一个名称
        • -d : 后台运行容器并返回容器ID,也即启动守护式容器
        • -i :以交互模式运行容器,通常与-t同时使用
        • -t : 为容器重新分配一个伪输入终端,通常与-i同时使用
        • -P :随机端口映射
        • -p : 指定端口映射
      •   [root@localhost lingp]# docker images
          REPOSITORY               TAG       IMAGE ID       CREATED         SIZE
          ubuntu                   latest    ba6acccedd29   21 months ago   72.8MB
          chenlicn163/hello-word   latest    3ed90589c0b6   4 years ago     844kB
          [root@localhost lingp]# docker run -it --name=myubuntu ubuntu:latest /bin/bash
          root@1a139f4915df:/#
        
      • TAG可以不写,默认就是最新的。这个命令表示使用镜像ubuntu:latest以交互模式启动一个容器,在容器内执行/bin/bash命令。并给这个容器起名字myname,如果不指定name,系统会自动分配一个。这里说下-d参数的作用,不加-d,那么关闭当前容器终端,容器就会关闭,加-d,关闭当前容器终端,容器不会关闭,依旧在后台运行。
      • 这个时候我们就进入了ubuntu镜像开启的容器中,可以看下版本。此时我们新建和启动的这个容器就可以看成一个真实的ubuntu系统。
      •   root@1a139f4915df:/# cat /etc/issue
          Ubuntu 20.04.3 LTS \n \l
        
          root@1a139f4915df:/# 
        
    • 列出所有容器 : docker ps [OPTIONS]
      • OPTIONS可取以下值
        • -a : 列出当前所有正在运行的容器和历史运行过的
        • -l : 显示最近创建的容器
        • -n : 显示最近n个创建的容器
        • -q : 静默模式,只显示容器编号
        • –no-trunc : 表示不省略信息展示
      • 新开一个终端,执行命令。直接执行docker ps,只显示已启动的容器
      •   [root@localhost lingp]# docker ps -a
          CONTAINER ID   IMAGE           COMMAND       CREATED          STATUS                      PORTS     NAMES
          1a139f4915df   ubuntu:latest   "/bin/bash"   6 minutes ago    Up 6 minutes                          myubuntu
          21c28993fefb   ubuntu:latest   "/bin/bash"   11 minutes ago   Exited (0) 7 minutes ago              infallible_dirac
          da45904d7f38   ubuntu          "/bin/bash"   24 minutes ago   Exited (0) 12 minutes ago             compassionate_thompson
          3e6cfc10ad0d   ubuntu          "/bin/bash"   29 minutes ago   Exited (0) 24 minutes ago             adoring_lalande
        
    • 退出容器
      • 先要进入容器
      • exit : 退出后,容器会停止
      • ctrl + p + q : 退出后,容器不会停止
    • 启动容器 : docker start 容器ID或容器名
      •   [root@localhost lingp]# docker ps -a
          CONTAINER ID   IMAGE           COMMAND       CREATED          STATUS                       PORTS     NAMES
          1a139f4915df   ubuntu:latest   "/bin/bash"   18 minutes ago   Exited (127) 5 seconds ago             myubuntu
          21c28993fefb   ubuntu:latest   "/bin/bash"   24 minutes ago   Exited (0) 19 minutes ago              infallible_dirac
          da45904d7f38   ubuntu          "/bin/bash"   36 minutes ago   Exited (0) 24 minutes ago              compassionate_thompson
          3e6cfc10ad0d   ubuntu          "/bin/bash"   42 minutes ago   Exited (0) 36 minutes ago              adoring_lalande
          [root@localhost lingp]# docker start 1a139f4915df
          1a139f4915df
          [root@localhost lingp]# docker ps -a
          CONTAINER ID   IMAGE           COMMAND       CREATED          STATUS                      PORTS     NAMES
          1a139f4915df   ubuntu:latest   "/bin/bash"   19 minutes ago   Up 3 seconds                          myubuntu
          21c28993fefb   ubuntu:latest   "/bin/bash"   24 minutes ago   Exited (0) 20 minutes ago             infallible_dirac
          da45904d7f38   ubuntu          "/bin/bash"   36 minutes ago   Exited (0) 25 minutes ago             compassionate_thompson
          3e6cfc10ad0d   ubuntu          "/bin/bash"   42 minutes ago   Exited (0) 36 minutes ago             adoring_lalande
        
    • 重启容器 : docker restart 容器ID或容器名
    • 停止容器:docker stop 容器ID或容器名
    • 强制停止容器 : docker kill 容器ID或容器名
    • 删除已停止的容器 : docker rm 容器ID或容器名
      •   [root@localhost lingp]# docker ps -a
          CONTAINER ID   IMAGE           COMMAND       CREATED          STATUS                      PORTS     NAMES
          1a139f4915df   ubuntu:latest   "/bin/bash"   21 minutes ago   Up 2 minutes                          myubuntu
          21c28993fefb   ubuntu:latest   "/bin/bash"   27 minutes ago   Exited (0) 22 minutes ago             infallible_dirac
          da45904d7f38   ubuntu          "/bin/bash"   39 minutes ago   Exited (0) 27 minutes ago             compassionate_thompson
          3e6cfc10ad0d   ubuntu          "/bin/bash"   45 minutes ago   Exited (0) 39 minutes ago             adoring_lalande
          [root@localhost lingp]# docker rm 3e6cfc10ad0d
          3e6cfc10ad0d
          [root@localhost lingp]# docker rm da45904d7f38
          da45904d7f38
          [root@localhost lingp]# docker ps -a
          CONTAINER ID   IMAGE           COMMAND       CREATED          STATUS                      PORTS     NAMES
          1a139f4915df   ubuntu:latest   "/bin/bash"   22 minutes ago   Up 3 minutes                          myubuntu
          21c28993fefb   ubuntu:latest   "/bin/bash"   27 minutes ago   Exited (0) 23 minutes ago             infallible_dirac
        
    • 强制删除所有容器 : docker rm -f $(docker ps -a -q)
      • 谨慎使用
    • 查看容器日志 : docker logs 容器ID
    • 查看容器内运行的进程:docker top 容器ID
    • 查看容器内部细节 : docker insepct 容器ID
    • 进入已启动容器
      • docker exec -it 容器ID /bin/bash
        • 在容器中打开新的终端,并且可以启动新的进程。用exit退出,不会导致容器的停止。
      • docker attach 容器ID
        • 直接进入容器启动命令的终端,不会启动新的进程。用exit退出,会导致容器的停止。
      • 简单说就是exec进入容器后,执行exit退出,容器不会停止。attach进入容器,执行exit退出,容器会停止。推荐使用exec进入容器。
    • 容器和主机之间文件拷贝
      • 容器到主机 : docker cp 容器ID:容器中文件路径 主机路径
      • 主机到容器 : docker cp 主机文件路径 容器ID:容器中文件路径
    • 导入和导出容器
      • 导出容器 : docker export 容器ID > ./myubuntu.tar
      • 导入容器 : cat myubuntu.tar | docker import -镜像用户/镜像名:镜像版本号

提交带环境的镜像

  • 让我们回到文章开头那个场景,运维人员使用的环境要运行开发人员的程序,就要保证和开发人员的环境一致,比如开发人员是在ununtu上开发的,使用的环境有vim,gcc等工具。但运维人员的环境上是没有的,那么开发人员就可以将自己的整个开发环境打包提交
  • 在上面的介绍中,我们拉取了一个ubuntu镜像,并开启了一个容器。默认这个容器是没有vim,gcc等工具,我们在这个容器中安装vim,gcc后,打包成一个镜像并提交。
  • 提交命令
    • docker commit -m=“提交的描述信息” -a=“作者” 容器ID 要创建的目标镜像名:[标签名]
    •   [root@localhost lingp]# docker images
        REPOSITORY               TAG       IMAGE ID       CREATED         SIZE
        ubuntu                   latest    ba6acccedd29   21 months ago   72.8MB
        chenlicn163/hello-word   latest    3ed90589c0b6   4 years ago     844kB
        [root@localhost lingp]# docker ps -a
        CONTAINER ID   IMAGE           COMMAND       CREATED       STATUS                     PORTS     NAMES
        8f9846cbdfb2   ubuntu:latest   "/bin/bash"   2 hours ago   Up 15 minutes                        myubuntud
        8a9c1c2704eb   ubuntu:latest   "/bin/bash"   2 hours ago   Exited (129) 2 hours ago             myubuntu
        [root@localhost lingp]# docker commit -m="add vim and gcc" -a="graywolf" 8f9846cbdfb2 grayubuntu:1.0.0
        sha256:56df3dc1d8b1f94583bc7ab59b4f0b2f402dbaa2869dd558b1630ce763eeb7a6
        [root@localhost lingp]# docker images
        REPOSITORY               TAG       IMAGE ID       CREATED         SIZE
        grayubuntu               1.0.0     56df3dc1d8b1   8 seconds ago   342MB
        ubuntu                   latest    ba6acccedd29   21 months ago   72.8MB
        chenlicn163/hello-word   latest    3ed90589c0b6   4 years ago     844kB
      
    • ubuntu是原始的镜像,grayubuntu是我们安装vim和gcc后的镜像。可以比对下,大小就发生变化了。运维拿到这个新镜像grayubuntu,启动容器后,就可以直接在容器中运行我们的程序了。

镜像发布到阿里云

  • 发布到阿里云
    • 进到阿里云官网容器镜像服务这块
      在这里插入图片描述
      在这里插入图片描述
      在这里插入图片描述
      在这里插入图片描述
    • 首先创建一个命名空间
      在这里插入图片描述
      在这里插入图片描述
    • 在命名空间下创建镜像仓库
      在这里插入图片描述
      在这里插入图片描述
    • 选择本地仓库
      在这里插入图片描述
    • 创建镜像仓库成功后,阿里云会告诉我们如何拉取和推送镜像
      在这里插入图片描述
    • 我们在终端直接执行命令
    • 先进行登录
    •   [root@localhost lingp]# docker login --username=littleGrayWolf registry.cn-hangzhou.aliyuncs.com
        Password: 
        WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
        Configure a credential helper to remove this warning. See
        https://docs.docker.com/engine/reference/commandline/login/#credentials-store
        
        Login Succeeded
      
    • 推送到阿里云镜像仓库
    •   [root@localhost lingp]# docker images
        REPOSITORY               TAG       IMAGE ID       CREATED          SIZE
        grayubuntu               1.0.0     56df3dc1d8b1   31 minutes ago   342MB
        ubuntu                   latest    ba6acccedd29   21 months ago    72.8MB
        chenlicn163/hello-word   latest    3ed90589c0b6   4 years ago      844kB
        [root@localhost lingp]# docker tag 56df3dc1d8b1 registry.cn-hangzhou.aliyuncs.com/littlegraywolf/ubuntu1.0:1.0.0
        [root@localhost lingp]# docker push registry.cn-hangzhou.aliyuncs.com/littlegraywolf/ubuntu1.0:1.0.0
        The push refers to repository [registry.cn-hangzhou.aliyuncs.com/littlegraywolf/ubuntu1.0]
        2d9e91b2c941: Pushed 
        9f54eef41275: Pushed 
        1.0.0: digest: sha256:1dd42bfd2491b694c6d0fa5971c066d37192135d212b42faa6fb961e139e6849 size: 742
      
    • 这个时候阿里云上就有我们的镜像了
      在这里插入图片描述
    • 可以测试下,先删除本地镜像,然后从阿里云重新拉取
    •   [root@localhost lingp]# docker images
        REPOSITORY               TAG       IMAGE ID       CREATED         SIZE
        ubuntu                   latest    ba6acccedd29   21 months ago   72.8MB
        chenlicn163/hello-word   latest    3ed90589c0b6   4 years ago     844kB
        [root@localhost lingp]# docker pull registry.cn-hangzhou.aliyuncs.com/littlegraywolf/ubuntu1.0:1.0.0
        1.0.0: Pulling from littlegraywolf/ubuntu1.0
        7b1a6ab2e44d: Already exists 
        d2cb84f992fd: Pull complete 
        Digest: sha256:1dd42bfd2491b694c6d0fa5971c066d37192135d212b42faa6fb961e139e6849
        Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/littlegraywolf/ubuntu1.0:1.0.0
        registry.cn-hangzhou.aliyuncs.com/littlegraywolf/ubuntu1.0:1.0.0
        [root@localhost lingp]# docker images
        REPOSITORY                                                   TAG       IMAGE ID       CREATED          SIZE
        registry.cn-hangzhou.aliyuncs.com/littlegraywolf/ubuntu1.0   1.0.0     56df3dc1d8b1   41 minutes ago   342MB
        ubuntu                                                       latest    ba6acccedd29   21 months ago    72.8MB
        chenlicn163/hello-word                                       latest    3ed90589c0b6   4 years ago      844kB
      
    • 这里测试使用的是个人版服务,可以免费使用。如果是企业使用,可以付费购买企业版。

镜像发布到私有库

  • 阿里云毕竟是第三方公司,将自己公司的东西放到阿里云还是不够安全的。因此一般来说,公司都会在内部搭建私有镜像仓库,提供给公司内部人员使用。

  • 搭建私有镜像仓库

  • 下载镜像 Docker Registry : docker pull registry

    •   [root@localhost lingp]# docker pull registry
        Using default tag: latest
        latest: Pulling from library/registry
        79e9f2f55bf5: Pull complete 
        0d96da54f60b: Pull complete 
        5b27040df4a2: Pull complete 
        e2ead8259a04: Pull complete 
        3790aef225b9: Pull complete 
        Digest: sha256:169211e20e2f2d5d115674681eb79d21a217b296b43374b8e39f97fcf866b375
        Status: Downloaded newer image for registry:latest
        docker.io/library/registry:latest
        [root@localhost lingp]# docker images
        REPOSITORY                                                   TAG       IMAGE ID       CREATED          SIZE
        registry.cn-hangzhou.aliyuncs.com/littlegraywolf/ubuntu1.0   1.0.0     56df3dc1d8b1   56 minutes ago   342MB
        registry                                                     latest    b8604a3fe854   20 months ago    26.2MB
        ubuntu                                                       latest    ba6acccedd29   21 months ago    72.8MB
        chenlicn163/hello-word                                       latest    3ed90589c0b6   4 years ago      844kB
      
  • 运行私有库镜像Registry

    •   [root@localhost lingp]# docker run -d -p 5000:5000 --privileged=true registry
        ccbe13e502cd4e9f85d08086aebbe0c5a4afff7eefc730209ac6f5004580fa60
        [root@localhost lingp]# docker ps
        CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS         PORTS                                       NAMES
        ccbe13e502cd   registry   "/entrypoint.sh /etc…"   7 seconds ago   Up 6 seconds   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   condescending_sammet
      
  • 查看私有库中的镜像

    •   [root@localhost lingp]# curl -XGET http://192.168.206.136:5000/v2/_catalog
        {
              
              "repositories":[]}
      
  • 修改tag

    • 修改下tag,符合私有仓库规范
    • docker tag 镜像名:TAG ip:port/镜像名:TAG
    •   [root@localhost lingp]# docker images
        REPOSITORY                                                   TAG       IMAGE ID       CREATED          SIZE
        grayubuntu2                                                  2.0.0     bcc9dba161d0   52 minutes ago   185MB
        registry.cn-hangzhou.aliyuncs.com/littlegraywolf/ubuntu1.0   1.0.0     56df3dc1d8b1   2 hours ago      342MB
        registry                                                     latest    b8604a3fe854   20 months ago    26.2MB
        ubuntu                                                       latest    ba6acccedd29   21 months ago    72.8MB
        chenlicn163/hello-word                                       latest    3ed90589c0b6   4 years ago      844kB
        [root@localhost lingp]# docker tag grayubuntu2:2.0.0 192.168.206.136:5000/grayubuntu2:2.0.0
        [root@localhost lingp]# docker images
        REPOSITORY                                                   TAG       IMAGE ID       CREATED          SIZE
        192.168.206.136:5000/grayubuntu2                             2.0.0     bcc9dba161d0   52 minutes ago   185MB
        grayubuntu2                                                  2.0.0     bcc9dba161d0   52 minutes ago   185MB
        registry.cn-hangzhou.aliyuncs.com/littlegraywolf/ubuntu1.0   1.0.0     56df3dc1d8b1   2 hours ago      342MB
        registry                                                     latest    b8604a3fe854   20 months ago    26.2MB
        ubuntu                                                       latest    ba6acccedd29   21 months ago    72.8MB
        chenlicn163/hello-word                                       latest    3ed90589c0b6   4 years ago      844kB
      
      
  • 修改配置使之支持http

    • 默认不支持http推送,可以修改下配置让其支持
    • vim /etc/docker/daemon.json
    •   {
              
              
        	  "registry-mirrors": ["https://ssfzdz4k.mirror.aliyuncs.com"],
        	  "insecure-registries":["192.168.206.136:5000"]
        }
      
    • 修改完后重启docker
    •   systemctl restart docker
      
    • 启动下私服仓库
    •   docker run -d -p 5000:5000 -v /opt/data/registry:/tmp/registry --privileged=true registry
      
  • 推送本地镜像到私有仓库

    • docker push 镜像名:TAG
    •   [root@localhost lingp]# docker push 192.168.206.136:5000/grayubuntu2:2.0.0
        The push refers to repository [192.168.206.136:5000/grayubuntu2]
        07c695add28a: Pushed 
        9f54eef41275: Pushed 
        2.0.0: digest: sha256:39ace23746821bff7798f32bbce0544789019485c231ea08aca15384cc0afe68 size: 741
        [root@localhost lingp]# curl -XGET http://192.168.206.136:5000/v2/_catalog
        {
              
              "repositories":["grayubuntu2"]}
      
  • 拉取私有仓库镜像到本地

    • docker pull 镜像名:TAG
    •   [root@localhost lingp]# docker images
        REPOSITORY                                                   TAG       IMAGE ID       CREATED         SIZE
        registry.cn-hangzhou.aliyuncs.com/littlegraywolf/ubuntu1.0   1.0.0     56df3dc1d8b1   2 hours ago     342MB
        registry                                                     latest    b8604a3fe854   20 months ago   26.2MB
        ubuntu                                                       latest    ba6acccedd29   21 months ago   72.8MB
        chenlicn163/hello-word                                       latest    3ed90589c0b6   4 years ago     844kB
        [root@localhost lingp]# git pull 192.168.206.136:5000/grayubuntu2:2.0.0
        bash: git: command not found...
        [root@localhost lingp]# docker pull 192.168.206.136:5000/grayubuntu2:2.0.0
        2.0.0: Pulling from grayubuntu2
        7b1a6ab2e44d: Already exists 
        0cdd9d7ee9f3: Pull complete 
        Digest: sha256:39ace23746821bff7798f32bbce0544789019485c231ea08aca15384cc0afe68
        Status: Downloaded newer image for 192.168.206.136:5000/grayubuntu2:2.0.0
        192.168.206.136:5000/grayubuntu2:2.0.0
        [root@localhost lingp]# docker images
        REPOSITORY                                                   TAG       IMAGE ID       CREATED             SIZE
        192.168.206.136:5000/grayubuntu2                             2.0.0     bcc9dba161d0   About an hour ago   185MB
        registry.cn-hangzhou.aliyuncs.com/littlegraywolf/ubuntu1.0   1.0.0     56df3dc1d8b1   2 hours ago         342MB
        registry                                                     latest    b8604a3fe854   20 months ago       26.2MB
        ubuntu                                                       latest    ba6acccedd29   21 months ago       72.8MB
        chenlicn163/hello-word                                       latest    3ed90589c0b6   4 years ago         844kB
      
    • 至此,运维就可以拉取开发上传到私有库的镜像,去在其他机器上跑我们的程序了。

猜你喜欢

转载自blog.csdn.net/new9232/article/details/131860751