从零开始学习制作、以及使用Tina的Docker编译镜像

### 前言
菜鸡玩家第一次接触linux的buildroot环境,还在熟悉中。对于长期使用linux命令行的、熟悉openharmony构建系统的人来说,更清楚编译环境对小白来说有多蛋疼。作为一个重度的docker环境使用者,以及小白折腾编译环境的劝退佬来说,有一句话特别想对想学linux的小白说。
本次制作镜像所需的文件已打包放在附件,想直接使用的,可以直接从**第2节** **创建docker镜像**开始。不想自己制作的,可以从**第3节** **创建docker容器**开始。
---
**你有空去折腾linux的编译环境,还不如来学学docker镜像的制作**
---
#### 用docker制作编译环境的好处
1. docker镜像的制作脚本,可以添加注释,作为学习过程记录和知识积累。
2. 学习docker镜像制作,能更好的理解linux权限
3. 加深对系统目录的理解
4. 重装系统时,不需要浪费大量时间回忆环境搭建过程
5. docker容器玩坏了没关系,重新用镜像创建一个新的就行
6. 工具链更新时,可以通过快速调整制作脚本快速升级环境
7. 启动快速,培养命令行使用习惯
---
#### docker的缺点
1. win下不能调用usb设备(无法直接烧录)
2. docker需要学习和理解(基本上花2小时在b站看一下入门教程即可)
3. 重度命令行工具(使用命令行完成几乎所有操作)
---
#### 开始学习制作docker镜像
准备工作:安装docker(百度很多教程,没啥好说,不废话),然后将附件里的压缩包解压缩到任意目录,在命令行中进入该目录下。
1. 编制制作脚本文件tina_env.dockfile。
  这里我已经制作好了,实际上脚本也没什么复杂的东西。简单来说,也就是from命令指定镜像的基础包。env命令设置镜像的环境变量。workdir命令设置进入容器的默认路径(本镜像默认把源码挂载在/mnt目录,所以工作区也设置为该路径)。copy命令将主机目录的内容复制到镜像内。run命令会在构建镜像时进入镜像环境中执行相关的linux指令(比方说给镜像安装指定的软件包,文件的处理等)。
  我们看一下本次制作tina_env的脚本文件。文件开头可以看到以下内容(均有注释,就不一一解释了):
```
    # 使用python2.7的精简版debian镜像作为基础
from python:2.7-slim-buster
    # 调整时区
env tz asia/shanghai
    # 设置默认工作路径
workdir /mnt
    # 拷贝主机的目录内容(.bashrc以及可执行的repo程序)到镜像内
copy docker/vuser/* /root/
    # 添加普通用户组
run groupadd -g 1000 tina && \
    # 添加普通用户
    useradd vuser -r -m --uid 1000 -g tina --shell /bin/bash && \
    # 复制powerline至/usr/bin目录,并调整权限
    mv /root/powerline-go /usr/bin/powerline-go && chmod 755 /usr/bin/powerline-go && \
    # 为root用户添加python国内源
    mkdir ~/.pip && \
    echo '[global]' >> ~/.pip/pip.conf && \
    echo 'index-url = https://pypi.tuna.tsinghua.edu.cn/simple' >> ~/.pip/pip.conf && \
    echo '[install]' >> ~/.pip/pip.conf && \
    echo 'trusted-host = https://pypi.tuna.tsinghua.edu.cn' >> ~/.pip/pip.conf && \
    # 复制pip国内源配置至普通用户
    cp -rv ~/.pip /home/vuser/ && chown -r vuser:tina /home/vuser/.pip && \
    # 修改系统软件包国内源
    sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list && \
    sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list && \
    # 添加 i386 环境支持
    dpkg --add-architecture i386 && \
    apt-get update && apt-get install apt-utils -y && \
    # 安装常见软件库
    apt-get install curl wget pv git lbzip2 libncurses5 libncursesw5 nano -y && \
    # 安装buildroot 必须依赖的软件库
    apt-get install sed gawk make binutils diffutils gcc g++ bash patch gzip bzip2 perl \
            tar cpio unzip rsync bc gawk libncurses-dev libssl-dev zlib1g-dev xz-utils file -y && \
    # 安装 i386环境支持库
    apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 busybox rsync lzma -y && \
    # 清理缓存,减少镜像体积
    apt-get clean && \
    # 复制bashrc配置文件到vuser用户
    cd /root && cp .bashrc .bash_aliases /home/vuser/ && chown -r vuser:tina /home/vuser && \
    # 创建工具链目录
    mkdir /opt/toolchains && chmod 666 /opt/toolchains
    # 容器创建后,默认登陆以bash作为登陆环境
cmd [/bin/bash]
```
2. 创建docker镜像
  在磁盘的任意位置创建一个任意目录(比如docker_build), 然后把附件的压缩包解压缩到这个目录里,然后用命令行进入这个目录(docker_build)。在命令行中执行以下命令(需要将walker2048替换成你自己的dockerhub用户名):
```
docker build -t walker2048/tina_env -f docker/tina_env.dockerfile --network host  .
```
命令解析:
build参数:告诉docker要执行创建镜像的工作
-t walker2048/tina_env 参数: 告诉docker这个镜像的tag标签(walker2048/tina_env)。可以理解成镜像的标识符(类似地址和姓名),这个tag标签是可以修改的,把镜像上传到dockerhub上也需要指定正确的标签。
-f docker/tina_env.dockerfile 参数: 告诉docker使用哪一个构建脚本(可以使用绝对路径和相对路径)
--network host参数: 告诉docker使用主机的网络配置
最后的. 参数:将当前目录的所有内容传递给docker作为临时输入内容(copy命令需要使用)。
---
正确执行命令后,网络正常的情况下,大概只需要5分钟左右,就顺利完成镜像构建了。命令行输出输出类似下图:
```
sending build context to docker daemon  95.64mb
step 1/7 : from python:2.7-slim-buster
---> eeb27ee6b893
step 2/7 : env tz asia/shanghai
---> using cache
---> 0c0e432b82be
step 3/7 : workdir /mnt
---> using cache
---> 0d54e5487ee3
step 4/7 : copy docker/vuser/* /root/
---> using cache
---> 3e1598cc56a3
step 5/7 : copy docker/bin/* /usr/bin/
---> using cache
---> 40895711f1dc
step 6/7 : run groupadd -g 1000 tina &&     useradd vuser -r -m --uid 1000 -g tina --shell /bin/bash &&     mv /root/powerline-go /usr/bin/powerline-go && chmod 755 /usr/bin/powerline-go &&     mkdir ~/.pip &&     echo '[global]'
>> ~/.pip/pip.conf &&     echo 'index-url = https://pypi.tuna.tsinghua.edu.cn/simple' >> ~/.pip/pip.conf &&     echo '[install]' >> ~/.pip/pip.conf &&     echo 'trusted-host = https://pypi.tuna.tsinghua.edu.cn' >> ~/.pip/pip.conf
&&     cp -rv ~/.pip /home/vuser/ && chown -r vuser:tina /home/vuser/.pip &&     sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list &&     sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list &&     dpkg --add-architecture i386 &&     apt-get update && apt-get install apt-utils -y &&     apt-get install curl wget patch pv git zip lbzip2 libncurses5 libncursesw5 nano -y &&     apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 busybox rsync lzma -y &&     apt-get clean &&     cd /root && cp .bashrc .bash_aliases /home/vuser/ && chown -r vuser:tina /home/vuser &&     mkdir /opt/toolchains && chmod 666 /opt/toolchains
##
篇幅原因,中间省略各种软件源更新和软件包安装的内容
##
---> using cache
---> 3a44cb328437
step 7/7 : cmd [/bin/bash]
---> using cache
---> 6b65b7402ecd
successfully built 6b65b7402ecd
successfully tagged walker2048/tina_env:latest
```
当我们看到所有的步骤(step)都正确执行了,最后也成功生成了镜像的时候,就可以开始下一步创建docker容器了。
3. 创建docker容器
  创建容器前,需要先理解一下创建容器的一些基础知识。
  3.1 容器类似独立的虚拟机,除了挂载上去的目录,文件系统和寄主机是不相通的。
  3.2 可以创建名称不同的、但是使用的镜像相同的容器。容器间是独立的关系。
  3.3 容器网络使用方式需要指定,一般编译服务器无特殊要求的,可以直接指定使用host网络。
  3.4 如果是linux系统,想让docker容器访问usb设备,可以使用--privileged参数开启。
  3.5 创建容器(虚拟机)后,下次使用不需要重新创建。
linux或者bash环境使用以下命令
```
docker run -it --name tina_build  \
 --net=host                      \
 --privileged                    \
 -v /mnt/iot:/mnt                \
 walker2048/tina_env
```
windows环境使用以下命令
```
docker run -it --name tina_build --net=host -v /mnt/iot:/mnt walker2048/tina_env
```
参数解析:
run 参数: 告诉docker需要创建容器
-it 参数: 创建完毕后自动运行容器时,使用命令行交互方式运行(开启这个参数才能使用命令行交互)
--name tina_build 参数: 告诉docker把这个容器命名为tina_build(后期运行容器需要使用这个)
--net=host 参数:指定容器的网络模式为host模式(和主机同一个网络)
--privileged 参数:特权模式,linux环境下才有用,开启后可以访问usb设备
-v /mnt/iot:/mnt 参数:挂载寄主机的/mnt/iot目录到容器的/mnt目录(都需要使用绝对路径,文件双方共享,可读写)
walker2048/tina_env 参数: 告诉docker使用哪个镜像来创建容器。
进入容器后,命令行提示内容如下(容器默认使用了powerline-go,简单美化了一下):
repo程序需要配置用户信息,所以只能自行配置和安装  #docker#    。
此时需要给vuser用户添加密码(只需要执行一次,没有密码不能使用login命令切换用户),在容器的命令行输入
```
```
> passwd vuser
然后就可以用login命令切换到vuser用户了。切换用户后就可以按tina sdk的说明下载和编译源码了(为什么要切换用户,因为和寄主机子共享了目录,如果寄主机子是linux环境,用root用户操作会影响寄主机对目录的读写)。保持良好的用户习惯是个好事儿。一般情况下,linux环境的常规用户id是1000开始的。
4. 退出容器和重新运行环境的方法。
  4.1 退出容器。
  在容器命令行输入exit(如果之前用root用户进入容器,然后使用login命令切换用户的,需要退出至少两次)。或者直接关闭命令行。
  4.2 容器未停止时进入容器的方式(-u 参数指定用户为root或者vuser):
> docker exec -it -u root tina_build bash
若命令行提示容器为停止状态(或者寄主机重启过),需要先使用以下命令开启容器。
> docker start tina_build
也可以通过-d参数,告诉docker以守护形式长时间开启容器(直到使用命令行关闭或者寄主机关机)
#### 本次分享的内容就是这些,希望能帮助到有需要的朋友。
因为这边不能上传附件,.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 anythingcase $- in *i*) ;; *) return;;esac# don't put duplicate lines or lines starting with space in the history.# see bash(1) for more optionshistcontrol=ignoreboth# append to the history file, don't overwrite itshopt -s histappend# for setting history length see histsize and histfilesize in bash(1)histsize=1000histfilesize=2000# check the window size after each command and, if necessary,# update the values of lines and columns.shopt -s checkwinsize# if set, the pattern ** used in a pathname expansion context will# match all files and zero or more directories and subdirectories.#shopt -s globstar# 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|*-256color) 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=yesif [ -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= fifiif [ $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\$ 'fiunset color_prompt force_color_prompt# if this is an xterm set the title to user@host:dircase $term inxterm*|rxvt*) ps1=\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$ps1 ;;*) ;;esac# enable color support of ls and also add handy aliasesif [ -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# colored gcc warnings and errors#export gcc_colors='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'# some more ls aliases#alias ll='ls -l'#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_aliasesfi# 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 ! shopt -oq posix; then if [ -f /usr/share/bash-completion/bash_completion ]; then . /usr/share/bash-completion/bash_completion elif [ -f /etc/bash_completion ]; then . /etc/bash_completion fifiexport path=/home/vuser/.bin:$pathexport path=/opt/toolchains:$pathfunction _update_ps1() { ps1=$(powerline-go -error $? -cwd-mode semifancy -trim-ad-domain -newline -jobs $(jobs -p | wc -l)) # uncomment the following line to automatically clear errors after showing # them once. this not only clears the error for powerline-go, but also for # everything else you run in that shell. don't enable this if you're not # sure this is what you want. #set ?}if [ $term != linux ] && [ -f /usr/bin/powerline-go ]; then prompt_command=_update_ps1; $prompt_commandfi  

WIFI已连接但无法访问互联网反反复复是什么原因
9针rs232串口接线图以及接线方法
人工智能“围城”渐成 未来的竞技关键词在哪里?
UVC LED关注度及市场热度逐步提升 研晶光电大力布局相关市场
荣耀V40正式发布,突破性游戏体验引关注
从零开始学习制作、以及使用Tina的Docker编译镜像
浅谈电力设备状态监测与智能运维
RFID可定距读卡器
什么是FET?FinFET将半导体制程带入新境界
恩智浦可调光市电LED驱动解决方案
全球首款丨ams为全面屏智能手机的“OLED屏下”应用加 Buff 了!
MAC和PC哪个最好用
“DCR电流检测”随风而去,我得告诉你这几种电路
5G时代应该具有怎样的语音方案
反无人机技术2024年市场规模将达18.5亿美元
数显型压力变送器怎样测量冲击型压力
华为助力泰国农业旅游业数智化升级,建立AI大模型
温湿度传感器在你乘坐或等候地铁时提供了舒适的环境
智慧工厂数据采集监控方案,自动化实现就是这么简单
IT安全设备市场规模稳步增长,企业纷纷引入网络安全技术及设备