How to install the rpm package to the Docker image

foreword

When we build a mirror container, the tools that the container needs to use may not be included in the basic image. At this time, we need to manually add these software tools to the Docker image. Here we have sorted out these application scenarios in practice.

For example, when we perform performance tests, we often have many problems that need tools such as JPS and JSTACK to help analyze. If all applications are containerized services, and the openjdk that comes with centos is used, these tools may not be available. At this time, we need We introduce and install the openjdk-devel package.

Our application image depends on the construction and generation of the base image, and supplements software tools to the docker image. Here are two ways for reference:


Method 1, Dockerfile online installation

From centos-77-base:1.0
COPY demo.jar /root/app.jar
COPY run.sh /root
RUN cd /etc/yum.repos.d/
RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
# 前置操作:将宿主机的yum源文件拷到当前repo目录中,对镜像yum源进行替换
COPY ./repo/* /etc/yum.repos.d/
RUN yum -y install java-1.8.0-openjdk-devel.x86_64

ENTRYPOINT ["sh", "/root/run.sh"]

Note: This method mainly executes the yum install command in the container on line 8 to complete the installation. However, during the execution process, the yum install may fail due to the inaccessibility of the yum source in the container.
Whether the mirror yum source can be accessed normally, you can execute the following command in the container to test:

# 列出软件包详情列表
yum list
# 列出制定软件,可以过滤
yum list | grep xxx

If the yum source in the container cannot be accessed normally, but the yum source of the host machine can be accessed normally, perform the steps to replace the yum source file of the host machine into the container to complete the normal installation of the rpm software. For the replacement process, refer to lines 4-7 of the Dockerfile example.

ps.Docker container network mode generally adopts bridging mode. If the host machine can normally access the yum source, the container can also access the external network yum source relying on the host machine NAT address translation.

Method 2. Offline installation

If the source of the container yum is unavailable and the yum of the host machine cannot be used normally, we can also consider the way of offline installation at this time, that is, find a host that is normally used by yum, download the specified rpm installation package, and transfer it to the packer Offline installation via DOCKERFILE

Step1. Package download: Download the installation package on a host that can use yum normally

yumdownloader java-1.8.0-openjdk-devel.x86_64
# 加上参数的示例
# -destdir(下载的软件包存放路径)
# -resolve(解决依赖关系并下载所需的包)
yumdownloader --destdir=/tmp --resolve xxx

Step2. Transplant the rpm installation package to the image build host through the scp command
2.1 Copy to the remote server

# 复制文件
scp ./java-1.8.0-openjdk-devel.x86_64.rpm [email protected]:/home
# 复制目录的话,加上-r参数,示例如下:
scp -r tmp [email protected]:/home

2.2 Copy from remote server

# 从远程复制文件
scp [email protected]:/tmp/java-1.8.0-openjdk-devel.x86_64.rpm /home
# 从远端复制目录的话,加上-r参数,示例如下:
scp -r [email protected]:/tmp /home

Step3. Modify the Dockerfile to transplant the software package into the container and perform the installation operation

From centos-77-base:1.0
COPY demo.jar /root/app.jar
COPY run.sh /root
ADD java-1.8.0-openjdk-devel.x86_64.rpm /root
RUN cd /root && rpm -ivh java-1.8.0-openjdk-devel.x86_64.rpm --force --nodeps

ENTRYPOINT ["sh", "/root/run.sh"]

Summarize

This article briefly introduces two ways to install rpm software in the container image through yum. The offline method is generally not recommended. When downloading some complex rpm installation packages, their environmental dependencies are affected by the host, and the relevant dependent packages may be inaccurate. When installing offline in the container, there may be failures, and method 1 is generally given priority.

Guess you like

Origin blog.csdn.net/zhzh980/article/details/129332911