Linux系統環境基於Docker搭建系統基礎鏡像

製作系統基礎環境鏡像

  • [x] 基於Ubuntu製作鏡像資源

⚠️[注意事項]:由於拉取的Docker hub 的大多數鏡像都沒有安裝Vim ss 等等資源,而且大部分鏡像資源是官方鏡像庫更新資源後安裝極其不方便,因此自己構建一個通用鏡像尤爲重要。

基於Ubuntu製作鏡像資源

1.在宿主機創建文件目錄:/docker/ubuntu/environment

mkdir -p /docker/ubuntu/environment

2.安裝的資源如下:

  • java
  • shh 等
  • vim
  • psmisc
  • rsync
  • supervisor
  • netcat

3.替換阿里雲的鏡像資源
bionic版本:

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

xenial版本:

deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse

⚠️[注意事項]:具體的依據自己的選擇的系統版本進行替換

4.編寫Dockerfile 文件:

########################################
#Setting source to base images
########################################
ARG IMAGE_VERSION=bionic-20191202
FROM ubuntu:${IMAGE_VERSION}

########################################
#Setting images author
########################################
MAINTAINER marklin<[email protected]>

########################################
#Setting and update datasource
########################################
WORKDIR /etc/apt
RUN mv sources.list sources-backup.list
RUN echo 'deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse' >>sources.list
RUN echo 'deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse' >>sources.list
RUN echo 'deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse' >>sources.list
RUN echo 'deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse' >>sources.list
RUN apt-get update

########################################
#Setting install software tools
########################################
RUN apt-get install vim openssh-server openssh-client openjdk-8-jdk ssh git dirmngr netcat psmisc rsync supervisor --assume-yes
RUN apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

########################################
#Setting  sytem path 
########################################
RUN rm -rf /root/.bashrc
ADD .bashrc /root
RUN chmod -R 777 /root/.bashrc


########################################
#Setting JAVA_HOME path
########################################
ENV JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
ENV PATH=${PATH}:${JAVA_HOME}/bin

########################################
#Setting ssh start 
########################################
RUN mkdir /var/run/sshd
RUN echo 'root:root' |chpasswd
RUN sed -ri 's/^#?PermitRootLogin\s+.*/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config
RUN mkdir /root/.ssh

########################################
#Setting hadoop start
########################################
WORKDIR /

EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

另外有一個全局的系統環境變量配置文件.bashrc:

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
    # We have color support; assume it's compliant with Ecma-48
    # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
    # a case would tend to support setf rather than setaf.)
    color_prompt=yes
    else
    color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
#    . /etc/bash_completion
#fi

#Setting JAVA_HOME
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export PATH=${PATH}:${JAVA_HOME}/bin

5.在/docker/ubuntu/environment目錄執行docker build命令:

docker build --no-cache  ./ -t  ubuntu:bionic-20191202

⚠️[注意事項]:
1.建議採用你選擇的系統版本號作爲鏡像版本號,譬如:bionic-20191202
2.構建完的鏡像有點偏大,或許是因爲添加阿里鏡像資源的緣故,需要使用docker-slim優化調整

6.把當前鏡像標識爲docker tag 倉庫支持的格式:

docker tag ubuntu:bionic-20191202 pivotalcloud/pivotal-ubuntu:bionic-20191202

⚠️[注意事項]:
1.推薦格式:鏡像倉庫名稱/鏡像名稱:鏡像版本號
2.鏡像賬戶需要自己去 Docker Hub 註冊

7.在宿主機安裝配置docker-slim:
在/usr/local/docker-slim安裝目錄:

wget  https://github.com/docker-slim/docker-slim/releases/download/1.26.1/dist_linux.tar.gz
ln -s /usr/local/docker-slim/dist_linux/docker-slim /usr/bin/docker-slim
ln -s /usr/local/docker-slim/dist_linux/docker-slim-sensor /usr/bin/docker-slim-sensor

使用docker-slim構建鏡像:

docker-slim build --http-probe ubuntu:bionic-20201108

打成目標鏡像:

docker tag ubuntu.slim:latest pivotalcloud/pivotal-ubuntu:18.04

⚠️[注意事項]:
docker-slim 使用需要Go語言支持:

export GOPATH=/usr/local/go
export GOROOT=/usr/local/go/go-1.13.5
export GOARCH=386
export GOOS=linux
export GOTOOLS=${GOROOT}/pkg/tool
export PATH=${PATH}:${GOROOT}/bin:${GOPATH}/bin

7.登錄Docker hub 賬戶,把本地鏡像推送遠程倉庫:

docker push pivotalcloud/pivotalcloud/pivotal-ubuntu:18.04

8.Docker hub驗證:
l2IGHs.png

版權聲明:本文爲博主原創文章,遵循相關版權協議,如若轉載或者分享請附上原文出處鏈接和鏈接來源。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章