使用docker构建基础镜像

基础镜像包含内容
centtos7.9
jdk17
androidSdk
nodejs
appium

一、文件准备

androidSdk文件下载

https://developer.android.google.cn/studio?hl=zh-cn

在这里插入图片描述

java17 文件下载

https://www.oracle.com/java/technologies/downloads/#java17

在这里插入图片描述

docker image准备

docker pull centos:7.9.2009

二、创建dockfile

#-- 第一步:指定基础镜像
FROM centos:7.9.2009
MAINTAINER "centtos7.9 + jdk17 + androidSdk + nodejs + appium" admin@juc.net

# 安装常用命令
RUN yum install -y curl \
    && yum install -y wget \
    && yum install -y zip \
    && yum install -y unzip \
    && yum install -y tar \
    && yum install -y lsof \
    && yum install -y git


#-- 第二步: 安装jdk17
WORKDIR /usr/local/java
ADD jdk-17_linux-x64_bin.tar.gz  /usr/local/java/
ENV JAVA_HOME=/usr/local/java/jdk-17.0.9
ENV CLASSPATH=.:$JAVA_HOME/lib/jrt-fs.jar
ENV PATH=$PATH:$JAVA_HOME/bin



#-- 第三步: 安装 NODEJS,NPM,appium2
RUN yum install -y epel-release\
    && yum install -y nodejs\
    && yum install -y npm

RUN node -v
RUN npm -v
# 安装appium2.x
RUN npm i --location=global appium

#验证
RUN appium -v


#-- 第四步: 安装androidSdk、ADB
# 拷贝android tools到容器中
RUN mkdir -p /opt/app/android-sdk/cmdline-tools/latest
COPY cmdline-tools /opt/app/android-sdk/cmdline-tools/latest/
# 解压android tools
RUN cd /opt/app/android-sdk/cmdline-tools/latest/bin


RUN yes | /opt/app/android-sdk/cmdline-tools/latest/bin/sdkmanager --licenses && /opt/app/android-sdk/cmdline-tools/latest/bin/sdkmanager --no_https  --install "cmake;3.10.2.4988404"
RUN yes | /opt/app/android-sdk/cmdline-tools/latest/bin/sdkmanager --licenses && /opt/app/android-sdk/cmdline-tools/latest/bin/sdkmanager --no_https --install "build-tools;29.0.0"
RUN yes | /opt/app/android-sdk/cmdline-tools/latest/bin/sdkmanager --licenses && /opt/app/android-sdk/cmdline-tools/latest/bin/sdkmanager --no_https --install "platforms;android-29"
RUN yes | /opt/app/android-sdk/cmdline-tools/latest/bin/sdkmanager --licenses && /opt/app/android-sdk/cmdline-tools/latest/bin/sdkmanager --no_https --install "platform-tools"
RUN /opt/app/android-sdk/cmdline-tools/latest/bin/sdkmanager --no_https --list_installed --verbose
ENV ANDROID_HOME=/opt/app/android-sdk
ENV PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
RUN source /etc/profile
RUN ls -al /opt/app/android-sdk/platform-tools

RUN adb devices

#验证android 是否安装成功
RUN cd $ANDROID_HOME
RUN adb version
RUN ls $ANDROID_HOME/build-tools

#-- 第五步: 安装uiautomator2驱动
RUN appium driver install uiautomator2

# 验证驱动是否已安装
RUN appium driver list --installed




#-- 第六步:     安装 xcuitest 驱动
RUN appium driver install xcuitest
# 验证驱动是否已安装
RUN appium driver list --installed

三、镜像构建命令

执行命令

docker build --no-cache --progress=plain -t auto_base_image  . 

镜像构建帮助文档

[root@localhost dockerFile]# docker build --progress=plain -t auto_base_image  .  --help

Usage:  docker buildx build [OPTIONS] PATH | URL | -

Start a build

Aliases:
  docker buildx build, docker buildx b

Options:
      --add-host strings              Add a custom host-to-IP mapping (format: "host:ip")
      --allow strings                 Allow extra privileged entitlement (e.g., "network.host", "security.insecure")
      --attest stringArray            Attestation parameters (format: "type=sbom,generator=image")
      --build-arg stringArray         Set build-time variables
      --build-context stringArray     Additional build contexts (e.g., name=path)
      --builder string                Override the configured builder instance (default "default")
      --cache-from stringArray        External cache sources (e.g., "user/app:cache", "type=local,src=path/to/dir")
      --cache-to stringArray          Cache export destinations (e.g., "user/app:cache", "type=local,dest=path/to/dir")
      --cgroup-parent string          Optional parent cgroup for the container
  -f, --file string                   Name of the Dockerfile (default: "PATH/Dockerfile")
      --iidfile string                Write the image ID to the file
      --label stringArray             Set metadata for an image
      --load                          Shorthand for "--output=type=docker"
      --metadata-file string          Write build result metadata to the file
      --network string                Set the networking mode for the "RUN" instructions during build (default "default")
      #重新构建时是否需要缓存
      --no-cache                      Do not use cache when building the image
      --no-cache-filter stringArray   Do not cache specified stages
  -o, --output stringArray            Output destination (format: "type=local,dest=path")
      --platform stringArray          Set target platform for build
      #是否为标准输出
      --progress string               Set type of progress output ("auto", "plain", "tty"). Use plain to show container output (default "auto")
      --provenance string             Shorthand for "--attest=type=provenance"
      --pull                          Always attempt to pull all referenced images
      --push                          Shorthand for "--output=type=registry"
  -q, --quiet                         Suppress the build output and print image ID on success
      --sbom string                   Shorthand for "--attest=type=sbom"
      --secret stringArray            Secret to expose to the build (format: "id=mysecret[,src=/local/secret]")
      --shm-size bytes                Size of "/dev/shm"
      --ssh stringArray               SSH agent socket or keys to expose to the build (format: "default|<id>[=<socket>|<key>[,<key>]]")
  -t, --tag stringArray               Name and optionally a tag (format: "name:tag")
      --target string                 Set the target build stage to build
      --ulimit ulimit                 Ulimit options (default [])

编译难点:怎样获取build过程中的打印信息

运行docker build的时候如何获得标准输出以进行调试?
现在在运行docker build的时候很多标准输出被直接屏蔽了,如果出现错误可能并不会显示本该有的具体信息。

问题
[root@localhost dockerFile]# docker build -t auto_base_image  .
[+] Building 368.1s (17/28)                                                                                                               docker:default
 => [internal] load build definition from dockerfile                                                                                                0.3s
 => => transferring dockerfile: 1.92kB                                                                                                              0.0s
 => [internal] load .dockerignore                                                                                                                   0.3s
 => => transferring context: 2B                                                                                                                     0.0s
 => [internal] load metadata for docker.io/library/centos:7.9.2009                                                                                  0.0s
 => CACHED [ 1/24] FROM docker.io/library/centos:7.9.2009                                                                                           0.0s
 => [internal] load build context                                                                                                                  21.5s
 => => transferring context: 480.29MB                                                                                                              21.4s
 => [ 2/24] RUN yum install -y curl     && yum install -y wget     && yum install -y zip     && yum install -y unzip     && yum install -y tar    101.0s
 => [ 3/24] WORKDIR /usr/local/java                                                                                                                 0.2s
 => [ 4/24] ADD jdk-17_linux-x64_bin.tar.gz  /usr/local/java/                                                                                      12.1s 
 => [ 5/24] RUN yum install -y epel-release    && yum install -y nodejs    && yum install -y npm                                                   37.0s 
 => [ 6/24] RUN node -v                                                                                                                             0.7s 
 => [ 7/24] RUN npm -v                                                                                                                              1.0s 
 => [ 8/24] RUN npm i --location=global appium                                                                                                    202.2s 
 => [ 9/24] RUN appium -v                                                                                                                           2.6s 
 => [10/24] RUN mkdir -p /opt/app/android-sdk/cmdline-tools                                                                                         0.4s 
 => [11/24] COPY cmdline-tools /opt/app/android-sdk/cmdline-tools/                                                                                  7.8s 
 => [12/24] RUN cd /opt/app/android-sdk/cmdline-tools                                                                                               0.8s 
 => ERROR [13/24] RUN mv cmdline-tools latest                                                                                                       0.7s 
------                                                                                                                                                   
 > [13/24] RUN mv cmdline-tools latest:
0.655 mv: cannot stat 'cmdline-tools': No such file or directory
------
dockerfile:47
--------------------
  45 |     
  46 |     RUN cd /opt/app/android-sdk/cmdline-tools
  47 | >>> RUN mv cmdline-tools latest
  48 |     RUN cd /opt/app/android-sdk/cmdline-tools/latest
  49 |     
--------------------
ERROR: failed to solve: process "/bin/sh -c mv cmdline-tools latest" did not complete successfully: exit code: 1
[root@localhost dockerFile]# vi dockerfile 
解决方案

这是由于docker默认使用build auto功能,如果要获得标准输出,则需要加上 --progress=plain 选项

docker build --progress=plain -t 镜像名 .
[root@localhost dockerFile]# docker build --no-cache --progress=plain -t auto_base_image  . 
#0 building with "default" instance using docker driver

#1 [internal] load .dockerignore
#1 transferring context: 2B done
#1 DONE 0.0s

#2 [internal] load build definition from dockerfile
#2 transferring dockerfile: 2.37kB done
#2 DONE 0.0s

#3 [internal] load metadata for docker.io/library/centos:7.9.2009
#3 DONE 0.0s

#4 [ 1/29] FROM docker.io/library/centos:7.9.2009
#4 CACHED

#5 [internal] load build context
#5 transferring context: 81.65kB 0.0s done
#5 DONE 0.0s

#6 [ 2/29] RUN yum install -y curl     && yum install -y wget     && yum install -y zip     && yum install -y unzip     && yum install -y tar     && yum install -y lsof     && yum install -y git
#6 0.283 Loaded plugins: fastestmirror, ovl
#6 0.413 Determining fastest mirrors
#6 2.479  * base: mirrors.aliyun.com
#6 2.481  * extras: mirrors.aliyun.com
#6 2.481  * updates: mirrors.163.com
#6 394.0 Resolving Dependencies
#6 394.0 --> Running transaction check
#6 394.0 ---> Package curl.x86_64 0:7.29.0-59.el7 will be updated
#6 394.0 ---> Package curl.x86_64 0:7.29.0-59.el7_9.1 will be an update
#6 394.0 --> Processing Dependency: libcurl = 7.29.0-59.el7_9.1 for package: curl-7.29.0-59.el7_9.1.x86_64
#6 394.1 --> Running transaction check
#6 394.1 ---> Package libcurl.x86_64 0:7.29.0-59.el7 will be updated
#6 394.1 ---> Package libcurl.x86_64 0:7.29.0-59.el7_9.1 will be an update
#6 394.3 --> Finished Dependency Resolution
#6 394.3 
#6 394.3 Dependencies Resolved
#6 394.3 
#6 394.3 ================================================================================
#6 394.3  Package         Arch           Version                   Repository       Size
#6 394.3 ================================================================================
#6 394.3 Updating:

#6 420.7 Complete!
#6 DONE 421.0s

#7 [ 3/29] WORKDIR /usr/local/java
#7 DONE 0.0s

sdkManager 不能默认 accepte

问题
#20 [16/29] RUN /opt/app/android-sdk/cmdline-tools/latest/bin/sdkmanager --no_https --install "platforms;android-29"

#20 1.474 January 16, 2019
#20 1.474 ---------------------------------------
#20 1.474 Accept? (y/N): Skipping following packages as the license is not accepted:
#20 1.476 Android SDK Platform 29
#20 1.476 The following packages can not be installed since their licenses or those of the packages they depend on were not accepted:
#20 1.478   platforms;android-29
#20 1.478 [=======================================] 100% Computing updates...             
#20 1.478 
#20 DONE 1.5s
解决

执行时会显示如下信息,可自动接受license,不再需要手动输入y。

RUN yes | /opt/app/android-sdk/cmdline-tools/latest/bin/sdkmanager --licenses && /opt/app/android-sdk/cmdline-tools/latest/bin/sdkmanager --no_https --install "platforms;android-29"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值