A method, device, medium and equipment for applying Docker container in Android system
By customizing and optimizing the Docker container and the Android system, the problem of low operating efficiency of the containerized platform on the Android system is solved, the stable operation and efficient resource management of the Docker container are realized, and the cross-platform compatibility is improved.
Patent Information
- Application Number
- CN202410824027.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-25
- Publication Date
- 2024-09-17
- Estimated Expiration
- 2044-06-25
AI Technical Summary
Existing containerization platforms are difficult to run efficiently on the Android system platform, mainly due to poor compatibility with non-Linux environments and improper resource management.
Ensure the compatibility of the container with the Android system by customizing the Docker container and optimizing the Android system, including creating a customized Dockerfile, customizing daemon startup parameters, writing Seccomp configuration files, adjusting namespace configuration, configuring access permissions and activating kernel configuration sex and resource management.
It realizes the stable operation and efficient resource utilization of Docker containers on Android systems, improves cross-platform compatibility, and overcomes the operating obstacles of traditional Docker in non-Linux environments.
Smart Images

Figure CN118377585B
Abstract
Description
Technical Field
[0001] The present application belongs to the field of Android system development, and specifically relates to a method, device, medium and equipment for applying a Docker container in an Android system. Background Art
[0002] Traditionally, containerization platforms are widely known and applied in Linux desktop and server environments, such as Docker, Kubernetes, Podman, etc. However, the reason why these containerization platforms are mainly built on Linux systems is that these containerization platforms are built based on the namespace and control group features of the Linux kernel. These kernel-level functions allow these containerization platforms to implement isolation of processes, networks, file systems, user IDs and mount points, as well as resource restrictions, while these functions are not directly provided in non-Linux operating systems or are implemented differently.
[0003] Although the above containerization platforms have achieved great success in the Linux field, for a long time, extending their application to non-Linux environments, especially directly applying them to mobile operating systems such as Android, has faced certain technical difficulties. Summary of the invention
[0004] In view of the above problems, the purpose of this application is to provide a method, device, medium and equipment for applying Docker containers on the Android system. This application aims to solve the problem that the existing containerized platform is difficult to run efficiently on the Android system platform.
[0005] To achieve the above objectives, this application provides the following technical solutions:
[0006] A method for applying a Docker container in an Android system, the method comprising: customizing the Docker container; optimizing the Android system; and containerizing the optimized Android system based on the customized Docker container.
[0007] Optionally, the customization of the Docker container includes: creating a Dockerfile; customizing the daemon startup parameters of the Docker container to limit the use of Android system resources by the Docker container; writing a Seccomp configuration file for the Docker container to control the system calls that can be executed after the Docker container is applied to the Android system; and adjusting the namespace configuration of the Docker container to control the degree of isolation of the Docker container from resources within the Android system and the way it interacts with the Android system.
[0008] Optionally, the optimizing the Android system includes: configuring access rights of the Docker container; and activating a kernel on which the Docker container runs on the Android system.
[0009] Optionally, configuring access rights for the Docker container includes: opening the Android system root permission to the Docker container; mounting the Android system directory to the Docker container with root permission; and setting environment variables in the Android system to make the Docker container point to the mounted directory.
[0010] Optionally, activating the kernel on which the Docker container runs on the Android system includes: adjusting a kernel configuration file of the Android system; and compiling the adjusted kernel configuration file.
[0011] Optionally, containerizing the optimized Android system based on the customized Docker container includes: creating a Docker container image; and deploying the created Docker container image on the optimized Android system.
[0012] Optionally, before deploying the created Docker container image on the optimized Android system, the method further includes: performing basic function testing, resource and performance testing, and security testing on the created Docker container image.
[0013] In addition, to achieve the above-mentioned purpose, the present application also provides a device for applying a Docker container in an Android system, the device comprising: a customization module for customizing the Docker container; an optimization module for optimizing the Android system; and a containerization module for containerizing the optimized Android system based on the customized Docker container.
[0014] In addition, to achieve the above-mentioned purpose, the present application also provides a storage medium, wherein the storage medium stores computer-executable instructions, and the computer-executable instructions are used to execute any of the methods described above.
[0015] In addition, to achieve the above-mentioned purpose, the present application also provides an electronic device, which includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements any of the methods described above when executing the program.
[0016] Compared with the prior art, this application can bring the following beneficial effects: by using customized Docker containers, this application can better adapt to the kernel characteristics of the Android system, thereby overcoming the running obstacles of traditional Docker in non-Linux desktop or server environments, allowing more Linux containerized applications to run on the Android system, improving cross-platform compatibility. In addition, by optimizing Android, this application can more efficiently allocate and utilize the resources of the Android system, thereby enabling Docker containers to run stably on the Android system. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] Figure 1 This is a flowchart of a method for applying a Docker container in an Android system provided by an embodiment of the present application;
[0018] Figure 2 This is a structural diagram of a device for applying a Docker container in an Android system provided by another embodiment of the present application;
[0019] Figure 3 is a schematic diagram of the structure of a storage medium provided by another embodiment of the present application;
[0020] Figure 4 It is a structural schematic diagram of an electronic device provided in another embodiment of the present application. DETAILED DESCRIPTION
[0021] The specific embodiments of the present application will be described in detail below with reference to the accompanying drawings. Although the specific embodiments of the present application are shown in the accompanying drawings, it should be understood that the present application can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided in order to enable a more thorough understanding of the present application and to fully convey the scope of the present application to those skilled in the art.
[0022] It should be noted that certain words are used in the specification and claims to refer to specific components. Those skilled in the art should understand that technicians may use different nouns to refer to the same component. This specification and claims do not use the difference in nouns as a way to distinguish components, but use the difference in the functions of the components as the criterion for distinction. As mentioned throughout the specification and claims, "including" or "comprising" is an open term, so it should be interpreted as "including but not limited to". The subsequent description of the specification is a preferred embodiment of the implementation of the present application, but the description is based on the general principles of the specification and is not used to limit the scope of the present application. The scope of protection of the present application shall be determined by the attached claims.
[0023] To facilitate understanding of the embodiments of the present application, further explanation will be given below using specific embodiments as examples in conjunction with the accompanying drawings, and the various drawings do not constitute a limitation on the embodiments of the present application.
[0024] To solve the above problems, an embodiment of the present application provides a method for applying a Docker container in an Android system. The execution subject of the method is an electronic device, and the electronic device can be, for example, a computer or server with computing capabilities. Figure 1 According to an implementation method of the present application, a method for implementing a Docker container in an Android system is provided. Figure 1 As shown, the method may include the following steps:
[0025] S100: Customize Docker containers;
[0026] S200: Optimize the Android system;
[0027] S300: Containerize the optimized Android system based on a customized Docker container.
[0028] This embodiment customizes the Docker container platform to better adapt to the kernel characteristics of the Android system and overcome the operational barriers of the traditional container platform in non-Linux desktop or server environments. At the same time, by optimizing the Android system, it is possible to more efficiently allocate and utilize the resources of the Android system, and reduce the interference of system processes and services on containerized applications, thereby enabling the existing container platform to run stably on the Android system.
[0029] Specifically, in an exemplary embodiment, in step S100, customizing the Docker container includes:
[0030] S101: Create Dockerfile;
[0031] In this step, you need to create a file called Dockerfile in the root directory of the Android system. This file contains all the instructions for building a Docker container image. Specifically, you can create the Dockerfile file by performing the following steps:
[0032] 1. Select the base image;
[0033] First, you need to determine which base image the Docker image will be based on. The base image is an existing image on which you can add your own customizations. Common base images include Ubuntu, Alpine Linux, Debian, etc. The first line of the Dockerfile usually uses the FROM instruction to specify the base image, as shown below:
[0034] # Use the official Ubuntu as the base image
[0035] FROM ubuntu:latest
[0036] 2. Install software packages and dependencies;
[0037] You can use the RUN instruction to install the required packages and dependencies. This can be achieved by the following command:
[0038] # Update package lists and install software
[0039] RUN apt-get update&&\
[0040] apt-get install -y software-package&&\
[0041] rm -rf / var / lib / apt / lists / *
[0042] 3. Add files;
[0043] Local files can be copied to the image using the COPY or ADD commands, where COPY is used for ordinary files and ADD can also handle URLs and automatic decompression.
[0044] 4. Set the container startup command;
[0045] You can use the CMD or ENTRYPOINT instructions to specify the command to run when the container starts. The CMD instruction can be overridden by the command line parameters of docker run, while ENTRYPOINT is usually used for fixed commands and is not easily overridden. The following is an example of setting the container startup command:
[0046] # Set the container startup command
[0047] CMD [". / myapp"]
[0048] 5. Save the Dockerfile.
[0049] Save the above contents as a text file named Dockerfile (no file extension).
[0050] S102: Customize the daemon startup parameters of the Docker container in the Dockerfile to limit the use of Android system resources by the Docker container;
[0051] In this step, because devices running the Android system are more limited in resources (such as CPU, memory, and storage) than traditional servers or PCs, you can better control the use of Android system resources by customizing the Docker container's daemon startup parameters, such as limiting memory usage (--memory), CPU shares (--cpu-shares), and disk I / O bandwidth, to ensure that when the Docker container runs on the Android system, it does not consume excessive resources or run in an unexpected manner.
[0052] Specifically, the above purpose can be achieved by modifying the cgroup configuration used by the Docker container to customize the startup parameters of the Docker daemon (dockerd).
[0053] For example, the cgroup configuration can be modified in the following ways:
[0054] Json
[0055] {
[0056] "default-shm": "128M",
[0057] "live-restore": true,
[0058] "cgroup-parent": " / docker"
[0059] }
[0060] The default-shm parameter described here sets the default memory limit for the Docker container. Specifically, this parameter can be adjusted according to the actual available memory size of the Android system. The live-restore parameter ensures that the Docker container still follows these limits after the Docker daemon is restarted.
[0061] S103: Writing a Seccomp configuration file of the Docker container in the Dockerfile to control the system calls that can be executed after the Docker container is applied to the Android system;
[0062] In this step, the Docker container allows you to customize the Seccomp profile to limit the system calls made by the processes in the Docker container.
[0063] For example, writing a Seccomp configuration file requires the following steps:
[0064] 1. Create a custom Seccomp profile;
[0065] First, you need to create a custom Seccomp profile. This file is a JSON document that defines system call rules that are allowed, denied, or handled by default (the default is allowed). Docker provides a default Seccomp profile that can be modified based on the default file. The default profile is located in the Docker installation directory, for example, it may be located at / usr / share / docker / profiles / seccomp / default.json on some systems.
[0066] 2. Specify a custom Seccomp configuration for the Docker daemon.
[0067] Modify the startup parameters of the Docker daemon process to use the customized Seccomp configuration file above. This operation can be achieved by editing the Docker configuration file (such as / etc / docker / daemon.json) or directly specifying the parameters when starting the Docker daemon process.
[0068] Specifically, you can add or modify the following configuration in the daemon.json file:
[0069] {
[0070] "seccomp-profile": " / path / to / your / custom-seccomp-profile.json"
[0071] }
[0072] It should be noted that you need to make sure to replace / path / to / your / custom-seccomp-profile.json with the actual path of the custom configuration file above.
[0073] After modifying the configuration, you need to restart the Docker daemon for the changes to take effect. You can use the following command to restart the Docker service:
[0074] adb reboot
[0075] By writing Seccomp configuration files, you can enhance the security of Docker containers and limit potential malicious activities or misoperations by fine-grainedly controlling the system calls that processes in the container can execute, thereby reducing the risk of container compromise or the scope of impact.
[0076] For example, if the application in the Android system does not require a network connection, network-related system calls can be prohibited, so that even if there are vulnerabilities in the code in the container, network attacks cannot be launched or data leaks cannot be made.
[0077] Second, by limiting system calls that may lead to resource exhaustion (e.g., infinite loops, large number of file openings, etc.), the resources of the Android system and other containers can be protected from being affected by a single container.
[0078] S104: Adjust the namespace configuration of the Docker container in the Dockerfile to control the degree of isolation of the container from resources in the Android system and the interaction method with the Android system.
[0079] In this step, adjusting the namespace configuration of the Docker container mainly involves creating and running the container, which can be achieved through Docker command line parameters.
[0080] For the network namespace, you can execute the -net=none or --network=none command to make the container leave the default network namespace. In addition, you can also execute the --network=host command to make the container share the host's network namespace. In this case, the container no longer has independent network isolation.
[0081] For user namespaces, you can specify the namespace using the --userns flag. For example, use --userns=host to make the container use the host's user ID mapping.
[0082] For other namespaces, such as file system (mount namespace), IPC namespace, etc., Docker indirectly adjusts them by mounting volumes, sharing or private IPC, etc., which can be achieved through parameters such as -v, --ipc, --uts, etc.
[0083] By adjusting the namespace configuration of the Docker container, you can flexibly control the resource isolation level, security attributes, and interaction mode of the Docker container with the Android system. For example, you need to set up a dedicated network namespace for a container to isolate its network traffic, or adjust the USER namespace to implement stricter permission control. These adjustments are implemented through instructions in the Dockerfile (such as the --privileged flag), the Docker API, or by directly modifying the underlying runtime configuration, so as to deeply customize the container to meet specific application requirements or security policies.
[0084] In an exemplary embodiment, in step S200, optimizing the Android system includes:
[0085] S201: Configuring access rights for the Docker container;
[0086] In this embodiment, configuring the access rights of the Docker container is specifically achieved by modifying the source code of the Android system.
[0087] Below, this application takes the Android 12 system source code installed on the system-on-chip RK3588 launched by Rockchip as an example to explain in detail how to implement Android system source code modification.
[0088] The Android 12 system source code is divided into 3 modules, which are stored in 5 cpp files: / system / extras / su / su.cpp, system / core / libcutils / fs_config.cpp, frameworks / base / core / jni / com_android_internal_os_Zygote.cpp, packages / modules / adb / daemon / main.cpp and system / core / init / selinux.cpp. The source code saved in these 5 cpp files needs to be modified at the same time to enable the Docker application to run on the Android system. Before modifying the source code, it should be stated in advance that: due to the large number of source code lines in each file, and this modification only modifies a small part of the code segments, the following examples only illustrate the source code segments to be modified and the modified code segments. The complete source code and kernel configuration files can be obtained through the following address:
[0089] repoinit --repo-url=ssh: / / git@www.rockchip.com.cn:2222 / repo-release / tools / repo.git -u ssh: / / git@www.rockchip.com.cn:2222 / Android_S / rk3588-manifests.git -m Android12.xml
[0090] The following are specific instructions on how to modify the parts to be modified in the Android 12 system source code:
[0091] 1) Modify / system / extras / su / su.cpp:
[0092] An example of a source code segment saved by this file is as follows:
[0093] 1: int main(int argc,char**argv) {
[0094] 2: uid_tcurrent_uid = getuid();
[0095] 3: if (current_uid != AID_ROOT&¤t_uid != AID_SHELL) error(1,0,"not allowed");
[0096] / / Handle -h and --help.
[0097] 4: ++argv;
[0098] It should be noted that, in the source code segment shown above, 1 to 4 represent the number of code lines.
[0099] Specifically, modify the above source code segment in the following way:
[0100] Keep line 1 and comment out lines 2, 3, and 4.
[0101] The following is an example of the code segment modified based on the above modification method:
[0102] 1: int main(int argc, char** argv) {
[0103] / / uid_tcurrent_uid = getuid();
[0104] / / if (current_uid != AID_ROOT&¤t_uid != AID_SHELL) error(1, 0,"not allowed");
[0105] / / Handle -h and --help.
[0106] 4: ++argv;
[0107] In the above modification, by commenting out the 2nd, 3rd, and 4th lines of code, the following effect can be achieved:
[0108] ①Relax authority restrictions:
[0109] Line 2 in the original code gets the UID (user identifier) of the current user.
[0110] Line 3 checks whether the current user's UID is equal to AID_ROOT (usually representing the superuser root) or AID_SHELL (which may represent a shell user with specific permissions). If it is not equal to these two IDs, the error() function is called and the error code and message are passed, indicating that the current user does not have permission to run this program.
[0111] Commenting out these two lines of code means that the Docker container can obtain higher operating permissions for the Android system.
[0112] ②Retain references to parameter processing logic:
[0113] Although the fourth line of code ++argv; does not actually perform any parameter processing operations, the comment / / Handle -h and --help. above it suggests that there may be processing logic for command line parameters (such as -h or --help) in the future. Keeping this line of code and its comments indicates that this part of the code structure has not been changed, and the corresponding parameter parsing and help information output functions may be added in the future.
[0114] In summary, the above modifications remove the strict restrictions on user permissions, allowing any user to run this program, while retaining the reference to the command line parameter processing logic, leaving room for possible parameter parsing and help information output functions in the future.
[0115] 2) Modify system / core / libcutils / fs_config.cpp
[0116] The following is an example of the source code snippet saved by this file:
[0117] 1: {06755,AID_ROOT,AID_ROOT,0,"system / xbin / procmem"},
[0118] 2: {04750,AID_ROOT,AID_SHELL,0,"system / xbin / su"},
[0119] The above code represents permission setting data. Each set of data contains 5 elements separated by commas. Taking the first line of code as an example, these 5 elements are represented as follows:
[0120] Permission mode (mode): Indicates the file permissions, expressed in octal form. For example, 06755 means that the file owner, group users, and other users have read, write, and execute (rwx), read and execute (rx), and execute (x) permissions, respectively.
[0121] Owner User ID (owner UID): Indicates the owner of the file, such as AID_ROOT represents the super user (root).
[0122] Owner group ID (group GID): Indicates the user group to which the file belongs, such as AID_SHELL represents the shell user group with specific permissions.
[0123] Additional flags: This is 0 and is usually used to store additional attributes of a specific file system, such as SELinux security context.
[0124] File path: indicates the full path of a file in the file system, such as system / xbin / procmem and system / xbin / su.
[0125] Specifically, modify the above source code segment in the following way:
[0126] Change the parameter in the second line of code, that is, the permission mode, from 04750 to 06755.
[0127] The code example modified based on the above modification method is as follows:
[0128] 1: {06755,AID_ROOT,AID_ROOT,0,"system / xbin / procmem"},
[0129] 2: {06755,AID_ROOT,AID_SHELL,0,"system / xbin / su"},
[0130] In the original second line of code, the permission mode 04750 means that the owner (root) has read, write and execute permissions, and the same group users (AID_SHELL) and other users only have read and execute permissions. In the above modification, by changing the permission mode 04750 to 06755, the owner, the same group users and other users of the system / xbin / su file can have read, write and execute permissions, which means that more users (not only the owner and the same group users) can read, modify and execute operations on the file.
[0131] In summary, the above modifications enhance the access rights of the system / xbin / su file, especially allowing all users to execute the file. Among them, su is usually a command used to switch to another user (usually root), and granting broader execution permissions may lead to security risks unless there are specific security policies or constraints to control its use.
[0132] 3) Modify frameworks / base / core / jni / com_android_internal_os_Zygote.cpp
[0133] An example of a source code segment saved by this file is as follows:
[0134] 1: static void DropCapabilitiesBoundingSet(fail_fn_tfail_fn) {
[0135] 2: for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0)>= 0; i++) {;
[0136] 3: if (prctl(PR_CAPBSET_DROP, i, 0, 0, 0) == -1) {
[0137] 4: if (errno == EINVAL) {
[0138] 5: ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify "
[0139] 6:"your kernel is compiled with file capabilities support");
[0140] 7:} else {
[0141] 8: fail_fn(CREATE_ERROR("prctl(PR_CAPBSET_DROP, %d) failed: %s", i,strerror(errno)));
[0142] 9:}
[0143] 10:}
[0144] 11:}
[0145] 12:}
[0146] Specifically, modify the above source code segment in the following way:
[0147] Keep lines 1 and 12 and comment out lines 2 to 11.
[0148] The code example modified based on the above modification method is as follows:
[0149] 1: static void DropCapabilitiesBoundingSet(fail_fn_tfail_fn) {
[0150] / / for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0)>= 0; i++) {;
[0151] / / if (prctl(PR_CAPBSET_DROP, i, 0, 0, 0) == -1) {
[0152] / / if (errno == EINVAL) {
[0153] / / ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify "
[0154] / / "your kernel is compiled with file capabilities support");
[0155] / / } else {
[0156] / / fail_fn(CREATE_ERROR("prctl(PR_CAPBSET_DROP, %d) failed: %s", i,strerror(errno)));
[0157] / / }
[0158] / / }
[0159] / / }
[0160] 12:}
[0161] In the above modification, the reason why lines 2 to 11 are commented out is that:
[0162] ① This code segment attempts to remove the capabilities bounding set of the process by calling the prctl() function in a loop. These capabilities are usually used to control specific system-level operations that a process can perform under non-root privileges. Commenting out this code is to avoid security issues, system instability, or conflicts with other code logic caused by improper privilege adjustments.
[0163] ②As mentioned in the code comments above, if the prctl(PR_CAPBSET_DROP) call fails and returns an EINVAL error (invalid parameters), it may be because the kernel has not compiled in file capabilities support. Commenting out this code is because the current operating environment or the kernel of the target device does not support the required function, and it will be enabled when conditions are ripe (such as kernel update, configuration adjustment).
[0164] In summary, by commenting out lines 2 to 11 of code, the Docker container can be allowed to obtain root permissions from the Android system, thereby reducing the permission barriers when running the Docker container on the Android system, and allowing the Docker container to directly access the hardware resources in the Android system and modify the network configuration.
[0165] 4) Modify packages / modules / adb / daemon / main.cpp
[0166] An example of a source code segment saved by this file is as follows:
[0167] 1: if (should_drop_privileges()) {
[0168] 2: const bool should_drop_caps= !__android_log_is_debuggable();
[0169] Specifically, modify the above source code segment in the following way:
[0170] Change the second line of code from "const bool should_drop_caps = !__android_log_is_debuggable();" to:
[0171] "const bool should_drop_caps = false; / / !__android_log_is_debuggable();"
[0172] The code example modified based on the above modification method is as follows:
[0173] 1: if (should_drop_privileges()) {
[0174] 2: const bool should_drop_caps = false; / / !__android_log_is_debuggable();
[0175] The effects of the above modifications are:
[0176] ①Fix the value of should_drop_caps:
[0177] In the original code, the value of should_drop_caps depends on the return result of the __android_log_is_debuggable() function. If the system is in debug mode (debuggable), the function returns true and should_drop_caps is assigned to false; otherwise, the function returns false and should_drop_caps is assigned to true.
[0178] After the modification, the value of should_drop_caps is directly set to false, regardless of whether the system is in debug mode. This is equivalent to canceling the dependency on the result of the __android_log_is_debuggable() function call.
[0179] ②Annotate the original logic:
[0180] A comment / / !__android_log_is_debuggable(); is added to the right side of the assignment statement to preserve the trace of the original code logic. This helps other developers understand the original intent of the code here and restore the original logic when necessary.
[0181] In summary, the above changes make should_drop_caps always set to false regardless of whether the system is in debug mode or not, that is, the drop capabilities operation related to this variable is never performed. At the same time, the original logic is retained through comments to facilitate subsequent code maintenance and understanding. This modification is intended to temporarily disable specific functions, avoid problems in debug mode, or adjust code behavior to adapt to new requirements.
[0182] 5) Modify system / core / init / selinux.cpp
[0183] An example of a source code segment saved by this file is as follows:
[0184] 1: bool IsEnforcing() {
[0185] 2: if (ALLOW_PERMISSIVE_SELINUX) {
[0186] 3: return StatusFromProperty() == SELINUX_ENFORCING;
[0187] 4:}
[0188] 5: return true;
[0189] 6:}
[0190] Specifically, modify the above source code segment in the following way:
[0191] Add the following code after the first line of code:
[0192] if(true) return false;
[0193] The code example modified based on the above modification method is as follows:
[0194] 1: bool IsEnforcing() {
[0195] 2: if(true) return false;
[0196] 3: if (ALLOW_PERMISSIVE_SELINUX) {
[0197] 4: return StatusFromProperty() == SELINUX_ENFORCING;
[0198] 5:}
[0199] 6: return true;
[0200] 7:}
[0201] In the above modification, by adding new code after the first line of code, the purpose is to:
[0202] ①Force return false:
[0203] A new line if (true) return false; is added at the beginning of the function. Since true is always true, this code always executes return false;, causing the function to return false immediately and not continue to execute the subsequent logic.
[0204] ②Retain the original logic:
[0205] Although the newly added if statement forces the function to return false, the original logic (including the ALLOW_PERMISSIVE_SELINUX check and the call to StatusFromProperty()) is not deleted, but only commented out or the execution opportunity is removed, thus preserving the structure and intent of the original code and making it easy to restore the original functionality when needed.
[0206] In summary, the above modification makes the IsEnforcing() function always return false, that is, SELinux is always considered to be in a non-enforced (permissive) state. This modification is to temporarily disable the enforcement mode of SELinux for debugging, testing, or solving problems that conflict with SELinux policies. At the same time, by retaining the original logical structure, it is easy to restore the original function as needed later.
[0207] By modifying the five cpp files above, the Docker container can obtain the root privileges of the Android system.
[0208] Furthermore, after obtaining root privileges, you also need to mount the Android system directory into the Docker container so that the Docker container can access the Android SDK or other resources in the Android system. Specifically, you can use the -v flag to mount the corresponding directory into the container, for example:
[0209] docker run -v / your / host / android-sdk: / usr / lib / android-sdk
[0210] The above command means that when starting a new Docker container, the Android SDK directory located at / your / host / android-sdk on the Android system is mounted to the / usr / lib / android-sdk directory in the container. In this way, the processes in the container can access the Android SDK resources on the host machine.
[0211] Furthermore, after the directory is mounted, you also need to set the environment variables of the Android system so that the Docker container can point to the mounted directory. Specifically, this function can be achieved through the following command:
[0212] docker run -e ANDROID_SDK_ROOT= / usr / lib / android-sdk
[0213] This command means that when starting a new container, an environment variable ANDROID_SDK_ROOT is set, and its value is set to / usr / lib / android-sdk. This allows the Android development or build process inside the container to know where to find the Android SDK files and tools.
[0214] S202: Activate the kernel on which the Docker container runs on the Android system.
[0215] In this step, activating the kernel on which the Docker container runs on the Android system includes the following steps:
[0216] 1. Adjust the kernel configuration file of the Android system;
[0217] In this step, you first need to obtain the kernel configuration file used by the current Android system. This file (usually named .config) defines the functions that are enabled or disabled when the kernel is compiled. The purpose of obtaining the configuration file is to understand which functions the current kernel supports and to determine which functions need to be added or modified to support Docker. The address for obtaining the kernel configuration file is as described above. At the same time, before modifying the kernel configuration file, you need to modify the file to prevent rollback.
[0218] Secondly, use the make menuconfig command to enter the Android system's kernel configuration interface to interactively modify the configuration, ensuring that the following key functions are enabled:
[0219] Namespaces (such as NET, IPC, PID, UTS, USER, CGROUP, MOUNT, etc.)
[0220] Control Groups (CONFIG_CGROUP_* related options, especially cgroups related to CPU, memory, and devices)
[0221] Union File Systems (CONFIG_OVERLAYFS or CONFIG_AUFS, etc., one of the storage drivers used by Docker)
[0222] By modifying the kernel configuration file of the Android system, it can be ensured that the Android system kernel supports the normal operation of the Docker container.
[0223] 2. Compile the adjusted kernel configuration file;
[0224] In this step, compiling the adjusted kernel configuration file specifically refers to adding the following configuration file at the end of the original kernel configuration file:
[0225] # Support Docker start
[0226] CONFIG_NAMESPACES=y
[0227] CONFIG_NET_NS=y
[0228] CONFIG_PID_NS=y
[0229] CONFIG_IPC_NS=y
[0230] CONFIG_UTS_NS=y
[0231] CONFIG_CGROUPS=y
[0232] CONFIG_CGROUP_CPUACCT=y
[0233] CONFIG_CGROUP_DEVICE=y
[0234] CONFIG_CGROUP_FREEZER=y
[0235] CONFIG_CGROUP_SCHED=y
[0236] CONFIG_CPUSETS=y
[0237] CONFIG_MEMCG=y
[0238] CONFIG_KEYS=y
[0239] CONFIG_VETH=y
[0240] CONFIG_BRIDGE=y
[0241] CONFIG_BRIDGE_NETFILTER=y
[0242] CONFIG_IP_NF_FILTER=y
[0243] CONFIG_IP_NF_TARGET_MASQUERADE=y
[0244] CONFIG_NETFILTER_XT_MATCH_ADDRTYPE=y
[0245] CONFIG_NETFILTER_XT_MATCH_CONNTRACK
[0246] CONFIG_NETFILTER_XT_MATCH_IPVS=y
[0247] CONFIG_NETFILTER_XT_MARK=y
[0248] CONFIG_IP_NF_NAT=y
[0249] CONFIG_NF_NAT=y
[0250] CONFIG_POSIX_MQUEUE=y
[0251] CONFIG_NF_NAT_IPV4=y
[0252] CONFIG_NF_NAT_NEEDED=y
[0253] CONFIG_CGROUP_BPF=y
[0254] CONFIG_VT=y
[0255] # Support Docker end
[0256] The container operating environment of the existing Android 12 system is closed by default. If the above configuration file is not added to the original kernel configuration file, the kernel function of the Android 12 system will not be turned on, and the Docker application will not be started and run normally. Therefore, the above configuration is a necessary configuration for the operation of the Docker application.
[0257] It should be noted that after the compilation is complete, you also need to install the new kernel configuration file on the Android system and update the bootloader to use the new kernel.
[0258] In another embodiment, in step S300, containerizing the optimized Android system based on the customized Docker container includes:
[0259] S301: Create a Docker container image;
[0260] In this step, the Docker container image can be built through the aforementioned Dockerfile. Exemplarily, this embodiment creates a Docker container image by executing the following program:
[0261] # Use the official Ubuntu as the base image
[0262] FROM ubuntu:latest
[0263] # Update apt sources and install necessary system tools
[0264] RUN apt-get update&&\
[0265] apt-get install -y \
[0266] curl \
[0267] git \
[0268] openjdk-11-jdk \
[0269] unzip \
[0270] wget \
[0271] zlib1g-dev \
[0272] libncurses5 \
[0273] lib32stdc++6 \
[0274] lib32z1 \
[0275] libgl1-mesa-dev \
[0276] libx11-dev \
[0277] libpulse-dev \
[0278] libxcomposite-dev \
[0279] libasound2-dev \
[0280] libxi-dev \
[0281] libxrandr-dev \
[0282] libxtst-dev
[0283] # Set the working directory
[0284] WORKDIR / opt / android-sdk
[0285] # Download and install the Android SDK command line tools
[0286] RUN wget -q
[0287] https: / / dl.google.com / android / repository / commandlinetools-linux-_latest.zip&&\ unzip commandlinetools-linux-*.zip&&\
[0288] rm commandlinetools-linux-*.zip
[0289] # Add environment variables
[0290] ENV ANDROID_HOME= / opt / android-sdk
[0291] ENV PATH="${PATH}:${ANDROID_HOME} / cmdline-tools / latest / bin:${ANDROID_HOME} / platform-tools"
[0292] # Accept the license agreement
[0293] RUN yes | sdkmanager --licenses
[0294] # Install required Android SDK components, such as API Level and Build Tools
[0295] RUN sdkmanager "platform-tools" "platforms;android-31" "build-tools;31.0.0"
[0296] # You can install more components as needed, such as system images, additional API Levels, etc.
[0297] # The final working directory can be your project directory
[0298] WORKDIR / app
[0299] # Example: Run a command or script, such as starting an Android emulator (if necessary)
[0300] # CMD ["emulator", "-avd", "my_avd"]
[0301] In the above procedure, ubuntu:latest is first selected as the base image because Ubuntu widely supports various software packages and is suitable for installing the Android SDK.
[0302] Secondly, install development and build tools such as OpenJDK, Git, Unzip, and necessary library files to ensure that the Android SDK and build tools can run normally.
[0303] Next, download the Android SDK command line tools and unzip them to your working directory. Also set environment variables to point to the Android SDK path and accept all SDK license agreements.
[0304] Secondly, the platform tools, specified API Level, and Build Tools are installed through the sdkmanager command.
[0305] Finally, set the working directory to / app.
[0306] By creating a Docker container image, we can ensure that applications and their dependencies on the Android system are encapsulated in an independent operating environment and isolated from other parts, which helps to improve the security and stability of the Android system and prevent Docker containers from improperly accessing or consuming global resources of the Android system.
[0307] S302: Deploy the created container image on the optimized Android system.
[0308] In this step, after creating the Docker container image, you can deploy it on the Android system by performing the following steps:
[0309] 1. Create a vendor / Docker directory in the Android source code and place the downloaded Docker binary file in this directory;
[0310] 2. Create the Android compilation file docker.mk. The main function of this file is to fully deploy the Docker application to the Android system when compiling the Android source code.
[0311] The vendor / Docker / docker.mk file contains the following
[0312] LOCAL_PATH := vendor / Docker
[0313] PRODUCT_COPY_FILES += \
[0314] $(LOCAL_PATH) / ctr:system / xbin / ctr \
[0315] $(LOCAL_PATH) / runc:system / xbin / runc \
[0316] $(LOCAL_PATH) / docker:system / xbin / docker \
[0317] $(LOCAL_PATH) / dockerd:system / xbin / dockerd\
[0318] $(LOCAL_PATH) / containerd:system / xbin / containerd \
[0319] $(LOCAL_PATH) / docker-init:system / xbin / docker-init \
[0320] $(LOCAL_PATH) / docker-proxy:system / xbin / docker-proxy \
[0321] $(LOCAL_PATH) / containerd-shim-runc-v2:system / xbin / containerd-shim-runc-v2 \
[0322] 3. Add the following code to line 32 of the file device / rockchip / rk3588 / device.mk:
[0323] $(call inherit-product-if-exists, vendor / Docker.mk)
[0324] The source files are as follows:
[0325] #$(call inherit-product-if-exists, vendor / rockchip / common / npu / npu.mk)
[0326] #BOARD_SEPOLICY_DIRS += device / rockchip / rk3588 / sepolicy_vendor
[0327] # enable this for support f2fs with data partition
[0328] BOARD_USERDATAIMAGE_FILE_SYSTEM_TYPE := f2fs
[0329] The modified file is as follows:
[0330] #$(call inherit-product-if-exists, vendor / rockchip / common / npu / npu.mk)
[0331] $(call inherit-product-if-exists, vendor / Docker.mk)
[0332] #BOARD_SEPOLICY_DIRS += device / rockchip / rk3588 / sepolicy_vendor
[0333] # enable this for support f2fs with data partition
[0334] BOARD_USERDATAIMAGE_FILE_SYSTEM_TYPE := f2fs
[0335] 4. Create the init.docker.rc file in the / vendor / Docker directory as mentioned above to automatically start the Docker service configuration, and add the following code to the end of the vendor / Docker / docker.mk file:
[0336] $(LOCAL_PATH) / init.docker.rc:system / etc / init / init.docker.rc \
[0337] The contents of the vendor / Docker / docker.mk file are as follows:
[0338] LOCAL_PATH := vendor / aidlux / Docker
[0339] PRODUCT_COPY_FILES += \
[0340] $(LOCAL_PATH) / ctr:system / xbin / ctr \
[0341] $(LOCAL_PATH) / runc:system / xbin / runc
[0342] $(LOCAL_PATH) / docker:system / xbin / docker
[0343] $(LOCAL_PATH) / dockerd:system / xbin / dockerd
[0344] $(LOCAL_PATH) / containerd:system / xbin / containerd
[0345] $(LOCAL_PATH) / docker-init:system / xbin / docker-init$(LOCAL_PATH) / docker-proxy:system / xbin / docker-proxy$(LOCAL_PATH) / containerd-shim-runc-v2:system / xbin / containerd-shim-runc-v2
[0346] $(LOCAL_PATH) / init.docker.rc:system / etc / init / init.docker.rc
[0347] The content of vendor / Docker / init.docker.rc is as follows:
[0348] # auto start docker service
[0349] on property:sys.boot_completed=1
[0350] start docker
[0351] service docker / system / xbin / dockerd
[0352] user root
[0353] group root
[0354] oneshot
[0355] seclabel u:r:shell:s0
[0356] After completing the above Android system customization and development, execute the following command to run Docker:
[0357] adb shell
[0358] docker run hello-world
[0359] The following is a detailed description of the above commands:
[0360] 1. adb shell is a subcommand of Android Debug Bridge (ADB) that is used to start an interactive shell session with an Android device (such as a smartphone, tablet, or other Android-based device) connected to a computer. The specific explanation is as follows:
[0361] 1.1. adb is a cross-platform command line tool, which is part of the Android SDK and is used to communicate with Android devices. It allows developers to perform various operations on devices through USB or network connections in the development environment, such as installing applications, pushing files, viewing device status, debugging applications, etc.
[0362] 1.2. Shell is a subcommand of adb, which instructs ADB to start a shell environment on the connected Android device. This shell environment is actually provided by the Linux kernel running on the device, because the underlying Android system is based on Linux. Through adb shell, developers can send Linux shell commands directly to the device in the command line and receive the execution results returned by the device.
[0363] After executing adb shell, the terminal prompt will change to the shell prompt of the device (usually displayed as device name@device ID:directory$). At this time, you can enter Linux shell commands to operate the device, just like in the local Linux system. For example, you can use ls to list the files in the current directory, use cd to change the working directory, and use ps to view the running processes. To exit the shell session of the device, just enter the exit command.
[0364] 2. The command "ocker run hello-world" is a command for the Docker container engine, which is used to run an official sample image named hello-world. The specific explanation is as follows:
[0365] 2.1、Docker run is an important command in the Docker command line tool, which is used to start a new container. It accepts multiple parameters and options to specify the image to run, the configuration of the container (such as port mapping, environment variables, volume mounting, etc.), and the behavior of the container (such as running commands, exit policies, etc.).
[0366] 2.2. The hello-world is a simple example image provided by Docker. When running this image, Docker will pull the hello-world image from the local or Docker Hub (default image repository) (if it does not exist locally), then create a new container based on the image and execute predefined commands in the container. The hello-world image is a very basic example, and its main purpose is to verify whether the Docker environment is correctly installed and can run the container normally. When running this image, the container will print a message to confirm the correct operation of Docker, and then exit immediately.
[0367] In summary, the adb shell command is used to open an interactive shell session on an Android device so that you can directly operate the device in the command line; while the docker run hello-world command is used to run a simple sample container through Docker to verify the correctness of the Docker environment. These two commands serve two completely different fields: Android device management and Docker container management. However, in practice, it is unusual to try to run a Docker container (such as docker run hello-world) directly on an Android device through the adb shell command, because the Android system does not usually support Docker directly. To run a Docker container on an Android device, complex system-level modifications or special tools and methods are usually required, which is beyond the normal operation scope of ordinary users.
[0368] In another exemplary embodiment, before deploying the created Docker container image on the optimized Android system, the method further includes:
[0369] Perform basic function tests, resource and performance tests, and security tests on the created Docker container images.
[0370] In the following, this embodiment describes the above tests respectively.
[0371] 1. Basic function test:
[0372] In this step, first use the docker run command to start the container without any additional parameters to verify whether the base image can be successfully started. For example:
[0373] docker run -it your-android-image-name bash
[0374] After entering the container, check whether the Android SDK, Java environment, and necessary tools are installed successfully, and whether the environment variables are set correctly.
[0375] Second, try running some basic Android commands like sdkmanager --list to see installed packages, or avdmanager list avd to list virtual devices.
[0376] 2. Resource and performance testing;
[0377] In this step, you can monitor the CPU, memory, and disk usage of the container at runtime to ensure that they are within the expected range. You can view them in real time through the docker stats command. The following is an example of the docker stats command:
[0378] docker stats --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}" my_android_container
[0379] This command can display the resource usage statistics of the specified container in real time, including container name, CPU usage percentage, memory usage, network input and output, and disk I / O. The part after the --format option defines the output format to make it easier to read.
[0380] 3. Security testing.
[0381] In this step, a permission review is first required to check the permission configuration of the container to ensure that no unnecessary system resources are over-exposed.
[0382] Specifically, in security testing, permission review includes the following aspects:
[0383] 1) You can use the USER instruction to specify a non-root user running the process in the container to reduce potential risks.
[0384] 2) Use the --cap-drop flag to remove unnecessary system capabilities and only give the container the minimum set of permissions required to run.
[0385] 3) Check the port mapping configuration of the container to ensure that no unnecessary services are exposed to the outside world and that the network mode is used reasonably.
[0386] 4) Review the volumes mounted to the container to ensure that no sensitive data or system directories are inadvertently exposed.
[0387] Secondly, you need to perform a vulnerability scan, using Docker's security scanning function or a third-party tool to scan the image and find known security vulnerabilities.
[0388] Specifically, you can directly use the docker scan command to scan the image. The command example is as follows:
[0389] docker scan <your-image-name>
[0390] This command connects to the Docker Hub security scanning service, analyzes the software components in the image, and reports known vulnerabilities.
[0391] Additionally, you can scan images using any of the third-party tools including Clair (an open source container image security scanner that statically analyzes vulnerabilities in Docker images), Trivy (a fast and comprehensive vulnerability scanner for container images and file systems by Aqua Security), and Anchore (a complete container security and compliance management platform that provides detailed vulnerability scanning, software composition analysis, policy management, and more).
[0392] In another embodiment, if Figure 2 As shown, the present application also provides a device for applying a Docker container in an Android system, the device comprising:
[0393] A customization module 100, used to customize the Docker container;
[0394] An optimization module 200, used for optimizing the Android system;
[0395] The containerization module 300 is used to containerize the optimized Android system based on a customized Docker container.
[0396] In another exemplary embodiment, the customization module 100 includes:
[0397] Create a submodule for creating a Dockerfile;
[0398] A custom submodule, which is used to customize the daemon startup parameters of the Docker container in the Dockerfile to limit the use of Android system resources by the Docker container;
[0399] Writing submodules, used to write Seccomp configuration files of Docker containers in Dockerfile to control the system calls that can be executed after the Docker container is applied to the Android system;
[0400] The adjustment submodule is used to adjust the namespace configuration of the Docker container in the Dockerfile to control the degree of isolation of the Docker container from the resources in the Android system and the way of interaction with the Android system.
[0401] In another exemplary embodiment, the optimization module 200 includes:
[0402] A configuration submodule, used to configure access permissions for the Docker container;
[0403] The activation submodule is used to activate the kernel on which the Docker container runs on the Android system.
[0404] In another exemplary embodiment, the containerization module 300 includes:
[0405] Image creation submodule, used to create Docker container images;
[0406] The deployment submodule is used to deploy the created Docker container image on the optimized Android system.
[0407] In another exemplary embodiment, the present application further provides a storage medium, wherein the storage medium stores computer executable instructions, and the computer executable instructions are used to execute any of the above methods.
[0408] In this embodiment, for example, Figure 3 As shown, the storage medium is a CD 30 on which a computer program (ie, a program product) is stored. When the computer program is executed by a processor, each step recorded in the above method implementation will be implemented.
[0409] In addition, the storage medium may also include, but is not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other optical or magnetic storage media, which are not listed here one by one.
[0410] In another exemplary embodiment, Figure 4 As shown, the present application also provides an electronic device, wherein the electronic device 50 includes but is not limited to: one or more processors or processing units 501, a system memory 502, and a bus 503 connecting different system components (including the system memory 502 and the processing unit 501).
[0411] The electronic device 50 typically includes a variety of computer system readable media. These media can be any available media that can be accessed by the electronic device 50, including volatile and non-volatile media, removable and non-removable media.
[0412] The system memory 502 may include computer system readable media in the form of volatile memory, such as random access memory (RAM) 5021 and / or cache memory 5022. The electronic device 50 may further include other removable / non-removable, volatile / non-volatile computer system storage media. By way of example only, the ROM 5023 may be used to read and write non-removable, non-volatile magnetic media ( Figure 4 is not shown in the Figure 4 As shown in FIG. 5 , a disk drive for reading and writing a removable non-volatile disk (e.g., a "floppy disk") and an optical disk drive for reading and writing a removable non-volatile optical disk (e.g., a CD-ROM, a DVD-ROM, or other optical media) can be provided. In these cases, each drive can be connected to the bus 503 via one or more data medium interfaces. The system memory 502 may include at least one program product, which has a set (e.g., at least one) of program modules, which are configured to perform the functions of the various embodiments of the present application.
[0413] A program / utility 5025 having a set (at least one) of program modules 5024 may be stored, for example, in system memory 502, and such program modules 5024 include, but are not limited to: an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment. Program modules 5024 generally perform the functions and / or methods of the embodiments described herein.
[0414] The electronic device 50 may also communicate with one or more external devices 504 (e.g., a keyboard, a pointing device, a display, etc.). Such communication may be performed via an input / output (I / O) interface 505. Furthermore, the electronic device 50 may also communicate with one or more networks (e.g., a local area network (LAN), a wide area network (WAN), and / or a public network, such as the Internet) via a network adapter 506. Figure 4 As shown, the network adapter 506 communicates with other modules (such as the processing unit 501, etc.) of the electronic device 50 via the bus 503. It should be understood that although Figure 4 Not shown, other hardware and / or software modules may be used in conjunction with the electronic device 50 .
[0415] The processing unit 501 executes various functional applications and data processing by running the program stored in the system memory 502, for example, customizing the Docker container; optimizing the Android system; containerizing the optimized Android system based on the customized Docker container. The specific implementation method of each step is not repeated here. It should be noted that although several units / modules or sub-units / sub-modules of the concurrent file downloading device are mentioned in the above detailed description, this division is only exemplary and not mandatory. In fact, according to the implementation mode of the present application, the features and functions of two or more units / modules described above can be concretized in one unit / module. Conversely, the features and functions of one unit / module described above can be further divided into multiple units / modules to be concretized.
[0416] The above embodiments are only for illustrating the technical concept and features of the present application, and their purpose is to enable people familiar with the technology to understand the content of the present application and implement it accordingly, and they cannot be used to limit the protection scope of the present application. Any equivalent changes or modifications made according to the spirit of the present application should be included in the protection scope of the present application.
Claims
1. A method for applying a Docker container in an Android system, characterized in that: The method comprises: Customize the Docker container; The customization of the Docker container includes: Create a Dockerfile; Customize the Docker container's daemon startup parameters in the Dockerfile to limit the Docker container's use of Android system resources; specifically, customize the Docker daemon startup parameters by modifying the cgroup configuration used by the Docker container; Writing a Seccomp configuration file of a Docker container in a Dockerfile to control the system calls that can be executed after the Docker container is applied to the Android system; writing the Seccomp configuration file includes: creating a custom Seccomp configuration file; specifying the custom Seccomp configuration file to the Docker daemon process; Adjust the namespace configuration of the Docker container in the Dockerfile to control the degree of isolation of the Docker container from the resources in the Android system and the way it interacts with the Android system. Optimize the Android system; The optimization of the Android system includes: Configure access permissions for the Docker container; Activating the kernel on which the Docker container runs on the Android system; Configure the access permissions of the Docker container, including: Open Android system root permissions to Docker containers; Mount the Android system directory to the Docker container with root permissions; Setting environment variables in the Android system to point the Docker container to the mounted directory; Containerize the optimized Android system based on a customized Docker container.
2. The method according to claim 1, characterized in that: The activating the kernel on which the Docker container runs on the Android system includes: Adjust the kernel configuration file of the Android system; Compile the adjusted kernel configuration file.
3. The method according to claim 1, characterized in that The containerization of the optimized Android system based on the customized Docker container includes: Create a Docker container image; Deploy the created Docker container image on the optimized Android system.
4. The method according to claim 3, characterized in that Before deploying the created Docker container image on the optimized Android system, the method further includes: Perform basic function testing, resource and performance testing, and security testing on the created Docker container image.
5. A device for implementing the method for applying a Docker container in an Android system as claimed in claim 1, characterized in that: The device comprises: Customization module, used to customize Docker containers; Optimization module, used to optimize the Android system; The containerization module is used to containerize the optimized Android system based on a customized Docker container.
6. A storage medium, characterized in that: The storage medium stores computer executable instructions, and the computer executable instructions are used to execute the method according to any one of claims 1 to 4.
7. An electronic device, characterized in that: The electronic device comprises: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the method according to any one of claims 1 to 4 is implemented.
