Docker容器

Linux系统环境基于Docker搭建系统基础镜像

本文主要是介绍Linux系统环境基于Docker搭建系统基础镜像,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

制作系统基础环境镜像

  • 基于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<marklin1992@outlook.com>

########################################
#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

版权声明:本文为博主原创文章,遵循相关版权协议,如若转载或者分享请附上原文出处链接和链接来源。

点击查看更多内容
这篇关于Linux系统环境基于Docker搭建系统基础镜像的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!