zoukankan      html  css  js  c++  java
  • docker_info_04_image 镜像管理

    docker_info_04_image镜像管理

    • Docker 镜像构建分为两种,一种是手动构建,另一种是 Dockerfile(自动构建)

    4.1.手动构建自定义镜像

    • 基于基础系统镜像进行构建,制作 nginx 镜像

    4.1.1.确认服务安装配置命令

    # yum 安装:
    --------------------------
    yum install wget -y
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    yum install nginx -y
    vi /etc/nginx/nginx.conf
    --------------------------
    
    # 源码安装:
    --------------------------
    yum install vim lrzsz net-tools wget gcc gcc-c++ make openssl-devel pcre -y
    cat /etc/redhat-release
    yum install -y
    mkdir -p /opt/tools
    cd /opt/tools/
    useradd -s /sbin/nologin -M www
    cd nginx-1.8.0
    ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-pcre
    make
    make install
    vim /etc/rc.local
    vim /usr/local/nginx/conf/nginx.conf
    /usr/local/nginx/sbin/nginx
    /usr/local/nginx/sbin/nginx -t
    /usr/local/nginx/sbin/nginx
    --------------------------
    

    4.1.2.创建容器制作模板镜像

    # 使用官方 centos 镜像安装 nginx 软件
    docker run -it --name centos-01-img -h centos-01 centos
    
    # 也可以用官方 nginx 镜像修改配置打包自定义镜像
    docker run -d -P --name nginx-01-img -h nginx-01 nginx
    
    docker run -d -p 92:80 --name zsnginx-01 -h zsnginx-01 zhaoshuai/mynginx:v1     # 后台启动docker,要写镜像全称,带上标签
    
    

    4.1.3.提交自建的 docker 镜像到镜像库

    docker commit -m "<描述信息>" <目标容器ID> <自定义的容器名称(推荐:仓库名称/镜像名称:tag标记)>
    ----------------------------
    docker commit -m "nginx-01-img-20180822" f9308d615b29 zhaoshuai/mynginx:v1
    ----------------------------
    
    
    # 删除自建docker镜像的方法
    docker rmi zhaoshuai/mynginx:v2
    

    4.1.4.使用 Dockerfile 构建镜像

    命令格式及示例:(需要在 Dockerfile 文件目录执行)
    docker build -t <仓库/镜像:tag版本标记> <构建目录>
    docker build -t mynginx:v2 .
    

    4.2.手动构建自定义镜像实例演示

    • 1.创建容器准备作为镜像
    # 使用 centos 需要手动安装 nginx 软件
    [root@zuiyoujie ~]# docker run -it --name centos-01-img -h centos-01 centos
    [root@centos-01 /]# yum install wget -y         # 安装下载命令
    [root@centos-01 /]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    [root@centos-01 /]# yum install nginx -y        # yum安装nginx
    [root@centos-01 /]# vi /etc/nginx/nginx.conf    # 修改nginx配置文件,使之在前台运行
    --------------------
    daemon off;                                     # 增加 daemon 参数,关闭后台运行,修改完成,退出查看 docker 进程
    --------------------
    [root@centos-01 /]# exit
    
    # 用官方 nginx 镜像修改配置打包自定义镜像
    [root@zuiyoujie tools]# docker run -d -P --name nginx-01-img -h nginx-01 nginx
    [root@zuiyoujie tools]# docker ps -a
    CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                   NAMES
    f9308d615b29        nginx               "nginx -g 'daemon of…"   2 minutes ago       Up 2 minutes        0.0.0.0:32770->80/tcp   nginx-01-img
    ----------------------------------
    
    • 2.推送镜像到自定义仓库
    [root@zuiyoujie tools]# docker commit -m "nginx-01-img-20180822" f9308d615b29 zhaoshuai/mynginx:v1
    sha256:f8c6d06b3f85cf2639afcfc4493cee1772133c9bbea6a561f8cd528debdac0ac
    [root@zuiyoujie tools]# docker images          # 查看docker镜像库
    REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
    zhaoshuai/mynginx   v1                  f8c6d06b3f85        3 seconds ago       109MB
    centos              latest              5182e96772bf        2 weeks ago         200MB
    nginx               latest              c82521676580        4 weeks ago         109MB
    hello-world         latest              2cb0d9787c4d        6 weeks ago         1.85kB
    ----------------------------------
    

    4.3.使用 Dockerfile 自动化构建镜像

    • 以创建一个 nginx 的 docker 容器镜像为例进行演示

    4.3.1.创建 Dockerfile 目录,上传 nginx 相关软件

    mkdir -p /data/docker/data/nginx01
    cd /data/docker/data/nginx01
    rz -y
    # 编写一个首页index文件
    echo www.zuiyoujie.com >index.html
    ----------------------
    [root@zuiyoujie ~]# cd  /data/docker/data/nginx01/
    [root@zuiyoujie nginx01]# echo www.zuiyoujie.com >index.html
    [root@zuiyoujie nginx01]# ll
    total 824
    -rw-r--r-- 1 root root    852 Aug 23 10:32 Dockerfile
    -rw-r--r-- 1 root root     18 Aug 23 14:44 index.html
    -rw-r--r-- 1 root root 832104 Jul 27 18:37 nginx-1.8.0.tar.gz
    ----------------------
    

    4.3.2.创建 Dockerfile 配置文件

    • 编写 Dockerfile 文件
    vim Dockerfile
    -------------------------
    # made by zhaoshuai for Dockerfile on 20180823
    # Version: 1.0
    
    # Specify the Base images
    FROM centos
    
    # Specify the Maintenance person
    MAINTAINER zhaoshuai zhaoshuai@zuiyoujie.com
    
    # ADD:Upload userful files.
    RUN mkdir -p /opt/tools
    
    # ADD can unpack the Compression package.
    ADD nginx-1.8.0.tar.gz /opt/tools
    ADD index.html /opt/tools
    
    # RUN:Install configuration command
    RUN yum install -y vim net-tools lrzsz wget gcc gcc-c++ make openssl-devel
    RUN useradd -s /sbin/nologin -M www
    
    # WORKDIR:Specify the workdir
    WORKDIR /opt/tools/nginx-1.8.0
    RUN ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-pcre && make && make install
    RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf
    RUN cd /usr/local/nginx/html
    RUN mv index.html index.html.ori
    RUN cp /opt/tools/index.html .
    
    ENV PATH /usr/local/nginx/sbin:$PATH
    EXPOSE 80
    
    CMD ["nginx"]
    ########## END ##########
    --------------------------
    
    • 注意:
    1.Dockerfile 文件名首字母必须大写,docker 规定
    2.行首的 # 才代表注释信息,行中间的 # 不识别为注释
    3.创建失败的镜像文件需要手动删除,命令:
    docker rmi <镜像名称>
    4.Dockerfile 文件需要包含以下字段的信息,各个命令字段需要全部大写:
    --------------------
    1)基础镜像信息
    FROM centos
    
    2)维护者信息
    MAINTAINER zhaoshuai zhaoshuai@zuiyoujie.com
    
    3)容器中的操作指令
    RUN mkdir -p /opt/tools
    
    4)上传宿主机的文件到指定目录,压缩包会自动解压
    ADD nginx-1.8.0.tar.gz /opt/tools
    
    5)切换工作目录,类似于cd
    WORKDIR /opt/tools/nginx-1.8.0
    
    6)申明环境变量
    ENV PATH /usr/local/nginx/sbin:$PATH
    
    7)配置容器项目对外的端口号
    EXPOSE 对外端口号
    
    8)容器启动时执行指令
    CMD ["nginx"]
    
    9)设置卷,挂载主机目录
    VOLUME
    --------------------
    
    • Dockerfile 参数解释

    参考:https://www.abcdocker.com/abcdocker/1724

    4.3.3.在当前目录构建 docker 镜像

    ------------------------
    [root@zuiyoujie nginx]# docker build -t mynginx:v2 .
    Sending build context to Docker daemon  836.6kB
    Step 1/12 : FROM centos
     ---> 5182e96772bf
    Step 2/12 : MAINTAINER zhaoshuai
     ---> Running in 7b6960526477
    Removing intermediate container 7b6960526477
     ---> 86a802d12e7d
    Step 3/12 : RUN mkdir -p /opt/tools
     ---> Running in eae1592b5043
    Removing intermediate container eae1592b5043
     ---> 2d0176de364e
    Step 4/12 : ADD nginx-1.8.0.tar.gz /opt/tools
     ---> 24595cf44b63
    Step 5/12 : RUN yum install -y vim net-tools lrzsz wget gcc gcc-c++ make openssl-devel
     ---> Running in 466996bbfacd
    Loaded plugins: fastestmirror, ovl
    Determining fastest mirrors
     * base: mirrors.huaweicloud.com
     * extras: mirrors.huaweicloud.com
     * updates: mirrors.huaweicloud.com
    Resolving Dependencies
    --> Running transaction check
    ---> Package gcc.x86_64 0:4.8.5-28.el7_5.1 will be installed
    --> Processing Dependency: libgomp = 4.8.5-28.el7_5.1 for package: gcc-4.8.5-28.el7_5.1.x86_64
    --> Processing Dependency: cpp = 4.8.5-28.el7_5.1 for packa
    ......
      perl-threads.x86_64 0:1.87-4.el7                                              
      perl-threads-shared.x86_64 0:1.43-6.el7                                       
      vim-common.x86_64 2:7.4.160-4.el7                                             
      vim-filesystem.x86_64 2:7.4.160-4.el7                                         
      which.x86_64 0:2.20-7.el7                                                     
      zlib-devel.x86_64 0:1.2.7-17.el7                                              
    
    Complete!
    Removing intermediate container 466996bbfacd
     ---> 6af873137dee
    Step 6/12 : RUN useradd -s /sbin/nologin -M www
     ---> Running in a7cb7eb3806a
    Removing intermediate container a7cb7eb3806a
     ---> a7b7aba0d32a
    Step 7/12 : WORKDIR /opt/tools/nginx-1.8.0
    Removing intermediate container 5e7bc1b22502
     ---> a26176ee7848
    Step 8/12 : RUN ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-pcre && make && make install
     ---> Running in 2f6fbca0e59a
    checking for OS
     + Linux 3.10.0-862.9.1.el7.x86_64 x86_64
    checking for C compiler ... found
     + using GNU C compiler
     + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
    checking for gcc -pipe switch ... found
    checking for gcc builtin atomic operations ... found
    checking for C99 variadic macros ... found
    checking for gcc variadic macros ... found
    
    -------------- 开始编译,可以看出,每执行 RUN 一条命令,会新生成一个容器镜像 ------------
    test -d '/usr/local/nginx/logs' ||              mkdir -p '/usr/local/nginx/logs'
    test -d '/usr/local/nginx/html'                 || cp -R html '/usr/local/nginx'
    test -d '/usr/local/nginx/logs' ||              mkdir -p '/usr/local/nginx/logs'
    make[1]: Leaving directory `/opt/tools/nginx-1.8.0'
    Removing intermediate container 2f6fbca0e59a
     ---> 69f198548070
    Step 9/12 : RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf
     ---> Running in c4e606942edd
    Removing intermediate container c4e606942edd
     ---> 9ff2a70821d6
    Step 10/12 : ENV PATH /usr/local/nginx/sbin:$PATH
     ---> Running in 1504f2334f44
    Removing intermediate container 1504f2334f44
     ---> 067d03029d09
    Step 11/12 : EXPOSE 80
     ---> Running in e3980bb8fbe5
    Removing intermediate container e3980bb8fbe5
     ---> d763445650e9
    Step 12/12 : CMD ["nginx"]
     ---> Running in e50d1a33f886
    Removing intermediate container e50d1a33f886
     ---> 9e202848bd6b
    Successfully built 9e202848bd6b
    Successfully tagged mynginx:v2
    
    ------------- 至此安装完成,可以查看docker镜像仓库 --------------
    [root@zuiyoujie nginx]# docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
    mynginx             v2                  9e202848bd6b        About a minute ago   452MB	# 这个是我构建的镜像
    zhaoshuai/mynginx   v1                  f8c6d06b3f85        18 hours ago         109MB
    centos              latest              5182e96772bf        2 weeks ago          200MB
    nginx               latest              c82521676580        4 weeks ago          109MB
    hello-world         latest              2cb0d9787c4d        6 weeks ago          1.85kB
    [root@zuiyoujie nginx]# 
    
    # 测试启动镜像
    docker run --name mynginx-test -d -p 82:80 mynginx:v2
    需要加镜像版本号
    ---------------------------------
    [root@zuiyoujie nginx]# docker run --name mynginx-test -d -p 82:80 mynginx:v2
    34a3b135ede4fb473a7b9383529ba738d469fbe0781f0a1dd357c9ea81b90024
    [root@zuiyoujie nginx]# docker ps -a
    CONTAINER ID        IMAGE                  COMMAND                  CREATED              STATUS                    PORTS                   NAMES
    34a3b135ede4        mynginx:v2             "nginx"                  About a minute ago   Up About a minute         0.0.0.0:82->80/tcp      mynginx-test
    [root@zuiyoujie nginx]# curl 127.0.0.1:82
    www.zuiyoujie.com
    ---------------------------------
    # 至此自动创建的docker镜像文件就完成了
    

    END

  • 相关阅读:
    C# WPF透明黑色样式窗口
    ExtJS速学
    编译away3d例程序记
    用VC加载Lua.lib,C++调用lua脚本函数
    mysql数据库备份
    Google TTS(文字转语音)api 2
    C Sharp Coding Standards
    微信公众平台功能大杂烩 ip/域名查询 车牌号归属地查询 手机归属地查询 公交查询 英汉互译
    google jsapi学习记录
    Google TTS(文字转语音)api
  • 原文地址:https://www.cnblogs.com/tssc/p/13902131.html
Copyright © 2011-2022 走看看