❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

发布时间:2022-07-03 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了❤�%的读者❤️都会收藏的Docker入门到实战的技术文章脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

文章目录

  • 虚拟化docker技术详解(二)
    • 关于作者
    • 作者介绍
    • 前言
    • 一、Docker 概念简述
      • 1.1 Docker 是什么?
      • 1.2 Docker 版本及支持平台
      • 1.3 为什么要使用Docker 容器技术
      • 1.4 Docker VS 传统虚拟化
      • 1.5 Docker 应用场景
      • 1.6 Docker 设计目标
      • 1.7 Docker 技术架构
      • 1.8 Docker 文件系统类型介绍
      • 1.9 Docker 镜像、容器、仓库核心概念
      • 1.10 Docker 安装
    • 二、Centos 7 构建Docker 平台实战
      • 2.1 实验环境
      • 2.2 Docker 部署
      • 2.3 Docker 基础用法
    • 三、Docker 镜像管理
      • 3.1 镜像是什么?
      • 3.2 配置加速器
      • 3.3 镜像常用管理命令
    • 三、Docker 容器管理
      • 3.1 创建容器常用选项
      • 3.2 常用管理命令
      • 3.3 容器和镜像命令实战
        • 3.3.1 镜像命令实战总结
        • 3.3.2 容器命令实战总结
        • 3.3.3 资控制命令总结
      • 3.4 容器数据持久化
        • 3.4.1 为什么要容器数据持久化
        • 3.4.2 数据持久化操作案例
      • 3.5 Docker 容器网络
        • 3.5.1 docker 容器网络通信原理及配置
          • 3.5.1.1 外部访问容器 `iptables -t nat -vnL DOCKER`
          • 3.5.1.2 容器访问外部` iptables -t nat -vnL POSTROUTING`
          • 3.5.1.3 通讯方式一:端口映射|暴露
          • 3.5.1.4 通讯方式二:容器互联
        • 3.5.2 Docker 4种 网络模式
          • 3.5.2.1 bridge 模式
          • 3.5.2.2 host 模式
          • 3.5.2.3 none 模式
          • 3.5.2.4 Container模式
        • 3.5.3 配置网络模式
          • 3.5.3.1 配置默认桥接bridge模式:默认桥接到 docker 0
          • 3.5.3.2 配置host模式:使用 --net=host指定
          • 3.5.3.3 配置none模式
          • 3.5.3.4 配置container模式:使用 --net=container:container_id/container_name
          • 3.5.3.5 自定义配置桥接模式、修改网段--- 实战案例
    • 四、Dockerfile构建镜像
      • 4.1 Dockerfile 概述
      • 4.2 Docker 常用指令
      • 4.3 构建镜像命令
      • 4.4 构建nginx 镜像--- 编译安装nginx
      • 4.5 Harbor镜像仓库搭建与使用
        • 4.5.1 什么是Harbor
        • 4.5.2 部署Harbor 环境说明
        • 4.5.3 部署Harbor 私有仓库步骤
          • 4.5.3.1 下载安装 Docker 和Docker Compose
          • 4.5.3.2 上传、解压 harbor离线安装包
          • 4.5.3.3 修改配置文件
          • 4.5.3.4 安装Harbor
        • 4.5.4 打标签、上传/拉取镜像
    • 五、总结

虚拟化技术之docker技术详解(二)

关于作者

  • 作者介绍


    🍊 博客主页:作者主页 🍊 简介云计算领域优质创作者🏆、学长的IT故事 公众号创作者🎓、在校期间参与众多计算机相关的省赛、国赛,斩获系列荣誉。考取华为资深工程师、红帽工程师等系列认证。

    🍊 关注我简历模板、学习资料、文档下载、技术支持 都可以关注、私信我哦! 让生命像一团热烈燃烧的火,直到死亡才能使它熄灭


❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

前言

  • 博文介绍

    ​ 回首这年写作之旅,将所学技术、项目实战的东西,分享给大家 。一路走来,小编从没有忘记初心,决心分享高质量的运维技术,不断提升自己,帮助linux爱好者。上一篇博文就给大家带来了超详细的docker 容器技术以及相关的虚拟化概念,今天接着介绍docker 基础用法,这篇文章文章大概1.5w字,从0到1 ,全面的介绍了docker,从入门到实战,如果学习过程中遇到问题,可以查看专栏介绍私信我,我会尽量帮助处理。

    ​ 本文将独立成立一个虚拟化技术专栏,后续持续更新虚拟化Docker、K8s等文章,欢迎大家订阅 往期:docker虚拟化技术概念(一)

一、Docker 概念简述

1.1 Docker 是什么?

  • 使用最广泛的开源容器引擎
  • 一种操作系统级的虚拟化技术、依赖于Linux内核特性:namespace(资源隔离)和Cgroups(资源限制)
  • 一个简单的应用程序打包工具

1.2 Docker 版本及支持平台

1.3 为什么要使用Docker 容器技术

  • 上线流程繁琐

    开发—> 测试 —> 申请资源 —> 审批 —> 部署 —> 测试

  • 资源利用率低,造成资源浪费

  • 扩容/缩容 不及时

  • 服务器环境臃肿,维护难度越来越困难

1.4 Docker VS 传统虚拟化

  • ❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

    # 传统虚拟化
    --->底层 计算机硬件资源(源、内存、CPU) 
    ---> Host OS (宿主机操作系统),好比自己的 笔记本的windows 系统
    --->HyPErvisor(运行在物理和虚拟机之间的,是虚拟化的核心,模拟多个虚拟机所具备的资源,包含虚拟机资源调度) ,好比你笔记本安装的VMware workstation 软件。
    ---> Guest OS 虚拟系统(自己安装的虚拟机),自己在VM 创建的虚拟主机
    ---> APP 系统中的程序|服务等
    
    # docker 容器技术
    --->底层 计算机硬件资源(电源、内存、CPU) 
    ---> Host OS (宿主机操作系统),好比自己的 笔记本的windows 系统
    --->docker Engine docker 引擎,相当于大船,运 集装箱的容器,少了Guest OS 子系统
    --->系统中的程序|服务等
    @H_777_370@ 
  • ❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

1.5 Docker 应用场景

  • 应用程序打包和发布
  • 应用程序隔离
  • 持续集成
  • 部署微服务
  • 快速搭建测试环境
  • 提供PaaS 产品(平台即服务)

1.6 Docker 设计目标

  • 提供简单的应用程序打包工具
  • 多环境保持一致性
  • 提高运维工作人员效率
  • ❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

1.7 Docker 技术架构

  • Docker 是基于C/S 端的

  • ❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

    # Docker Client
    执行docker 的命令
    
    # Host
    Docker的主机,安装完Docker 后,会有一个守护进程
    执行docker 命令后,Daemon 进程会使用本地镜像去创建 容器,启动容器
    如果本地没有这个镜像,它会从docker 镜像仓库中拉去镜像。
    
    # Docker Registry
    镜像
    
    

1.8 Docker 文件系统类型介绍

  • aufs

    AUFS是Docker最先使用的storage driver,Docker在Debian,Ubuntu系的系统中默认使用aufs,它技术很成熟,社区支持也很好,特点如下: 
     1.容器启动速度很快 
     2.存储空间利用很高效 
     3.内存的利用很高效
    
    AUFS是一种联合文件系统,意思是它将同一个主机下的不同目录堆叠起来(类似于栈)成为一个整体,对外提供统一的视图。AUFS是用联合挂载来做到一点。 
    
    AUFS使用单一挂载点将多个目录挂载到一起,组成一个栈,对外提供统一的视图,栈中的每个目录作为一个分支。栈中的每个目录包括联合挂载点都必须在同一个主机上。 
    在Docker中,AUFS实现了镜像的分层。AUFS中的分支对应镜像中的层。 
    
    此外,容器启动时创建的读写层也作为AUFS的一个分支挂载在联合挂载点上。
    
  • devicemapper

    `redhat系中使用device mapper。device mapper在Linux2.6内核中被并入内核,它很稳定,也有很好的社区支持。`device mapper将所有的镜像和容器存储在它自己的虚拟设备上,这些虚拟设备是一些支持写时复制策略的快照设备。device mapper工作在块层次上而不是文件层次上,这意味着它的`写时复制策略不需要拷贝整个文件`。 device mapper创建镜像的过程如下: 
    - 使用device mapper的storge driver创建一个精简配置池;精简配置池由块设备创建;- 接下来创建一个基础设备; 
    - 每个镜像和镜像层都是基础设备的快照;这写快照支持写时复制策略,这意味着它们起始都是空的,当有数据写入时才耗费空间。在device mapper作为storage driver的系统中,容器层container layer是它依赖的镜像的快照。与镜像一样,container layer也支持写时复制策略,它保存了所有对容器的更改。当有数据需要写入时,device mapper就为它们在资源池中分配空间; 
    
    写入数据:
    对容器的写操作由“需要时分配”策略完成。更新已有数据由“写时复制”策略完成,这些操作都在块的层次上完成,每个块的大小为64KB; 
    向容器写入56KB的新数据的步骤如下: 
    - 进程向容器发出写56KB数据的请求; 
    - device mapper的“需要时分配”策略分配一个64KB的块给容器快照(container snapshot);如果要写入的数据大于64KB,就分配多个大小为64KB的块。 
    - 将数据写入新分配的块中;
    
    
  • ❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

  • OverlayFS文件系统简介

    `OverlayFS是目前使用比较广泛的层次文件系统`,是一种类似Aufs的一种堆叠文件系统,于2014年正式合入Linux 3.18主线内核,OverlayFS文件系统,实现简单,而且性能很好,可以充分利用不同或则相同Overlay文件系统的Page Cache,具有:`上下合并、同名遮盖、写时拷贝`等特点。
    
    Docker虚拟化Overlay存储驱动利用了很多OverlayFS特性来构建和管理镜像与容器的磁盘结构。 从Docker1.12起,Docker也支持Overlay2存储驱动,相比于Overlay来说,Overlay2在inode优化上更加高效。但Overlay2驱动只兼容Linux kernel 4.0+以上的版本。
    OverlayFS加入Linux Kernel主线后,在Linux Kernel模块中的名称从Overlayfs改名为Overlay。在真
    
    实使用中,`OverlayFS代表整个文件系统,而Overlay/Overlay2表示Docker的存储驱动。`
    
    在Docker虚拟化技术中,OverlayFS将一个Linux主机中的两个目录组合起来,一个在上,一个在下,对外提供统一的视图。这两个目录就是层layer,将两个层组合在一起的技术被成为联合挂载Union mount。在OverlayFS中,上层的目录被称作upperdir,下层的,目录被称作lowerdir,对外提供的统一视图被称作merged。
    
  • ❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

1.9 Docker 镜像、容器、仓库核心概念

  • 镜像

    创建容器的基础,类似于虚拟机的快照功能,可以理解为一个面向docker容器引擎的只读模板,每个镜像都有一个tag标签,作为其标识;比如:一个镜像可以是一个完整的centos系统,称之为centos镜像;也可以是一个安装了MySQL的应用程序,称之为mySQL镜像;
    
  • 容器

    基于镜像所创建的实例,可以被启动、停止、删除,多个容器之间互相隔离,互不可见的,可以保证容器的进程隔离性与平台的安全性,也可以把容器看作成为一个简易的linux环境,可以利用容器来部署业务;
    
  • 仓库

    仓库作为docker集中存放镜像的位置,当创建了自己的镜像后,可以使用push命令将镜像上传到公共仓库(public)或者私有仓库(PRivate),这样下次再次使用时,直接从仓库pull下来即可;
    仓库注册服务器(registry)是存放仓库的地方,其中包含很多仓库,如果无私有仓库,则使用当前docker最大的公共仓库docker hub,其中存放了数量庞大的镜像供用户下载使用(注:docker hub的账号需要梯子注册,国内网络无法收验证码);
    
    

1.10 Docker 安装

  • Docker 官方文档(https://docs.docker wangt.cc )

    # yum 安装
    安装前建议关闭selinux和firewalld
     1.安装依赖包
        yum install -y yum-utils 
     2.添加Docker软件包源
        yum-config-manager --add-repo https://download.docker wangt.cc /linux/centos/docker -ce.repo
     3.安装Docker CE
        yum install -y docker -ce
    4.启动Docker服务并设置开机启动
        Systemctl start docker
        systemctl enable docker
    
    # 编译安装(忽略,一般人不用)
    
  • 阿里源:

    https://developer.aliyun wangt.cc /mirror/docker-ce

二、Centos 7 构建Docker 平台实战

2.1 实验环境

  • 环境介绍

  • 服务器名称版本IP地址
    docker-serverCentOS 7.610.0.0.8

2.2 Docker 部署

  • 安装步骤

    # 官方安装教程
    [root@docker-server~]#  yum install -y yum-utils
    [root@docker-server~]# yum-config-manager  --add-repo  https://download.docker wangt.cc /linux/centos/docker-ce.repo
    
    # 安装docker-ce
    yum install -y docker-ce
    
    # 查看docker 版本
    docker version
    docker info
    
  • ❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

2.3 Docker 基础用法

  • 基本使用

    # 查看docker 信息
    root@docker-server~]#docker version
    root@docker-server~]#docker info
    
    # 查看本地镜像
    [root@docker-server~]# docker images 
    REPOSITORY     TAG       IMAGE ID       CREATED      SIZE
    tomcat         latest    c662ee449a7e   4 days ago   680MB
    nginx          latest    f8f4ffc8092c   5 days ago   133MB
    rivers/nginx   v1        f8f4ffc8092c   5 days ago   133MB
    
    # 搜索镜像
    root@docker-server~]# docker seArch nginx
    
    # 下载镜像
    [root@docker-server~]# docker pull nginx
    
    # 运行docker 中的nginx 容器
    [root@web02_server~]# docker images
    REPOSITORY          TAG       IMAGE ID       CREATED       SIZE
    redis               latest    5d89766432d0   5 days ago    105MB
    nginx               latest    f8f4ffc8092c   5 days ago    133MB
    consol/tomcat-7.0   latest    7c34bafd1150   6 years ago   601MB
    
    [root@docker-server~]# docker run -itd nginx
    121962a620a5f18e56fb63e8CF19668f6d1be067eeccfcceaa27437355f0d499
    
    [root@web02_server~]# docker run -itd -p 8088:8080 consol/tomcat-7.0:latest
    3088f97bbd9af1d080b117B2B94fdea35f0461f5d9b0bba0e0c50eaabed8370e
    
    # 查看容器进程
    # docker ps 查看正在运行的 进程
    [root@docker-server~]# docker ps -a # 显示所有的容器,包括未运行的
    
    # 停止|重启|杀死 一个容器
     #1.通过CONTAINER ID 关闭 (ID通过docker ps查看)
    [root@web02_server~]# docker ps
    CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                   NAMES
    51399f33ff35   nginx     "/docker-entrypoint.…"   36 minutes ago   Up 36 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp   adoring_leavitt
    [root@web02_server~]# docker kill 51399f33ff35
    
     # 2.通过names stop|start 启动关闭
     
    [root@web02_server~]# docker run -d -p 8099:8080 --name nginx-test nginx
    bede0DFf127c16c61c4221d217d33ec84c6d317e6cfdc6d8c6afe54a9428145b
    [root@web02_server~]# docker stop nginx-test
    nginx-test
    [root@web02_server~]# docker start nginx-test
    nginx-test 
    
     3.通过dashboard页面关闭
    # 暴露一个端口,宿主机进行访问
    [root@docker-server~]# docker run -d -p 8080:80 nginx
    51399f33ff3563d9444796d926409ce51e509f7b9e652fce75c415082dc72b27
    
    浏览器输入:10.0.0.8:8080 即可访问容器里面的 nginx
    浏览器输入:http://10.0.0.8:8088/,即可访问tomcat
    

三、Docker 镜像管理

3.1 镜像是什么?

  • 一个分层存储的文件,不是一个单一的文件
  • 一个软件的环境
  • 一个镜像可以创建N个容器
  • 一种标准化的交付
  • 一个不包含Linux内核而又精简的Linux操作系统

3.2 配置加速器

Docker Hub是由Docker公司负责维护的公共镜像仓库,包含大量的容器镜像,Docker工具默认从这个公共镜像库下载镜像。

  • 地址:https://hub.docker wangt.cc

    # Docker镜像修改方法,vim /etc/docker/daemon.json,执行如下命令即可:
    
    vi /etc/docker/daemon.JSON 
    {
    "registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs wangt.cc "]
    }
    
    # 如果没有daemon.json文件,可以手动创建一个
    
    # 重启docker
    systemctl restart docker
    
    

3.3 镜像常用管理命令

  • docker 命令参数

    [root@docker-server~]# docker --help
    
    # docker 语法
    usage:  docker [OPTIONS] COMMAND
    
    A self-sufficient runtime for containers
    # 常用选项参数,基本用不到
    Options:
          --config string      Location of client config files
                               (default "/root/.docker")
      -c, --context string     Name of the context to use to connect
                               to the daemon (overrides DOCKER_HOST
                               env VAR and default context set with
                               "docker context use")
      -D, --debug              Enable debug mode
      -H, --host list          Daemon socket(s) to connect to
      -l, --LOG-level string   Set the logging level
                               ("debug"|"info"|"warn"|"error"|"fatal")
                               (default "info")
          --tls                Use TLS; implied by --tlsverify
          --tlscACERt string   Trust certs signed only by this CA
                               (default "/root/.docker/ca.pem")
          --tlscert string     Path to TLS certificate file (default
                               "/root/.docker/cert.pem")
          --tlskey string      Path to TLS key file (default
                               "/root/.docker/key.pem")
          --tlsverify          Use TLS and verify the remote
      -v, --version            Print version information and quit
    
    # docker 管理命令,后来分类细化后才有的
    Management Commands:
      app*        Docker App (Docker Inc., v0.9.1-beta3)
      builder     Manage builds
      buildx*     Build with BuildKit (Docker Inc., v0.6.1-docker)
      config      Manage Docker configs
      container   Manage containers
      context     Manage contexts
      image       Manage images
      manifest    Manage Docker image manifests and manifest lists
      network     Manage networks
      node        Manage Swarm nodes
      plugin      Manage plugins
      scan*       Docker Scan (Docker Inc., v0.8.0)
      secret      Manage Docker secrets
      service     Manage services
      stack       Manage Docker stacks
      swarm       Manage Swarm
      system      Manage Docker
      trust       Manage trust on Docker images
      volume      Manage volumes
    
    # docker 命令,开始只有command命令,没有管理命令
    Commands:
      attach      Attach local standard input, output, and error streams to a running container
      build       Build an image From a Dockerfile
      commit      Create a new image from a container's changes
      cp          Copy files/folders between a container and the local filesystem
      create      Create a new container
      diff        Inspect changes to files or directories on a container's filesystem
      events      Get real time events from the server
      exec        Run a command in a running container
      export      Export a container's filesystem as a tar archive
      history     Show the history of an image
      images      List images
      import      Import the contents from a tarball to create a filesystem image
      info        Display system-wide information
      inspect     Return low-level information on Docker objects
      kill        Kill one or more running containers
      load        Load an image from a tar archive or STDIN
      login       Log in to a Docker registry
      logout      Log out from a Docker registry
      logs        Fetch the logs of a container
      pause       Pause all processes within one or more containers
      port        List port mappings or a specific mapping for the container
      ps          List containers
      pull        Pull an image or a repository from a registry
      push        Push an image or a repository to a registry
      rename      Rename a container
      restart     Restart one or more containers
      rm          Remove one or more containers
      rmi         Remove one or more images
      run         Run a command in a new container
      save        Save one or more images to a tar archive (streamed to STDOUT by default)
      search      Search the Docker Hub for images
      start       Start one or more stopped containers
      stats       Display a live stream of container(s) resource usage statistics
      stop        Stop one or more running containers
      tag         Create a tag TargET_IMAGE that refers to SOURCE_IMAGE
      top         Display the running processes of a container
      unpause     Unpause all processes within one or more containers
      update      Update configuration of one or more containers
      version     Show the Docker version information
      wait        Block until one or more containers stop, then print their exit codes
    
    # docker命令主要分2部分,早期 只有command 这一部分,当发展到CE 版本,对docker 命令做了一个分类,形成了Management Commands 这一部分
    
  • 常用管理命令

    指令描述eg:
    ls列出本地镜像docker images ls
    build构建镜像来自Dockerfile后面详细介绍
    history查看镜像历史docker history nginx
    inspect现实一个或多个镜像详细信息docker inspect tomcat
    pull从镜像仓库拉取镜像docker pull nginx
    push推送一个镜像到镜像仓库docker push rivers/nginx:v1
    rm移除一个或多个镜像docker rm redis
    prune移除没有被标记或者没有任何容器引用的镜像,-a 就会删除所有本地镜像(docker image prune -a)docker image prune
    tag创建一个引用源镜像标记目标镜像docker tag nginx:latest rivers/nginx:v1
    save保存一个或多个镜像到一个tar 归档文件(假如某台不能上网,就在可以上网的机器上拉取,归档,scp传过去,加载)docker save nginx -o nginx.tar
    load加载镜像来自tar归档或标准输入docker load -i nginx.tar

三、Docker 容器管理

3.1 创建容器常用选项

  • 创建镜像常用选项

    选项描述
    -i, –interactive交互式
    -t, –tty分配一个伪终端
    -d, –detach运行容器到后台
    -e, –env设置环境变量
    -p, –publish list发布容器端口到主机
    -P, –publish-all发布容器所有EXPOSE的端口到宿主机随机端口
    –name string指定容器名称
    -h, –hostname设置容器主机名
    –ip string指定容器IP,只能用于自定义网络
    –network连接容器到一个网络
    -v, –volume list–mount mount(新方式)将文件系统附加到容器
    –restart string容器退出时重启策略,默认no,可选值:[always]
    -m,–memory容器可以使用的最大内存量
    –memory-swap允许交换到磁盘的内存量
    –memory-swappiness=<0-100>容器使用SWAP分区交换的百分比(0-100,默认为-1)
    –oom-kill-disable用OOM Killer
    –cpus可以使用的CPU数量
    –cpuset -cpus限制容器使用特定的CPU核心,如(0-3, 0,1)
    –cpu-sharesCPU共享(相对权重)

3.2 常用管理命令

  • 常用管理命令

    选项描述
    ls列出容器
    inspect查看一个或多个容器详细信息
    exec在运行容器中执行命令
    commit创建一个新镜像来自一个容器(用的少)
    cp拷贝文件/文件夹到一个容器
    logs获取一个容器日志
    port列出或指定容器端口映射
    top显示一个容器运行的进程
    stats显示容器资源使用统计
    stop/start/restart停止/启动一个或多个容器
    rm删除一个或多个容器
    prune移除已停止的容器

3.3 容器和镜像命令实战

3.3.1 镜像命令实战总结

  • 镜像命令

    #查找redis镜像
    [root@web02_server~]# docker search redis
    
    #下载镜像
    [root@web02_server~]# docker pull redis
    
    #查看本机镜像
    [root@web02_server~]# docker images
    REPOSITORY          TAG       IMAGE ID       CREATED       SIZE
    redis               latest    5d89766432d0   5 days ago    105MB
    nginx               latest    f8f4ffc8092c   5 days ago    133MB
    consol/tomcat-7.0   latest    7c34bafd1150   6 years ago   601MB
    [root@web02_server~]#
    
    #上传镜像
    [root@web02_server~]# docker push redisnew:latest
    
    #备份镜像为tar包
    [root@web02_server~]# docker save nginx -o nginx.tar
    
    #导入备份的镜像文件
    [root@web02_server~]# docker load -i nginx.tar
    
    #删除镜像(必须先删除该镜像启动的所有容器)
    [root@web02_server~]# docker rmi nginx:latest
    [root@web02_server~]# docker ps -l
    CONTAINER ID   IMAGE           COMMAND       CREATED          STATUS                     PORTS     NAMES
    f49824647c75   centos:latest   "/bin/bash"   11 minutes ago   Exited (1) 7 seconds ago             apache
    [root@web02_server~]# docker rm apache   # 删除自定义的apache 容器
    apache
    
    # 查看镜像的详细信息
    [root@web02_server~]# docker inspect redis:latest
    
    # 查看镜像的历史信息
    [root@web02_server~]# docker history redis:latest
    
    # 给镜像添加新的名词和标签
    [root@web02_server~]# docker tag redis:latest newname:newtag
    [root@web02_server~]# docker tag nginx:latest rivers/nginx:v1 
    
    

3.3.2 容器命令实战总结

  • 容器命令实战

    # 在后台启动容器
    [root@web02_server~]# docker run -itd nginx
    cb4c606fd62558321677a9b15b3be40390bdd0077da6a5ee3d9f884bb31d03a5
    
    
    # 在前台启动容器
    [root@web02_server~]# docker run -it --name Centos centos:latest /bin/bash
    [root@web02_server~]# ctrl+p, ctrl+q # 使用快捷键退出,保证容器不关闭
    [root@web02_server~]# docker ps -l    #查看最近启动的容器进程
    CONTAINER ID   IMAGE           COMMAND       CREATED              STATUS              PORTS     NAMES
    fef50558fd7f   centos:latest   "/bin/bash"   About a minute ago   Up About a minute             Centos
    [root@web02_server~]# 
    
    
    # 查看正在运行的容器
    [root@web02_server~]# docker ps
    CONTAINER ID   IMAGE                      COMMAND                  CREATED         STATUS         PORTS                                                 NAMES
    fef50558fd7f   centos:latest              "/bin/bash"              2 minutes ago   Up 2 minutes                                                         Centos
    cb4c606fd625   nginx                      "/docker-entrypoint.…"   4 minutes ago   Up 4 minutes   80/tcp                                                silly_curie
    3088f97bbd9a   consol/tomcat-7.0:latest   "/bin/sh -c /opt/tom…"   2 hours ago     Up 2 hours     8778/tcp, 0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   magical_lumiere
    
    # 只查看id
    [root@web02_server~]# docker ps -q
    fef50558fd7f
    cb4c606fd625
    3088f97bbd9a
    
    # 查看所有容器,包含未启动的
    [root@web02_server~]# docker ps -a
    
    # 启动、停止、重启容器
    [root@web02_server~]# docker stop Centos  #前提是启动时 加参数 --name 选项才可以 
    Centos
    [root@web02_server~]# docker start Centos
    Centos
    
    [root@web02_server~]# docker ps -q  # 只查看ID
    fef50558fd7f
    cb4c606fd625
    3088f97bbd9a
    [root@web02_server~]# docker stop fef50558fd7f
    fef50558fd7f
    [root@web02_server~]# docker restart fef50558fd7f
    fef50558fd7f
    [root@web02_server~]# 
    
    
    # 删除容器  -f 参数,强制删除
    [root@web02_server~]# docker rm -f fef50558fd7f
    fef50558fd7f
    [root@web02_server~]# 
    # 删除所有容器
    [root@web02_server~]#  docker rm -f $(docker ps -aq)
    
    # 进入容器的默认进程
    [root@web02_server~]# docker attach cb4c606fd625
    2021/10/04 07:51:40 [notice] 1#1: signal 28 (SIGWINCH) received
    2021/10/04 07:51:40 [notice] 1#1: signal 28 (SIGWINCH) received
    2021/10/04 07:51:40 [notice] 31#31: signal 28 (SIGWINCH) received
    2021/10/04 07:51:40 [notice] 31#31: signal 28 (SIGWINCH) received
    
    [root@web02_server~]# exit # 退出后容器会关闭
    
    # 查看容器详细信息
    [root@web02_server~]# docker inspect cb4c606fd625
    [root@web02_server~]# docker inspect 7f39d656b934|grep -i ipaddr  # 查看容器IP地址
                "SecondaryIPAddresses": null,
                "IPAddress": "172.17.0.3",
                        "IPAddress": "172.17.0.3",
    
    
    # 进入nginx容器,查看nginx路径,设置htML文件,访问测试
    [root@web02_server~]# docker ps -l
    CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                   NAMES
    7f39d656b934   nginx     "/docker-entrypoint.…"   3 minutes ago   Up 2 minutes   0.0.0.0:8000->80/tcp, :::8000->80/tcp   wizardly_tesla
    
    [root@web02_server~]# docker exec -it 7f39d656b934 /bin/bash 
    root@7f39d656b934:/# cd /usr/share/nginx/html/     #进入nginx网站目录
    root@7f39d656b934:/usr/share/nginx/html# ls
    50x.html  index.html
    root@7f39d656b934:/usr/share/nginx/html# 
    root@7f39d656b934:/usr/share/nginx/html# echo "<h1>this nginx Container</h1>" > index.html # 准备测试文件内容
    root@7f39d656b934:/usr/share/nginx/html# exit
    
    # 测试访问
    [root@web02_server~]# curl 0 10.0.0.8:8000/index.html
    curl: (7) Failed connect to 0:80; Connection refused
    <h1>this nginx Container</h1>
    [root@web02_server~]# 
    
    # 从容器内拷贝首页文件到宿主机,修改后拷贝回容器内
    [root@web02_server~]# docker cp 7f39d656b934:/usr/share/nginx/html/index.html .
    
    [root@web02_server~]# ls
    anaconda-ks.cfg  backup  index.html  nohup.out  szz-job-v1.jar
    [root@web02_server~]# vim index.html 
    <h1>this nginx Container</h1>
    <h2>在宿主机上修改的index.html</h2>
    
    # 将本地index.html 拷贝到docker容器里面
    [root@web02_server~]# docker cp ./index.html 7f39d656b934:/usr/share/nginx/html/index.html
    [root@web02_server~]#
    
    # 测试
    [root@web02_server~]# docker inspect 7f39d656b934|grep -i ipaddr
                "SecondaryIPAddresses": null,
                "IPAddress": "172.17.0.3",
                        "IPAddress": "172.17.0.3",
    [root@web02_server~]# curl http://172.17.0.3
    <h1>this nginx Container</h1>
    <h2>在宿主机上修改的index.html</h2>
    [root@web02_server~]# 
    
    # 启动一个nginx  容器
    [root@web02_server~]# docker run -d -e evn=prod -p 88:80 --name web01 -h nginx-server --restart=always nginx
    3d175068754b1e1d5d14970e5f41b35c6cbcb37427b9cf83e4a284ec377aeed0
    
    -e 环境变量 evn
    --name  设置名称 web01
    -h 设置 主机名 nginx-server
    -- restart=always 设置开机启动,docker 重启了,设置了restart=always,docker 也会自动拉起nginx服务
    echo $evn
    prod
    
    [root@web02_server~]# docker exec -it web01 /bin/bash
    root@nginx-server:/# echo $evn
    prod
    
    root@nginx-server:/# hostname 
    nginx-server
    root@nginx-server:/# 
    
    
    # 只查看web01 容器运行的端口
    [root@web02_server~]# docker port web01
    80/tcp -> 0.0.0.0:88
    80/tcp -> :::88
    [root@web02_server~]# 
    
    # 不进入容器,查看容器利用率
    [root@web02_server~]# docker stats web01 
    
    CONTAINER ID   NAME      CPU %     MEM USAGE / LIMIT     MEM %     NET I/O     BLOCK I/O    PIDS
    3d175068754b   web01     0.00%     1.719MiB / 904.6MiB   0.19%     65 / 0B   3.2MB / 0B   2
    ^C
    
    [root@web02_server~]# docker stats web01 --no-stream|awk '{print $3}'
    NAME
    0.00%
    
    [root@web02_server~]# docker stats web01 --no-stream|awk 'NR==2{print $3}'
    
    0.00%
    

3.3.3 资源控制命令总结

  • 资源限制

    # 限制redis 容器使用资源,非常有必要,最多使用500M,最多使用1个cpu
    [root@web02_server~]# docker run -m="500m" --cpus="1" -d redis
    ce392030c634d7206b51a11af880492232ae48ba8d7d36b4e6e30e07af17238c
    [root@web02_server~]# 
    
    # 限制CPU的使用速率
    [root@web02_server~]# docker run --cpu-quota 20000 redis
    
    # 据多个容器设置其占用CPU的比例,如下为1:1:2:4
    [root@web02_server~]# docker run --cpu-shares 1024 容器1^C
    [root@web02_server~]# docker run --cpu-shares 1024 容器2^C
    [root@web02_server~]# docker run --cpu-shares 2048 容器3^C
    [root@web02_server~]# docker run --cpu-shares 4096 容器4^C
    
    # 根据多个容器进行绑定所使用的CPU的核心,如下为八颗CPU,分配给两个容器:
    [root@web02_server~]# docker run --cpuset-cpus 0,1,2,3 容器1^C
    [root@web02_server~]# docker run --cpuset-cpus 4,5,6,7 容器2^C
    
    #内存控制
    #限制容器1使用的内存为512M;
    [root@web02_server~]# docker run -m 512m 容器1^C
    
    
    #blkio的控制:
    如果一台服务器上多个容器的部署,会出现同时又多个程序进程读写磁盘数据的情况;
    [root@web02_server~]# docker run --device-write-bps /dev/sda1:1mb 容器1^C
    [root@web02_server~]# docker run --device-read-bps /dev/sda1:1mb 容器2^C
    

3.4 容器数据持久化

3.4.1 为什么要容器数据持久化

  • 为什么要数据持久化

    # 当我们在虚拟机中创建了容器后,如果产生了数据,我们删除了容器,数据就会丢失,下次从新创建容器,就是默认的,之前配置的就会丢失,所以需要数据持久化。创建容器后,数据也写到宿主机上,这样就不会数据丢失。
    
    # Docker提供2种方式将数据从宿主机挂载到容器中:
    • volumes:Docker管理宿主机文件系统的一部分(/var/lib/docker/volumes)。
    • bind mounts:将宿主机上的任意位置的文件或者目录挂载到容器中
    
    volumes示例:
    1、创建数据卷
    # docker volume create nginx-vol
    # docker volume ls
    # docker volume inspect nginx-vol
    2、使用数据卷
    # docker run -d --name=nginx-test  --mount src=nginxvol,dst=/usr/share/nginx/html nginx
    # docker run -d --name=nginx-test  -v nginxvol:/usr/share/nginx/html nginx
    
    bind mounts示例:
    1、挂载宿主机目录到容器
    # docker run -d --name=nginx-test  --mount 
    type=bind,src=/app/wwwroot,dst=/usr/share/nginx/html nginx
    # docker run -d --name=nginx-test  -v 
    /app/wwwroot:/usr/share/nginx/html nginx
    
    

3.4.2 数据持久化操作案例

  • 将本地nginx 容器数据持久化,采用bind mounts 实现

    # 1. 清除原本的nginx 镜像,确保环境无误
    [root@web02_server~]# docker stop 3d175068754b
    3d175068754b
    [root@web02_server~]# docker rm 3d175068754b
    3d175068754b
    [root@web02_server~]# 
    [root@web02_server~]# docker ps 
    CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
    
    # 2. 创建宿主机 挂在目录 /opt/wwwroot
    [root@web02_server~]# mkdir /opt/wwwroot
    
    # 3. 创建容器
    [root@web02_server~]# docker run -itd -p 88:80 --name web -v /opt/wwwroot/:/usr/share/nginx/html nginx
    cf6e36c41091731b13bb47aaf6515a9d864bce1bec0f766b2e16de4eee6b407b
    # 查看最新创建的容器进程
    [root@web02_server~]# docker ps -l
    CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                               NAMES
    cf6e36c41091   nginx     "/docker-entrypoint.…"   26 seconds ago   Up 25 seconds   0.0.0.0:88->80/tcp, :::88->80/tcp   web
    # 过滤出容器IP 地址,方便本机测试
    [root@web02_server~]# docker inspect cf6e36c41091|grep -i ipaddr
                "SecondaryIPAddresses": null,
                "IPAddress": "172.17.0.2",
                        "IPAddress": "172.17.0.2",
    # 3.测试 报错,发现403.这是正常的,原因看第4步
    [root@web02_server~]# curl http://172.17.0.2
    <html>
    <head><title>403 Forbidden</title></head>
    <body>
    <center><h1>403 Forbidden</h1></center>
    <hr><center>nginx/1.21.3</center>
    </body>
    </html>
    [root@web02_server~]#
    
    #4.进入容器,进入nginx 网站目录,发现没有html文件,我们新建一个
    [root@web02_server~]# docker exec -it web /bin/bash
    root@cf6e36c41091:/# cd /usr/share/nginx/html
    root@cf6e36c41091:/# ls
    root@cf6e36c41091:/usr/share/nginx/html# echo "<h1>hello</h1>" > index.html 
    root@cf6e36c41091:/usr/share/nginx/html# exit
    exit
    # 5.再次测试,访问正常
    [root@web02_server~]# curl http://172.17.0.2
    <h1>hello</h1>
    [root@web02_server~]#
    
    
    # 6.此时stop nginx 容器,并且删除,再次访问
    [root@web02_server~]# docker stop web
    web
    [root@web02_server~]# docker rm web
    web
    [root@web02_server~]# curl http://172.17.0.2^C  # 无法访问
    
    # 7. 再次创建容器,测试:发现数据还在
    [root@web02_server~]# docker run -itd -p 88:80 --name web -v /opt/wwwroot/:/usr/share/nginx/html nginx
    d350f8f8fc20b4c71c40d694964792aaecdbf0402c080591d821008a7201b828
    
    [root@web02_server~]# curl http://172.17.0.2
    <h1>hello</h1>
    [root@web02_server~]# 
    
    

3.5 Docker 容器网络

3.5.1 docker 容器网络通信原理及配置

  • Docker使用iptables实现网络通信。
  • veth pair:成对出现的一种虚拟网络设备,数据从一端进,从另 一端出。 用于解决网络命名空间之间隔离。
  • docker0:网桥是一个二层网络设备,通过网桥可以将Linux支持 的不同的端口连接起来,并实现类似交换机那样的多对多的通信。
3.5.1.1 外部访问容器 iptables -t nat -vnL DOCKER
  • 外部访问 容器

    [root@web02_server~]# iptables -t nat -vnL DOCKER
    Chain DOCKER (2 references)
     pkts bytes target     prot opt in     out     source               destination         
        0     0 RETURN     all  --  docker0 *       0.0.0.0/0            0.0.0.0/0           
        0     0 DNAT       tcp  --  !docker0 *       0.0.0.0/0            0.0.0.0/0            tcp dpt:88 to:172.17.0.2:80
    [root@web02_server~]# 
    
  • ❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

3.5.1.2 容器访问外部iptables -t nat -vnL POSTROUTING
  • 容器访问外部

    [root@web02_server~]# iptables -t nat -vnL POSTROUTING
    Chain POSTROUTING (policy ACCEPT 5331 packets, 344K bytes)
     pkts bytes target     prot opt in     out     source               destination         
        0     0 MASQUERADE  all  --  *      !docker0  172.17.0.0/16        0.0.0.0/0           
        0     0 MASQUERADE  tcp  --  *      *       172.17.0.2           172.17.0.2           tcp dpt:80
    
    
  • ❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

3.5.1.3 通讯方式一:端口映射|暴露
  • 端口映射

    # 在run 容器时,-P选项可以将docker容器内的端口随机映射到宿主机的49000-49900范围;
    
    1.创建基于centos镜像的容器并且实现端口映射,将容器的httpd的80端口映射到宿主机的8081端口;
    [root@web02_server~]# docker run -itd --name docker_httpd -e"container=container" --privileged -p 8081:80 centos /usr/sbin/init
    219f3636adb4c17920a5768425d48eb1b50275ba89bf3b4bd1715ff846c14afb
    [root@web02_server~]# docker ps|grep init
    219f3636adb4   centos              "/usr/sbin/init"         19 seconds ago   Up 17 seconds   0.0.0.0:8081->80/tcp, :::8081->80/tcp   docker_httpd
    [root@web02_server~]# 
    
    2.为容器安装httpd服务并且启动服务;
    [root@web02_server~]# docker exec -it docker_httpd /bin/bash
    [root@219f3636adb4 /]# yum -y install net-tools httpd vim
    [root@219f3636adb4 /]# systemctl start httpd
    [root@219f3636adb4 /]# ip a|grep 172|awk '{print $2}'
    172.17.0.4/16
    [root@219f3636adb4 /]# netstat -lnutp|grep 80
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      331/httpd           
    [root@219f3636adb4 /]# exit
    exit
    
    3. docker 宿主机查看,检查
    [root@web02_server~]# docker ps|grep init
    219f3636adb4   centos              "/usr/sbin/init"         7 minutes ago   Up 7 minutes   0.0.0.0:8081->80/tcp, :::8081->80/tcp   docker_httpd
    [root@web02_server~]# 
    
    [root@web02_server~]# netstat -lnutp|grep 8081
    tcp        0      0 0.0.0.0:8081            0.0.0.0:*               LISTEN      36063/docker-Proxy  
    tcp6       0      0 :::8081                 :::*                    LISTEN      36067/docker-proxy  
    [root@web02_server~]# 
    
    4.外部节点测试:
    	浏览器输入:http://10.0.0.8:8081
    	或者 linux 命令行输入:curl http://10.0.0.8:8081
    
    
3.5.1.4 通讯方式二:容器互联
  • 容器互联

    1.创建源容器:
    [root@web02_server~]# docker run -itd --name centos7-1 centos /bin/bash
    925baaf21047ce727c65d527394b5658efc463a200d8a485ad900ac348ac0248
    [root@web02_server~]# docker ps -l
    CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS         PORTS     NAMES
    925baaf21047   centos    "/bin/bash"   10 seconds ago   Up 9 seconds             centos7-1
    
    2.创建接收容器,指定centos容器为源容器:
    
    [root@web02_server~]# docker run -itd --name centos7-2 --link centos7-1:centos7-1 centos /bin/bash
    dc2af328a61c9c2a3ba165da5e9d0d9db6b98add5331bde1a10ea4a46a611cdb
    [root@web02_server~]#
    [root@web02_server~]# docker ps |grep centos7-2
    dc2af328a61c   centos              "/bin/bash"              About a minute ago   Up About a minute                                       centos7-2
    [root@web02_server~]# 
    
    
    3.连接到接收容器进行测试容器之间的连通性:
    
    [root@web02_server~]# docker exec -it centos7-2 /bin/bash
    [root@dc2af328a61c /]# ping centos7-1
    PING centos7-1 (172.17.0.4) 56(84) bytes of data.
    64 bytes from centos7-1 (172.17.0.4): icmp_seq=1 TTL=64 time=0.075 ms
    64 bytes from centos7-1 (172.17.0.4): icmp_seq=2 ttl=64 time=0.045 ms
    ^C
    --- centos7-1 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1002ms
    rtt min/avg/max/mdev = 0.045/0.060/0.075/0.015 ms
    [root@dc2af328a61c /]# 
    
    

3.5.2 Docker 4种 网络模式

  • docker 容器网络

    Docker在安装后自动提供3种网络,可以使用docker network ls命令查看

    [root@web02_server~]# docker network ls
    NETWORK ID     NAME      DRIVER    SCOPE
    f2671c948ee2   bridge    bridge    local
    56ca8e9e001c   host      host      local
    fe6f1a90a81f   none      null      local
    [root@web02_server~]# 
    
    

    Docker使用Linux桥接,在宿主机虚拟一个Docker容器网桥(docker0),Docker启动一个容器时会根据Docker网桥的网段分配给容器一个IP地址,称为Container-IP,同时Docker网桥是每个容器的默认网关。因为在同一宿主机内的容器都接入同一个网桥,这样容器之间就能够通过容器的Container-IP直接通信

3.5.2.1 bridge 模式
  • bridge 桥接模式

    1.Bridge模式:默认的网络模式,在启动docker进程后,宿主机会创建名为docker0的虚拟网桥,ip地址为172.17.0.1,所有容器的默认网络会自动获取一个172.17.0.0网络的地址,实际上宿主机在此模式默认自动做了一条NAT转换规则并且开启路由转发;
    
  • ❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

3.5.2.2 host 模式
  • host 模式—共享宿主机的IP和端口,但不能冲突

    2.Host模式:如果启动容器的时候使用host模式,那么这个容器将不会获得一个独立的Network Namespace,而是和宿主机共用一个Network Namespace。容器将不会虚拟出自己的网卡,配置自己的IP等,而是使用宿主机的IP和端口。但是,容器的其他方面,如文件系统、进程列表等还是和宿主机隔离的;
    
  • ❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

3.5.2.3 none 模式
  • none 模式

    3.None模式: Docker容器拥有自己的Network Namespace,但是,并不为Docker容器进行任何网络配置。也就是说,这个Docker容器没有网卡、IP、路由等信息。需要我们自己为Docker容器添加网卡、配置IP等;
    
  • ❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

3.5.2.4 Container模式
  • Container模式

    指定新创建的容器和已经存在的一个容器共享一个 Network Namespace,而不是和宿主机共享。新创建的容器不会创建自己的网卡,配置自己的 IP,而是和一个指定的容器共享 IP、端口范围等。同样,两个容器除了网络方面,其他的如文件系统、进程列表等还是隔离的。两个容器的进程可以通过 lo 网卡设备通信;
    
  • ❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

3.5.3 配置网络模式

3.5.3.1 配置默认桥接bridge模式:默认桥接到 docker 0
  • 默认bridge 模式,可以不指定

    # 默认桥接到docker 0 通信
    [root@web02_server~]# ip address show docker0
    4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
        link/ether 02:42:8b:50:e3:1e brd ff:ff:ff:ff:ff:ff
        inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
           valid_lft forever preferred_lft forever
        inet6 fe80::42:8bff:fe50:e31e/64 scope link 
           valid_lft forever preferred_lft forever
    [root@web02_server~]# 
    
    [root@web02_server~]# docker run -itd --net=bridge --name centos7-3 centos
    d4150878301744871040d8e2043a98ab831fc2473c09a033485da3a3ae1c1764
    [root@web02_server~]# docker ps -l
    CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
    d41508783017   centos    "/bin/bash"   9 seconds ago   Up 6 seconds             centos7-3
    
    
3.5.3.2 配置host模式:使用 --net=host指定
  • 配置host 模式

    [root@web02_server~]# docker run -itd --net=host --name centos7-3 centos
    bde69a0881b49411be31f9aa25d9729953a8762217c179e3f6c78a38225e0661
    [root@web02_server~]# docker ps -l
    CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
    bde69a0881b4   centos    "/bin/bash"   9 seconds ago   Up 8 seconds             centos7-3
    [root@web02_server~]
    
    # 进入创建的 centos 7-3容器
    [root@web02_server~]# docker exec -it centos7-3 /bin/bash
    [root@web02_server /]# 
    
    # 创建一个httpd 服务
    [root@web02_server /]#  yum -y install iproute net-tools httpd
    
    # 查看centos 7-3容器的路由
    [root@web02_server /]# ip route
    default via 10.0.0.2 dev eth0 proto static metric 102 
    10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.8 metric 102 
    172.16.1.0/24 dev eth1 proto kernel scope link src 172.16.1.8 metric 101 
    172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 
    [root@web02_server /]# 
    
    
    [root@web02_server /]# /usr/sbin/httpd
    httpd (pid 105) already running
    [root@web02_server /]# 
    
    [root@web02_server~]# netstat -utpln |grep 80
    tcp6       0      0 :::80                   :::*                    LISTEN      31347/httpd         
    tcp6       0      0 :::8081                 :::*                    LISTEN      7387/java           
    [root@web02_server~]#
    
3.5.3.3 配置none模式
  • 配置 none模式

    [root@web02_server~]# docker run -itd --net=none --name centos7 centos
    28da7577477176c4d9b95df5f5e6553714d6a87539323c4b68fa3895bb50c064
    [root@web02_server~]# docker ps -l
    CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
    28da75774771   centos    "/bin/bash"   7 seconds ago   Up 6 seconds             centos7
    [root@web02_server~]# docker inspect centos7 |grep -i ipaddr
                "SecondaryIPAddresses": null,
                "IPAddress": "",
                        "IPAddress": "",
    [root@web02_server~]# 
    
    
3.5.3.4 配置container模式:使用 --net=container:container_id/container_name
  • 配置container 模式

    # 先启动一个容器
    [root@web02_server~]# docker run -itd --name tomcat consol/tomcat-7.0
    6b2544e960634da6389d10a703d5591fe2bcef57d128328929eb0720de3c1a49
    [root@web02_server~]# docker ps -l
    CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS         PORTS                NAMES
    6b2544e96063   consol/tomcat-7.0   "/bin/sh -c /opt/tom…"   10 seconds ago   Up 7 seconds   8080/tcp, 8778/tcp   tomcat
    [root@web02_server~]# docker inspect tomcat |grep -i ipaddr
                "SecondaryIPAddresses": null,
                "IPAddress": "172.17.0.3",
                        "IPAddress": "172.17.0.3",
    [root@web02_server~]# 
    
    # 新创建一个容器
    [root@web02_server~]# docker run -itd --name=container_test --net=container:6b2544e96063 redis
    291a19591a0fccc79ce8eddf9399a5da831cfb82261be60e91e89858f1af9410
    [root@web02_server~]# 
    
    # 查看最新启动的 容器
    [root@web02_server~]# docker ps -l
    CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS     NAMES
    291a19591a0f   redis     "docker-entrypoint.s…"   38 seconds ago   Up 37 seconds             container_test
    [root@web02_server~]#
    
3.5.3.5 自定义配置桥接模式、修改网段— 实战案例
  • 自定义桥接模式

    # 注意说明:
    	本机 宿主机 eth0 ip 地址:10.0.0.8
    					网关:10.0.0.2
    					DNS:223.5.5.5
    
    # CentOS7需安装网络和桥接工具
    [root@web02_server~]# yum -y install bridge-utils net-tools
    [root@web02_server~]#cd /etc/sysconfig/network-scripts/
    
    # 复制一份网卡,重命名为 ifcfg-br0
    [root@web02_server/etc/sysconfig/network-scripts]# cp ifcfg-eth0 ./ifcfg-br0cp ifcfg-eth0 ./ifcfg-br0
    
    # 备份一个eth0 网卡,方便恢复
    [root@web02_server/etc/sysconfig/network-scripts]# cp ifcfg-eth0 ifcfg-eth0.bak
    
    # 编写 ifcfg-br0 桥接网卡
    [root@web02_server/etc/sysconfig/network-scripts]# vim ifcfg-br0
    TYPE=Bridge
    BOOTPROTO=static
    DEFROUTE=yes
    PEERDNS=yes
    PEERROUTES=yes
    ipv4_FAILURE_FATAL=yes
    NAME=br0
    DEVICE=br0
    ONBOOT=yes
    NM_CONTROLLED=no
    IPADDR=10.0.0.8
    PREFIX=24
    GATEWAY=10.0.0.2
    DNS1=10.0.0.2
    
    # 修改 ifcfg-eth0 网卡
    [root@web02_server/etc/sysconfig/network-scripts]# vim ifcfg-eth0
    TYPE=Ethernet
    BOOTPROTO=none
    DEFROUTE=yes
    PEERDNS=yes
    PEERROUTES=yes
    Ipv4_FAILURE_FATAL=yes
    NAME=eth0
    DEVICE=eth0
    ONBOOT=yes
    NM_CONTROLLED=no
    BRIDGE=br0
    
    # 重启网卡
    [root@web02_server/etc/sysconfig/network-scripts]# cd --
    /root
    [root@web02_server~]# systemctl restart network
    
    
    # 此时,eth0 网卡桥接到了 br0 网卡,eth0 没有了IP地址
    [root@web02_server~]# ifconfig 
    br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 10.0.0.8  netmask 255.255.255.0  broadcast 10.0.0.255
            inet6 fe80::20c:29ff:fe32:8b73  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:32:8b:73  txqueuelen 1000  (Ethernet)
            RX packets 67295  bytes 230884723 (220.1 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 56721  bytes 6322538 (6.0 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet6 fe80::20c:29ff:fe32:8b73  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:32:8b:73  txqueuelen 1000  (Ethernet)
            RX packets 10371209  bytes 13133670349 (12.2 GiB)
            RX errors 0  dropped 6  overruns 0  frame 0
            TX packets 2643465  bytes 285133908 (271.9 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
            
      
    
    [root@web02_server~]# brctl show
    bridge name	bridge id		STP enabled	interfaces
    br0		8000.000c29328b73	no		eth0
    docker0		8000.02428b50e31e	no		
    [root@web02_server~]# 
    
    # 安装pipework开源网络配置工具
    [root@web02_server~]# git clone https://github wangt.cc /jpetazzo/pipework
    Cloning into 'pipework'...
    remote: Enumerating objects: 518, done.
    remote: Counting objects: 100% (8/8), done.
    remote: ComPressing objects: 100% (6/6), done.
    remote: total 518 (delta 2), reused 5 (delta 2), pack-reused 510
    Receiving objects: 100% (518/518), 182.72 KiB | 0 bytes/s, done.
    Resolving deltas: 100% (272/272), done.
    
    [root@web02_server~]# cp pipework/pipework /usr/local/bin/
    
    # 指定一个网段,容器划分的网段,修改网段 /etc/sysconfig/docker
    [root@web02_server~]# vim /etc/sysconfig/docker
    OPTIONS=' --selinux-enabled -b=br0 --fixed-cidr=10.0.0.0/24 --log-driv
    er=journald --signature-verification=false'
    [root@web02_server~]# systemctl restart docker
    
    -b=br0 ---docker 引擎使用br0 桥接
    --fixed-cidr=10.0.0.0/24  指定某一个网段
    
    
    # 创建容器时指定--net=none不采用默认桥接方式自动产生ip,使用pipeword工具设置ip地址
    [root@web02_server~]# docker run -itd --net=none --name centos7-101 centos /bin/bash
    165af60e0748a97c17371967e4193402f04df2f074877fc39ea1d4ca1f600b46
    
    
    # 使用pipework命令为容器配置ip地址。指定桥接网卡、容器ID、ip地址、子网掩码、网关,pipeword工具功能强大,划分vlan等,划分 10.0.0.100/24 这个ip地址
    
    [root@web02_server~]# pipework br0 centos7-101 10.0.0.100/24@10.0.0.2
    
    [root@docker ~]# docker exec -it centos7-101 /bin/bash
    [root@ade2fb172937 /]# yum -y install net-tools
    
    [root@2c1bb62ea9a9 /]# ip a
    1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
        link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
        inet 127.0.0.1/8 scope host lo
           valid_lft forever preferred_lft forever
    68: eth1@if69: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
        link/ether b2:00:a3:2e:65:4c brd ff:ff:ff:ff:ff:ff link-netnsid 0
        inet 10.0.0.100/24 brd 10.0.0.255 scope global eth1
           valid_lft forever preferred_lft forever
    [root@2c1bb62ea9a9 /]# 
    
    [root@2c1bb62ea9a9 /]# ping -c 2 www.baidu wangt.cc 
    PING www.a.shifen wangt.cc  (36.152.44.96) 56(84) bytes of data.
    64 bytes from 36.152.44.96 (36.152.44.96): icmp_seq=1 ttl=128 time=16.6 ms
    64 bytes from 36.152.44.96 (36.152.44.96): icmp_seq=2 ttl=128 time=16.1 ms
    
    --- www.a.shifen wangt.cc  ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 1003ms
    rtt min/avg/max/mdev = 16.094/16.343/16.592/0.249 ms
    [root@2c1bb62ea9a9 /]# 
    
    

四、Dockerfile构建镜像

4.1 Dockerfile 概述

  • 概述

    **概述:**除了手动生成docker镜像之外,还可以通过dockerfile自动生成镜像,dockerfile是由一组指定组成的文件,其中每条指令对应linux中的一条命令,docker程序将读取dockerfile中的指令生成指定镜像;

    Docker通过Dockerfile自动构建镜像,Dockerfile是一个包含用于组建镜像的文本文件,由一条一条 的指令组成。逐行执行

    结构:

    1.基础镜像信息;

    2.维护者信息;

    3.镜像操作指令;

    4.容器启动时执行指令;

    **规则:**dockerfile每行支持一条指令,每条指令可携带多个参数,支持使用以#号开头的注释;

4.2 Docker 常用指令

  • 常用指令

    命令描述
    FROM构建新镜像是基于哪个镜像
    LABEL标签
    RUN构建镜像时运行的Shell命令
    COPY拷贝文件或目录到镜像中
    ADD解压压缩包并拷贝
    ENV设置环境变量
    USER为RUN、CMD和ENTRYPOINT执行命令指定运行用户
    EXPOSE声明容器运行的服务端口
    WORKDIR为RUN、CMD、ENTRYPOINT、COPY和ADD设置工作目录
    CMD运行容器时默认执行,如果有多个CMD指令,最后一个生效

4.3 构建镜像命令

  • 构建镜像常用命令

    Usage: docker build [OPTIONS] PATH | URL | - [flags]
    Options:
    -t, --tag list # 镜像名称
    -f, --file string # 指定Dockerfile文件位置
    # docker build -t shykes/myapp .
    # docker build -t shykes/myapp -f /path/Dockerfile /path
    # docker build -t shykes/myapp http://www.example wangt.cc /Dockerfile
    
  • 简单的构建镜像案例

    [root@web02_server~/dockerfile]# cat dockerfile 
    FROM centos:7
    RUN yum install EPEL-release -y && 
        yum install nginx -y
    CMD ["nginx","-g","daemon off;"]
    [root@web02_server~/dockerfile]# docker build -t nginx:v1 .
    Sending build context to Docker daemon  2.048kB
    Step 1/3 : FROM centos:7
    7: Pulling from library/centos
    2d473b07cdd5: Pull complete 
    Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
    Status: Downloaded newer image for centos:7
     ---> eeb6ee3f44bd
    Step 2/3 : RUN yum install epel-release -y &amp;&     yum install nginx -y
     ---> Running in 1ffcbdc36107
    …………(省略)                                  
    Dependency Updated:
      openssl-libs.x86_64 1:1.0.2k-21.el7_9                                         
    
    Complete!
    Removing intermediate container 1ffcbdc36107
     ---> 32e58021c0b5
    Step 3/3 : CMD ["nginx","-g","daemon off;"]
     ---> Running in 1de98bc362df
    Removing intermediate container 1de98bc362df
     ---> 5662cbdf65e0
    Successfully built 5662cbdf65e0
    Successfully tagged nginx:v1
    
    
    # 查看本地构建的镜像(tag 标签为1是自己构建的)
    [root@web02_server~/dockerfile]# docker image ls
    REPOSITORY                TAG       IMAGE ID       CREATED         SIZE
    nginx                     v1        5662cbdf65e0   3 minutes ago   415MB
    
    
    #启用一个本地构建的nginx v1版本镜像(这里借用数据持久化环境,创建了/opt/wwwroot/)
    [root@web02_server~]# docker run -itd --name web01 -p 90:80 -v /opt/wwwroot/:/usr/share/nginx/html nginx:v1
    24528b991b7c26317ae9070f4a6395b68b3c2bb329f9598ac4307bbd773a6192
    
    
    [root@web02_server~]# docker ps -l
    CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                               NAMES
    24528b991b7c   nginx:v1   "nginx -g 'daemon of…"   21 seconds ago   Up 20 seconds   0.0.0.0:90->80/tcp, :::90->80/tcp   web01
    
    [root@web02_server~]# docker inspect web01|grep -i ipaddr
                "SecondaryIPAddresses": null,
                "IPAddress": "172.17.0.2",
                        "IPAddress": "172.17.0.2",
    [root@web02_server~]# 
    
    [root@web02_server~]# curl http://172.17.0.2:80
    <h1>hello</h1>
    [root@web02_server~]# 
    

4.4 构建nginx 镜像— 编译安装nginx

  • 构建nginx 镜像

    # 准备一个压缩包,里面放nginx
    [root@web02_server~/dockerfile/nginx]# ls
    Dockerfile  nginx-1.15.5.tar.gz  nginx.conf  php.conf
    
    # 构建nginx dockerfile
    [root@web02_server~/dockerfile/nginx]# cat Dockerfile
    FROM centos:7
    LABEL maintainer www.ctnrs wangt.cc 
    RUN yum install -y gcc gcc-c++ make 
        openssl-devel pcre-devel gd-devel 
        iproute net-tools telnet wget curl && 
        yum clean all && 
        rm -rf /var/cache/yum/*
    
    ADD nginx-1.15.5.tar.gz /
    RUN cd nginx-1.15.5 && 
        ./configure --prefix=/usr/local/nginx 
        --with-http_ssl_module 
        --with-http_stub_status_module && 
        make -j 4 && make install && 
        mkdir /usr/local/nginx/conf/vhost && 
        cd / && rm -rf nginx* && 
        ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    
    ENV PATH $PATH:/usr/local/nginx/sbin
    COPY nginx.conf /usr/local/nginx/conf/nginx.conf
    WORKDIR /usr/local/nginx
    EXPOSE 80
    CMD ["nginx", "-g", "daemon off;"]
    
    
    # 构建一个 nginx v2版本的----编译安装
    [root@web02_server~/dockerfile/nginx]# docker build -t nginx:v2 .
    Sending build context to Docker daemon  1.031MB
    Step 1/10 : FROM centos:7
    …… (省略)
    ……
     ---> 6cc9110c56c4
    Step 10/10 : CMD ["nginx", "-g", "daemon off;"]
     ---> Running in a3ab71b03867
    Removing intermediate container a3ab71b03867
     ---> e9548d7e4691
    Successfully built e9548d7e4691
    Successfully tagged nginx:v2
    
    # 启动一个 nginx 经常,名字 web02 
    [root@web02_server~]# docker run -itd --name web02 -p 99:80 -v /opt/wwwroot/:/usr/share/nginx/html nginx:v2
    456c3451ade36178a5c54e1ff4a318305d4cce2c8a7dec2ffa29086de4187d5e
    [root@web02_server~]# docker ps -l
    CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                               NAMES
    456c3451ade3   nginx:v2   "nginx -g 'daemon of…"   12 seconds ago   Up 11 seconds   0.0.0.0:99->80/tcp, :::99->80/tcp   web02
    [root@web02_server~]# docker inspect web02 |grep -i ipaddr
                "SecondaryIPAddresses": null,
                "IPAddress": "172.17.0.3",
                        "IPAddress": "172.17.0.3",
    
    # 测试访问,发现访问的是nginx 默认页面,为啥呢,因为我们编译安装的网站目录不是 /usr/share/nginx/html,而是自定义的 安装目录
    [root@web02_server~]# curl http://172.17.0.3   
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    <style>
        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 wangt.cc /">nginx wangt.cc </a>.</p>
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>
    [root@web02_server~]# 
    
    
    #在运行一个nginx 容器 web03 。
    [root@web02_server~]# docker run -itd --name web03 -p 100:80 -v /opt/wwwroot/:/usr/local/nginx/html nginx:v2
    d0955979f38020832f7ddd55588b2e1402b78ede45e7a6602cb18c8148c26995
    [root@web02_server~]# 
    
    # 这里访问的才是我们之前 数据持久化写的 网页
    [root@web02_server~]# curl http://172.17.0.4
    <h1>hello</h1>
    [root@web02_server~]# 
    

4.5 Harbor镜像仓库搭建与使用

4.5.1 什么是Harbor

  • Harbor是由VMWare公司开源的容器镜像仓库。
  • 事实上,Harbor是在Docker Registry上进行了相应的企业级扩展,从而获得了更加广泛的应用,这些新的企业级特性包括:管理用户界面基于角色的访问控制AD/LDAP集成以及审计日志等,足以满足基本企业需求。
  • Harbor 官网:https://goharbor.io/
  • Harbor 官方文档:https://github wangt.cc /goharbor/harbor

4.5.2 部署Harbor 环境说明

  • 宿主机环境

    主机名IP地址系统内核
    HB-XYJF-nginx-node110.0.0.91CentOS Linux release 7.6.1810
    docker0110.0.0.8CentOS Linux release 7.6.1810
  • ❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

  • 实验部署先决条件

    # 服务器硬件配置:
    • 最低要求:CPU2核/内存4G/硬盘40GB
    • 推荐:CPU4核/内存8G/硬盘160GB
    
    # 软件:
    • Docker CE 17.06版本+
    • Docker Compose 1.18版本+
    
    # Harbor安装有2种方式:
    
    • 在线安装:从Docker Hub下载Harbor相关镜像,因此安装软件包非常小
    • 离线安装:安装包包含部署的相关镜像,因此安装包比较大
    
    本次测试,采用离线部署方式
    

4.5.3 部署Harbor 私有仓库步骤

4.5.3.1 下载安装 Docker 和Docker Compose
  • 下载安装Docker

    # 官方安装教程
    [root@docker-server~]#  yum install -y yum-utils
    [root@docker-server~]# yum-config-manager  --add-repo  https://download.docker wangt.cc /linux/centos/docker-ce.repo
    
    # 安装docker-ce
    yum install -y docker-ce
    
    # 配置加速器
     参照前面加速器配置
    
  • 下载安装Docker Compose

    # 下载地址:https://github wangt.cc /docker/compose/releases
    
    # 上传 compose 和 harbor
    [root@docker01~]# ls
    anaconda-ks.cfg              harbor-offline-installer-v2.0.0.tgz
    docker-compose-Linux-x86_64  nohup.out
    pipework                    szz-job-v1.jar
    
    # 下载完成后,通过工具上传到服务器,这步忽略。#对docker-compose文件增加执行权限,移动到sbin目录下面
    
    [root@docker01~]# chmod o+x docker-compose-Linux-x86_64 
    [root@docker01~]# mv docker-compose-Linux-x86_64 /usr/bin/docker-compose
    
    
    #查看docker-compose版本,是否生效。
    [root@docker01~]# docker-compose 
    define and run multi-container applications with Docker.
    
    Usage:
      docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]
      docker-compose -h|--help
    
    Options:
      -f, --file FILE             Specify an alternate compose file
                                  (default: docker-compose.yml)
      -p, --project-name NAME     Specify an alternate project name
                                  (default: directory name)
      -c, --context NAME          Specify a context name
      --verbose                   Show more output
      --log-level LEVEL           Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
      --no-ansi                   Do not print ANSI control characters
    …………(忽略)
    
    # 执行 docker-compose 可以查看到使用参数即可,表示docker-compose安装成功
    
4.5.3.2 上传、解压 harbor离线安装包
  • 上传、解压离线包

    # 上传步骤忽略
    # 解压 
    [root@docker01~]# tar xf harbor-offline-installer-v2.0.0.tgz
    
    # 进入harbor/ 目录
    [root@docker01~]# cd harbor/
    [root@docker01~/harbor]# ls
    common.sh  harbor.v2.0.0.tar.gz  harbor.yml.tmpl  install.sh  LICENSE  prepare
    
    # 拷贝一个配置文件,软件自带有一个tmpl的模板文件,可以通过复制此文件进行编辑。
    [root@docker01~/harbor]# cp harbor.yml.tmpl harbor.yml
    [root@docker01~/harbor]# ls
    common.sh             harbor.yml       install.sh  prepare
    harbor.v2.0.0.tar.gz  harbor.yml.tmpl  LICENSE
    [root@docker01~/harbor]# 
    
    
4.5.3.3 修改配置文件
  • 修改配置文件

    # 修改 第 5行
     5 hostname: 10.0.0.8  # 默认写的域名访问,这里我直接写服务器IP,访问的域名
    
    # 关闭 https 证书,我这里13-18行,全部注释掉,不使用https证书
     13 #https:
     14 #  # https port for harbor, default is 443
     15 #  port: 443
     16 #  # The path of cert and key files for nginx
     17 #  certificate: /your/certificate/path
     18 #  private_key: /your/private/key/path
    
    
    # 设置harbor 管理员密码 ,第34行。
    34 harbor_admin_password: Harbor12345
    # 第47行,harbor的数据存储位置
     data_volume: /data
    
4.5.3.4 安装Harbor
  • 安装harbor

    # 拉取镜像,做初始化配置文件,Harbor服务就会根据当前目录下的harbor.yml文件开始下载依赖的镜像,检测并按照顺序依次启动
    [root@docker01~/harbor]# ./prepare 
    prepare base dir is set to /root/harbor
    Unable to find image 'goharbor/prepare:v2.0.0' locally
    
    # 在的当前目录执行./install.sh,进行harbor安装(导入镜像)
    
    [root@docker01~/harbor]# ./install.sh 
    [Step 0]: checking if docker is installed ...
    Note: docker version: 20.10.8
    [Step 1]: checking docker-compose is installed ...
    Note: docker-compose version: 1.26.0
    
    [Step 2]: loading Harbor images ...
    Loaded image: goharbor/notary-signer-photon:v2.0.0
    Loaded image: goharbor/clair-adapter-photon:v2.0.0
    
    …………(省略)
    Creating redis         ... done
    Creating harbor-db     ... done
    Creating harbor-core   ... done
    Creating harbor-jobservice ... done
    Creating nginx             ... done
    ✔ ----Harbor has been installed and started successfully.----
    
    安装成功,此时浏览器输入 10.0.0.8 访问,输入用户名、密码
    	admin
    	Harbor12345 
    
  • 测试:浏览器输入http://10.0.0.8

  • ❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

4.5.4 打标签、上传/拉取镜像

  • 打标签、上传/拉取镜像

    # 因为采用的http ,所以连接推送会拒绝,因此我先做个信任
    # 配置http镜像仓库可信任
    [root@docker01/etc/docker]# vim daemon.json 
    {
    "registry-mirrors":["https://b9pmyelo.mirror.aliyuncs wangt.cc "],
    "insecure-registries":["10.0.0.8"]
    }
    
    # 重启docker 
    [root@docker01~/harbor]# systemctl start docker
    
    [root@docker01~/harbor]# cd --
    [root@docker01~]# cd harbor/
    [root@docker01~/harbor]# docker-compose ps
          Name                    Command                State                 Ports           
    -------------------------------------------------------------------------------------------
    harbor-core         /harbor/entrypoint.sh         Up (healthy)                             
    harbor-db           /docker-entrypoint.sh         Up (healthy)   5432/tcp                  
    harbor-jobservice   /harbor/entrypoint.sh         Exit 137                                 
    harbor-log          /bin/sh -c /usr/local/bin/    Up (healthy)   127.0.0.1:1514->10514/tcp 
                        ...                                                                    
    harbor-portal       nginx -g daemon off;          Up (healthy)   8080/tcp                  
    nginx               nginx -g daemon off;          Up (healthy)   0.0.0.0:80->8080/tcp,:::80
                                                                     ->8080/tcp                
    redis               redis-server                  Up (healthy)   6379/tcp                  
                        /etc/redis.conf                                                        
    registry            /home/harbor/entrypoint.sh    Up (healthy)   5000/tcp                  
    registryctl         /home/harbor/start.sh         Up (healthy)  
    
    # 使用docker-compose 批量管理容器 
    [root@docker01~/harbor]# docker-compose up -d
    harbor-log is up-to-date
    registryctl is up-to-date
    registry is up-to-date
    harbor-db is up-to-date
    redis is up-to-date
    harbor-portal is up-to-date
    harbor-core is up-to-date
    nginx is up-to-date
    harbor-jobservice is up-to-date
    [root@docker01~/harbor]# 
    
    # 首次推送,它会提示没有推送权限的(这里我们可以登录,在推送,也可以修改配置文件)
    [root@docker01~/harbor]# docker push 10.0.0.8/library/nginx:v2
    The push refers to repository [10.0.0.8/library/nginx]
    0372836c6231: Preparing 
    09b1a253ff74: Preparing 
    b7748bf7f3c7: Preparing 
    5f74203d35b5: Preparing 
    174f56854903: Preparing 
    unauthorized: unauthorized to access repository: library/nginx, action: push: unauthorized to access repository: library/nginx, action: push
    [root@docker01~/harbor]# 
    
    # 登录10.0.0.8 输入用户 admin 密码Harbor12345
    [root@docker01~/harbor]# docker login 10.0.0.8 
    Username: admin
    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 wangt.cc /engine/reference/commandline/login/#credentials-store
    
    Login Succeeded
    # 登录后,再次推送。同送成功
    [root@docker01~/harbor]# docker push 10.0.0.8/library/nginx:v2
    The push refers to repository [10.0.0.8/library/nginx]
    0372836c6231: Pushed 
    09b1a253ff74: Pushed 
    b7748bf7f3c7: Pushed 
    5f74203d35b5: Pushed 
    174f56854903: Pushed 
    v2: digest: sha256:3a10d0eebb837296f16a90dce9fa3f18d453d45c5dca3e132dcd7808eb228469 Size: 1370
    
    # 然后可以去客其它机器下载,这里我以 10.0.0.91这台机器去下载
     1.首先还是要先做信任
     2.重启docker 服务
     3.拉去
    
    [root@HB-XYJF-nginx-node1~]# vim /etc/docker/daemon.json 
    {
    "registry-mirrors":["https://b9pmyelo.mirror.aliyuncs wangt.cc "],
    "insecure-registries":["10.0.0.8"]
    
    [root@HB-XYJF-nginx-node1~]# systemctl restart docker
    [root@HB-XYJF-nginx-node1~]# docker pull 10.0.0.8/library/nginx:v2
    v2: Pulling from library/nginx
    2d473b07cdd5: Pull complete 
    81bf2c135455: Pull complete 
    ef0246aa7a1d: Pull complete 
    3bf8f2b88785: Pull complete 
    24dfce654dbf: Pull complete 
    Digest: sha256:3a10d0eebb837296f16a90dce9fa3f18d453d45c5dca3e132dcd7808eb228469
    Status: Downloaded newer image for 10.0.0.8/library/nginx:v2
    10.0.0.8/library/nginx:v2
    
    [root@HB-XYJF-nginx-node1~]# docker image ls
    REPOSITORY               TAG       IMAGE ID       CREATED        SIZE
    10.0.0.8/library/nginx   v2        e9548d7e4691   23 hours ago   377MB
    [root@HB-XYJF-nginx-node1~]# 
    
    # 启动下载后的一个容器
    [root@HB-XYJF-nginx-node1~]# docker run -itd -p 88:80 --name web01 10.0.0.8/library/nginx:v2
    fe8e3a5568849dd943970049d924fcacc766b1d00243dc7a99792cefd69a3084
    
    
    
  • ❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

  • ❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

五、总结

  • 博文回顾

    本篇文章详细的介绍了Docker 概念、基础语法、容器管理、镜像制作、搭建私有仓库等,从0到1,非常详细,当你遇到了这篇文章,欢迎大家一起学习,交流技术。如果你认真看完我的文章,相信你可以掌握docker 基础使用,面对企业项目可以快速上手。

    下一篇文章,我会分享 docker 更偏向于实战的技术,介绍一篇案列,欢迎订阅。

    如果喜欢博主的博文,欢迎访问:首页链接,进行专栏订阅!

    linux 云计算专栏

    100个 常用 linux 命令总结

    linux shell 脚本入门到实战

    ansible 自动化运维详解 linux 系统基础优化 …… 下面是博主写的博文笔记,如果喜欢,可以私信,赠送给老铁/粉丝

  • ❤�%的读者❤️都会收藏的Docker入门到实战的技术文章

纵有狂风拔地起,我亦乘风破万里

脚本宝典总结

以上是脚本宝典为你收集整理的❤�%的读者❤️都会收藏的Docker入门到实战的技术文章全部内容,希望文章能够帮你解决❤�%的读者❤️都会收藏的Docker入门到实战的技术文章所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。