第一章 环境搭建
安装Ubuntu,推荐使用20.04版本。用户名不能包含中文。
1. 安装docker
2. 创建以下三个文件,文件名分别为dockerfile、build-container、entrycontainer
2.1 dockerfile
2.1.1 作用
dockerfile 是一个文本文件,包含了一系列用于构建 Docker 镜像的指令。
2.1.2 创建方法
(1)cd ~ (2)mkdir proj (3)cd ./proj (4)vim dockerfile (5)将以下文本粘贴进来后保存。
FROM ubuntu:18.04
ARG TARGETPLATFORM
ARG DEBIAN_FRONTEND=noninteractive
ARG userid
ARG groupid
ARG username
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
RUN cp -a /etc/apt/sources.list /etc/apt/sources.list.bak
RUN sed -i 's@http://.*ubuntu.com@http://repo.huaweicloud.com@g' /etc/apt/sources.list
RUN dpkg --add-architecture i386 && \
apt-get update && \
apt-get install -y locales && \
localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
ENV LANG en_US.UTF-8
RUN apt-get install --no-install-recommends --no-install-suggests --yes \
binutils git git-lfs gnupg flex bison gperf build-essential zip \
curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev \
x11proto-core-dev libx11-dev lib32z1-dev ccache libgl1-mesa-dev libxml2-utils \
xsltproc unzip m4 bc gnutls-bin python3.8 python3-pip ruby openjdk-8-jdk \
python3-distutils dosfstools mtools libssl-dev libelf-dev sudo vim openssh-client wget libfl-dev liblz4-tool scons mtd-utils u-boot-tools default-jdk cpio genext2fs gcc-arm-none-eabi && \
apt-get clean && \
rm -rf /var/lib/apt/* /var/cache/apt/* /tmp/* /var/tmp/*
RUN pip3 install setuptools kconfiglib
RUN pip3 install scons ecdsa pycryptodome
RUN pip3 install --upgrade --ignore-installed six
RUN rm -f /usr/bin/python
RUN ln -s /usr/bin/python3.8 python
RUN groupadd -g $groupid $username \
&& useradd -m -u $userid -g $groupid $username \
&& echo "$username:123456" | chpasswd \
&& echo $username >/root/username
RUN sed -i -e '/\%sudo/ c \%sudo ALL=(ALL) NOPASSWD: ALL' /etc/sudoers
RUN usermod -a -G sudo $username
RUN echo "root:123456" | chpasswd
ENV HOME=/home/$username
ENV USER=$username
WORKDIR $HOME
ENV HOME=/home/$username
ENV USER=$username
ENV WORKFOLDER=/home/$username/proj
ENV GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
ENV PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home'
RUN mkdir -p $WORKFOLDER
ENTRYPOINT chroot --userspec=$(cat /root/username):$(cat /root/username) / /bin/bash -c "cd $WORKFOLDER && exec /bin/bash -i"
在第39行,使用传入的 groupid 创建组; 在第40行,使用传入的 userid 创建用户; 在第41行,修改用户密码为123456,可在此处将用户的默认密码自行修改为自身常用的密码; 在第48行,将root的密码设置为123456,此处可将其修改为自身常用的密码。
2.2 build-container
2.2.1 作用
该脚本用于:根据dockerfile创建出所需的镜像(image)。
2.2.2 创建方法
(1)cd ~/proj (2)vim build-container (3)将以下文本粘贴进来后保存。
#!/bin/bash
USER_ID=$(id -u)
GROUP_ID=$(id -g)
USERNAME=$(whoami)
docker build --build-arg userid=$USER_ID --build-arg groupid=$GROUP_ID --build-arg username=$USERNAME --tag sc3568ha2:latest .
第3、4、5行将主机当前用户的组ID、用户ID、用户名称传递给dockerfile,在2.1所创建的dockerfile中,会根据传入的这三个参数创建一个新用户。 第8行末尾的 " . " 用于指定dockerfile所处位置,通常我们将dockerfile、build-container、entrycontainer这三个文件都放在同一路径下,因此此处就使用 " . " ,表示dockerfile处于当前目录下。 第8行的"sc3568ha2:latest"用于指定创建出的镜像的名称及其版本号,可自行修改,格式为"镜像名称:版本号"。 (4)chmod +x ./build-container
2.3 entrycontainer
2.3.1 作用
该脚本用于:根据镜像创建并进入容器。
2.3.2 创建方法
(1)cd ~/proj (2)vim entrycontainer (3)将以下文本粘贴进来后保存。
#!/bin/bash
set -e
HOME_DIR="/home/xxx/proj"
BUILD_DIR="/home/xxx/proj"
docker run --privileged --volume ${BUILD_DIR}:${HOME_DIR}:rw --volume /tmp:/tmp \
--workdir=${HOME_DIR} \
--env TERM=xterm-256color --env SHELL=/bin/bash \
--rm --init --tty --interactive \
--hostname docker_OP \
sc3568ha2:latest
使用docker的一个便利之处在于可以将主机文件夹映射到容器中,我们进入到容器中后,仿佛是在使用一台新的电脑的同时,还可以访问主机的指定文件夹。 第9行的"--volume ${BUILD_DIR}:${HOME_DIR}:rw"是实现这一映射功能的语句,其将主机文件夹BUILD_DIR映射到容器的路径HOME_DIR下面,当我们进入容器中后,可以在路径HOME_DIR下面访问主机文件夹BUILD_DIR下面的所有文件。 第6行的路径通常也是"/home/xxx/proj",与容器内路径一致,以避免混淆。其中,xxx是当前用户的用户名称。 (4)chmod +x ./entrycontainer
3. 创建镜像
cd ~/proj
bash ./build-container
4. 进入容器
cd ~/proj
bash ./entrycontainer
5. 配置默认命令行解释器为bash
ls -l /bin/sh #如果显示为“/bin/sh -> bash”则为正常,否则请以以下方式修改:
sudo dpkg-reconfigure dash #然后选择no