• Docker构建自定义镜像(shell为zsh)


    Ⅰ. 要构建一个什么样的镜像

    1. 不是 root 用户
    2. 利用 tzdata 调节到 Asia/Shanghai 时区
    3. 采用 zsh 而不是默认的 bash,并且为 zsh 安装两个插件 zsh-syntax-highlighting 和 zsh-autosuggestions
    4. 安装一些基础软件
      • ssh
      • vim
      • samba
      • c/c++ 基础环境
      • python3.6 环境
      • ……
    5. ……

    Ⅱ. 编写Dockerfile文件

    首先 Dockerfile 的第一行都是 FROM

    我采用 Ubuntu:18.04 来构建我的基础镜像

    FROM ubuntu:18.04
    
    • 1

    创建用户

    首先需要更新 apt 包管理系统,不然无法找到软件。默认镜像只提供了一个最基础的 rootfs,为了使普通用户享有 sudo 权限,因此首先需要安装 sudo,然后通过 shell 命令创建用户,修改 sudoers 文件。

    # 创建一个普通用户并增加sudo权限,不需要密码
    RUN apt update \
        && apt install -y sudo \
        && useradd -m ubuntu -s /bin/bash \
        && adduser ubuntu sudo \
        && echo "ubuntu ALL=(ALL) NOPASSWD:ALL" | tee /etc/sudoers.d/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    useraddadduser 用法:Ubuntu创建用户adduser和useradd

    tee 命令:Linux tee命令

    调节时区

    # 调节时区
    ENV DEBIAN_FRONTEND=noninteractive
    RUN apt install -y tzdata \
        && ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    
    • 1
    • 2
    • 3
    • 4

    tzdata 命令:在自动化运维中设置apt-get install tzdata的noninteractive方法

    安装zsh并为zsh安装插件

    # 安装ZSH
    RUN sudo apt install -y git zsh \
        && git clone https://github.com/ohmyzsh/ohmyzsh.git ~/.oh-my-zsh \  
        && cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc \
        && sed -i "s/robbyrussell/ys/" ~/.zshrc \
        && git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions \
        && git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting \
        && sed -i "s/plugins=(git.*)$/plugins=(git zsh-syntax-highlighting zsh-autosuggestions)/" ~/.zshrc \
        && sudo usermod -s /bin/zsh ubuntu
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    sed 命令:sed

    安装基础软件

    # 安装ssh
    RUN sudo apt install -y net-tools openssh-server
    
    # 安装vim
    RUN sudo apt install -y vim \
        && echo "set nu" >> ~/.vimrc
    
    # 安装Samba
    RUN sudo apt-get install -y samba
    
    # 安装C/C++基础环境
    RUN sudo apt-get install -y build-essential
    
    # 安装python3 pip
    RUN sudo apt-get install -y python3 python3-pip \
        && mkdir -p ~/.pip \
        && touch ~/.pip/pip.conf \
        && echo "[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple/ \n[install]\ntrusted-host = pypi.tuna.tsinghua.edu.cn" >> /home/ubuntu/.pip/pip.conf \
        && sudo rm /usr/bin/python \
        && sudo ln -s /usr/bin/python3 /usr/bin/python \
        && sudo ln -s /usr/bin/pip3 /usr/bin/pip
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    Ⅲ. 整个Dockerfile

    FROM ubuntu:18.04
    
    LABEL maintainer="huachaowu@qq.com"
    
    # 创建一个普通用户并增加sudo权限,不需要密码
    RUN apt update \
        && apt install -y sudo \
        && useradd -m ubuntu -s /bin/bash \
        && adduser ubuntu sudo \
        && echo "ubuntu ALL=(ALL) NOPASSWD:ALL" | tee /etc/sudoers
    
    # 调节时区
    ENV DEBIAN_FRONTEND=noninteractive
    RUN apt install -y tzdata \
        && ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    
    USER ubuntu
    WORKDIR /home/ubuntu
    
    # 安装ZSH
    RUN sudo apt install -y git zsh \
        && git clone https://github.com/ohmyzsh/ohmyzsh.git ~/.oh-my-zsh \  
        && cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc \
        && sed -i "s/robbyrussell/ys/" ~/.zshrc \
        && git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions \
        && git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting \
        && sed -i "s/plugins=(git.*)$/plugins=(git zsh-syntax-highlighting zsh-autosuggestions)/" ~/.zshrc \
        && sudo usermod -s /bin/zsh ubuntu
    
    # 安装ssh
    RUN sudo apt install -y net-tools openssh-server
    
    # 安装vim
    RUN sudo apt install -y vim \
        && echo "set nu" >> ~/.vimrc
    
    # 安装Samba
    RUN sudo apt-get install -y samba
    
    # 安装C/C++基础环境
    RUN sudo apt-get install -y build-essential
    
    # 安装python3 pip
    RUN sudo apt-get install -y python3 python3-pip \
        && mkdir -p ~/.pip \
        && touch ~/.pip/pip.conf \
        && echo "[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple/ \n[install]\ntrusted-host = pypi.tuna.tsinghua.edu.cn" >> /home/ubuntu/.pip/pip.conf \
        && sudo rm /usr/bin/python \
        && sudo ln -s /usr/bin/python3 /usr/bin/python \
        && sudo ln -s /usr/bin/pip3 /usr/bin/pip
    
    
    # 清除apt缓存
    RUN sudo apt autoremove \
        && sudo apt clean -y \
        && sudo rm -rf /var/lib/apt/lists/*
    
    EXPOSE 22
    ENTRYPOINT ["/bin/zsh"]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    Ⅳ. 生成Docker镜像

    docker build . -t incipe/ubuntu:18.04
    
    • 1

    Ⅴ. 验证Docker镜像

    Ⅵ. 上传到阿里云容器服务器

    Ⅶ. 总结

    终于可以抛弃虚拟机了O(∩_∩)O

  • 相关阅读:
    NodeJS V8引擎的内存和垃圾回收器(GC)
    2.3 矩阵消元
    redis的5种数据类型
    Flutter项目安装到Android手机一直显示在assembledebug
    轻松安装vue脚手架的一个详细讲解
    Spring Cloud Alibaba Gateway全局token过滤、局部过滤访问时间超过50ms日志提示
    线扫相机的使用
    LeetCode第20题:有效的括号
    ps制作透明公章 公章变透明 ps自动化批量抠图制作透明公章
    【大魔王送书第二期】搞懂大模型的智能基因,RLHF系统设计关键问答
  • 原文地址:https://blog.csdn.net/qq_43826212/article/details/126336514