由于日常需要经常用到某个镜像,所以需要将已经比较稳定的容器生成镜像便于后续复用。
harbar:一个镜像上传的云端仓库,自己搭建即可,地址本文为docker.###.###.com,日常打包文件目录为abc
1、dockerfile构建:后续便于一键构建,但是中间如果出错(如网络等等问题),可能需要清理镜像或容器
2、直接容器构建:不方便后续一键构建,但是过程便于调试清理问题,这里暂时采用该种思路(后续环境基本不变)
1、本地运行容器并在容器中安装所需要的包,调试是否具备脚本运行的环境
sudo docker run -it --name env_stability -d -v /dev/bus/usb:/dev/bus/usb -e TZ="Asia/Shanghai" --network host --privileged docker.###.###.com/abc/python3.9.5:v2 bash
确认容器运行无误后,进入容器安装所需的环境(各类包等,脚本用到的环境)
docker exec -it env_stability bash
接下来就是环境安装等,如
cat > /etc/apt/sources.list << EOF
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-updates main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bullseye-backports main contrib non-free
deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
# deb-src https://mirrors.tuna.tsinghua.edu.cn/debian-security bullseye-security main contrib non-free
EOF
apt update
apt install -y adb
pip3 install -U uiautomator2
pip3 install -U requests
2、以上确认环境ok后,将容器打包成镜像
sudo docker commit 45c4fc07### env_stability:v2
其中45c4fc07###是容器对应的容器id
操作完,可以通过images看到里面已经存在
###@ubuntu:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
env_stability v2 c87ae4f64041 5 seconds ago 1.54GB
3、由于需要上传到自己云端harbor,需要遵循一定的格式才能拉下来使用,所以给当前的镜像打个别名
sudo docker tag env_stability:v2 docker.###.###.com/abc/env_stability:v2
再看一下镜像
###@ubuntu:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
env_stability v2 c87ae4f64041 About a minute ago 1.54GB
docker.###.###.com/abc/env_stability v2 c87ae4f64041 About a minute ago 1.54GB
4、通过docker连接上harbor所在仓库
###@ubuntu:~$ sudo docker login http://docker.###.###.com/
Authenticating with existing credentials...
Stored credentials invalid or expired
Username (admin): admin
Password:
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
5、提交本地的镜像到云端仓库
###@ubuntu:~$ sudo docker push docker.###.###.com/adb/env_stability:v2
The push refers to repository [docker.###.###.com/abc/env_stability]
720eae20260d: Pushed
c4c5017a38ab: Mounted from iottest/python3.9.5
0d8eeeffc6a6: Mounted from iottest/python3.9.5
巴拉巴拉.....
6、云端harbor仓库查看是否推送成功
7、jenkins拉取镜像对比速度