Chapter 1 Environment Setup
Install Ubuntu, version 20.04 is recommended. The username cannot contain Chinese characters.
- Reference 1: https://laval.csdn.net/648aaf800fa9cc227b4def20.html
- Reference 2: https://forums.openharmony.cn/forum.php?mod=viewthread&tid=897&extra=page%3D1&login=from_csdn
1. Install Docker
2. Create the following three files, with filenames: dockerfile, build-container, entrycontainer
2.1 dockerfile
2.1.1 Purpose
A dockerfile is a text file containing a series of instructions for building a Docker image.
2.1.2 Creation Method
(1) cd ~
(2) mkdir proj
(3) cd ./proj
(4) vim dockerfile
(5) Paste the following text and save.
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"- On line 39, use the passed groupid to create a group;
- On line 40, use the passed userid to create a user;
- On line 41, modify the user password to 123456. You can change the user's default password to your commonly used password here;
- On line 48, set the root password to 123456. You can change this to your commonly used password.
2.2 build-container
2.2.1 Purpose
This script is used to create the required image (image) based on the dockerfile.
2.2.2 Creation Method
(1) cd ~/proj
(2) vim build-container
(3) Paste the following text and save.
#!/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 .- Lines 3, 4, and 5 pass the current user's group ID, user ID, and username on the host to the dockerfile. In the dockerfile created in 2.1, a new user will be created based on these three passed parameters.
- The " . " at the end of line 8 is used to specify the location of the dockerfile. Since we typically place all three files (dockerfile, build-container, entrycontainer) in the same path, we use " . " here to indicate that the dockerfile is in the current directory.
- The "sc3568ha2:latest" on line 8 is used to specify the name and version of the created image. You can modify it yourself, in the format "image name: version number".
(4) chmod +x ./build-container
2.3 entrycontainer
2.3.1 Purpose
This script is used to create and enter a container based on the image.
2.3.2 Creation Method
(1) cd ~/proj
(2) vim entrycontainer
(3) Paste the following text and save.
#!/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- One convenience of using Docker is that it can map host folders to the container. When we enter the container, we feel like we are using a new computer while also being able to access specific host folders.
- The "--volume ${BUILD_DIR}😒{HOME_DIR}:rw" on line 9 is the statement that implements this mapping function. It maps the host folder BUILD_DIR to the path HOME_DIR in the container. When we enter the container, we can access all files in the host folder BUILD_DIR under the path HOME_DIR.
- The path on line 6 is also typically "/home/xxx/proj", consistent with the path inside the container to avoid confusion. Among them, xxx is the name of the current user and needs to be manually modified.
(4) chmod +x ./entrycontainer
3. Create Image
cd ~/proj
bash ./build-container4. Enter Container
cd ~/proj
bash ./entrycontainer5. Configure Default Command Line Interpreter as bash
ls -l /bin/sh #If it shows "/bin/sh -> bash", it's normal. Otherwise, modify as follows:
sudo dpkg-reconfigure dash #Then select no