Docker unexpected behavior when using dockerfile to yum install a package inside container

32 Views Asked by At

Hi to make my question clear, I will use sever pictures here. First, I want to install rstudio server in my new docker image, the rpm file I downloaded beforehand,using

wget https://download2.rstudio.org/server/centos6/x86_64/rstudio-server-rhel-1.3.959-x86_64.rpm

here is my Dockerfile:

enter image description here

With Dockerfile and rserver rpm in current folder I want to build the image using :

docker build -t temp:temp .

However,sometimes the setup process is terminated due to unable to connect to the mirror site at the step yum install, sometimes it succeeded with some sites weird:

enter image description here

This success process mentions a mirror site:mirrors.163.com, however I never set mirror site as this, actually I didn't even know this site.

I checked my settings in three places: 1 my host machine's /etc/yum.repos.d/CentOS-Base.repo: enter image description here

2 The container run according to that build up image's /etc/yum.repos.d/CentOS-Base.repo:

enter image description here

3 I also checked my docker's /etc/docker/daemon.json:

enter image description here

So here is my question: 1 why the build image process sometimes failed and sometimes succeed? 2 why the building process used a mirror site I have never seen and never set before? Or it was set to some place I haven't been awared of?

Another background info is that since the host machine is in china,due to policy it can't freely access the website outside freely, that's why the mirror setting is so critical, sometimes it takes us several hours to figure out the correct mirror site setting.

3 So the last question is, is there a way to quickly setup the mirror set inside a country in order for the docker to run installation freely?

My host machine and docker image are all centos 7.4

1

There are 1 best solutions below

3
datawookie On

If you want to lock down the mirror being used to download packages then you need to modify /etc/yum.repos.d/CentOS-Base.repo on the image.

Create a local CentOS-Base.repo that specifies a working mirror. For example, http://mirror.centos.org/ (but you need to choose one that works reliably for you).

CentOS-Base.repo

[base]
name=CentOS-$releasever - Base
baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[updates]
name=CentOS-$releasever - Updates
baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-$releasever - Extras
baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[centosplus]
name=CentOS-$releasever - Plus
baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

Dockerfile

FROM rstudio/r-base:devel-centos7 AS base

WORKDIR /R_setup

COPY CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo

RUN wget -q https://download2.rstudio.org/server/centos7/x86_64/rstudio-server-rhel-2023.12.1-402-x86_64.rpm

RUN sed -i 's/enabled=1/enabled=0/' /etc/yum/pluginconf.d/fastestmirror.conf && \
    yum clean all && \
    yum makecache fast && \
    yum update && \
    yum install -y rstudio-server-rhel-2023.12.1-402-x86_64.rpm

CMD ["bash"]

In the Dockerfile you copy that configuration across into the correct location and it will then be used by subsequent yum commands. You also explicitly disabled the fastestmirror plugin.

FYI I have replaced the .rpm file that you are using by one that's directly downloaded during the Docker build. However, you can replace this with your original approach where you COPY across an .rpm. Personally I prefer to have the whole thing wrapped up in the Dockerfile.