docker学习笔记(四)

docker学习笔记(四)
Docker镜像管理
  docker run --name gitlab-redis -d sameersbn/redis:latest
    #将使用镜像sameersbn/redis:latest创建并运行容器,首先在本地查找,如果没有就会去官方仓库下载。
    #镜像下载后,容器不依赖镜像运行,可以删除。
  docker images -a            #查看本机已有的所有镜像
    #镜像唯一标识image id,和container ID一样


镜像的分层
  采用分层机制,相同部分独立成层,只需要存储一份就行。
  镜像通过联合文件系统(union filesystem)将各层文件系统叠加在一起,用户看来像是一个完整的文件系统。
  例如:第一层有三个文件夹,第二层有两个文件夹,使用联合文件系统后,用户可以看到五个文件夹,感觉不到分层存在。

  docker history sameersbn/redis:latest        #可以查看镜像分为多少层

  特性:已有的分层只能读不能修改;
       上层镜像的优先级高于底层镜像。已有的分层都不能修改,只能通过在源镜像基础上新增分层修改。
       联合文件最多层数为128层(aufs最多128层),层级太多会导致维护量大,使用dockerfile来更新维护

Dockerfile
  docker提供了dockerfile来管理镜像,如同GUN的makefile。
  语法:
    FROM:表示新的镜像来源,即最底层镜像
    MAINTAINER:指定该镜像创建者
    ENV:设置环境变量
    RUN:运行shell命令,多条命令使用“&&”连接
    COPY:将编译机本地文件拷贝到镜像文件系统中
    EXPOST:指定监听端口
    ENTRYPOINT:容器启动后执行的命令,启动前不执行
    
    例:
        FROM sameersbn/ubuntu:14.04.20160121        
        MAINTAINER sameer@damagehead.com

        ENV REDIS_USER=redis \
            REDIS_DATA_DIR=/var/lib/redis \
            REDIS_LOG_DIR=/var/log/redis

        RUN apt-get update \
        && DEBIN_FRONTEND=noninteractive apt-get install -y redis-server \
        && sed 's/^daemonize yes/daemonize no/' -i /etc/redis/redis.conf \
        && sed 's/^bind 127.0.0.1/bind 0.0.0.0/' -i /etc/redis/redis.conf \
        && sed 's/^# unixsocket /unixsocket /' -i /etc/redis/redis.conf \
        && sed 's/^# unixsocketperm 755/unixsocketperm 777/' -i /etc/redis/redis.conf \
        && sed '/^logfile/d' -i /etc/redis/redis.conf \
        && rm -rf /var/lob/apt/lists/*

        COPY entrypoint.sh /sbin/entrypoint.sh
        RUN chmod 755 /sbin/entrypoint.sh

        EXPOSE 6379/tcp
        VOLUME ["#{REDIS_DATA_DIR}"\
        ENTRYPOING ["/sbin/entrypoint.sh"]


  编译Dockerfile
    docker build -t image_redis:v1.0 Dockerfile
    选项:-t:给镜像齐名(带版本号)

定制私有的基础镜像
  debootstrap:用来构建一套基本的系统(根文件系统),生成的目录符合Linux文件系统标准(FHS),即包含/boot、/etc、/bin、/usr等等目录,但是体积小很多。类似工具:febootstrap(fedora版)
    命令格式:debootstap --arch [平台][发行版本代号][目录]
    1、安装debootstrap
    apt-get install debootstrap
    #centos下安装
     1、wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
     2、rpm -Uvh epel-release-7-5.noarch.rpm
     3、yum repolist all
     4、yum install debootstrap.noarch -y
    2、制作镜像
    debootstrap --arch amd64 trusty ubuntu-trustry http://mirrors.163.com/ubuntu/
        #--arch:指定CPU架构
        #trusty:发行版本14.04(代号为trust)
    cd ubuntu-trusty
        cp usr/share/zoneinfo/Asiz/Shanghai etc/localtime
        #系统时间修改为东八区时间
    3、提交生成基础镜像
    cd ubuntu-trusty
    tar -c . | docker import - ubuntu.14.04-baseimage:1.0
    4、查看镜像和创建容器
    docker images
    docker run -t -i ubuntu.14.04-baseimage:1.0 /bin/bash

  febootstrap使用
    1、安装
    yum -y install febootstrap(centos6)
        #wget http://rpmfind.net/linux/centos/6.10/os/x86_64/Packages/febootstrap-3.21-4.el6.x86_64.rpm
        # wget http://rpmfind.net/linux/centos/6.10/os/x86_64/Packages/fakechroot-2.9-24.5.el6_1.1.x86_64.rpm
        # wget https://www.dwhd.org/wp-content/uploads/2016/06/febootstrap-supermin-helper-3.21-4.el6_.x86_64.rpm
        # wget http://rpmfind.net/linux/centos/6.10/os/x86_64/Packages/fakechroot-libs-2.9-24.5.el6_1.1.x86_64.rpm
        # wget http://rpmfind.net/linux/centos/6.10/os/x86_64/Packages/fakeroot-1.12.2-22.2.el6.x86_64.rpm
        # wget http://rpmfind.net/linux/centos/6.10/os/x86_64/Packages/fakeroot-libs-1.12.2-22.2.el6.x86_64.rpm
    2、febootstrap参数
    febootstrap -i yum -i net-tools -i bash -i wget -i apt-get -i iputils -i iproute -i openssh-server -i openssh-clients centos7.4 centos7.4-doc https://mirrors.aliyun.com/centos/7.4.1708/os/x86_64/
        #-i:安装rpm包
    








posted @ 2020-04-08 21:27  zyxywy  阅读(166)  评论(0编辑  收藏  举报