jenkins-infra / docker-builder

A Docker image providing tools for... building Docker Images (usually in rootless or daemon-less contexts)
3 stars 10 forks source link

Optimize Jenkins Inbound Agent Dockerfile #263

Open Mayank77maruti opened 1 day ago

Mayank77maruti commented 1 day ago

What feature do you want to see added?

I've identified several opportunities to streamline the build process and improve overall performance. What Needs Improvement

Current Challenges

  1. Build process is slower than it could be
  2. Dockerfile has accumulated multiple separate RUN commands
  3. Package installations could be more efficient
  4. Some tool download methods are overly complicated

Proposed Solutions

  1. Consolidate package installations
  2. Optimize tool download processes
  3. Reduce overall image build complexity
  4. Improve layer caching

Upstream changes

Combine RUN Commands:-

Some RUN commands can be combined to reduce the number of layers:

dockerfileCopyRUN set -eux; \
    apt-get update; \
    LC_ALL=C DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        curl \
        git \
        make \
        build-essential \
        jq \
        zip \
        gpg \
        lsb-release \
        libssl-dev \
        libreadline-dev \
        zlib1g-dev \
        libyaml-dev \
        libncurses5-dev \
        libffi-dev \
        libgdbm-dev \
        # ... other packages ... \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

Use --mount=type=cache for apt-get We can use caching for apt-get to speed up builds:

dockerfileCopyRUN --mount=type=cache,target=/var/cache/apt \
    apt-get update \
    && apt-get install -y --no-install-recommends ...

Optimize ASDF Installation The ASDF installation could be more streamlined:

dockerfileCopyRUN git clone https://github.com/asdf-vm/asdf.git $HOME/.asdf --branch v${ASDF_VERSION} && \
    echo 'legacy_version_file = yes' > $HOME/.asdfrc && \
    . $HOME/.asdf/asdf.sh && \
    asdf plugin add ruby nodejs && \
    asdf install ruby 3.3.4 nodejs 18.20.4 && \
    asdf global ruby 3.3.4 nodejs 18.20.4

Use ARG for Home Directory Instead of hardcoding /home/jenkins, we can use an ARG:

dockerfileCopyARG HOME_DIR=/home/${USER}
ENV HOME=${HOME_DIR}
dduportal commented 23 hours ago

Hi @Mayank77maruti , thanks for raising this issue.

There are a lot of different things, i'll try to answer to each one on separate comments below.

My main concern is that most of the challenges you describe are either not challenges for the Jenkins infra platform OR looks really subjective.

Please be aware of the context: this image is not aimed to be consumed somewhere else than in the Jenkins Infra platform.

dduportal commented 23 hours ago

Use ARG for Home Directory Instead of hardcoding /home/jenkins, we can use an ARG:

This makes no sense to me. Could you explain (without any help of any kind of LLM) a little bit?

What is the problem it aims to solve exactly?

dduportal commented 23 hours ago

Optimize ASDF Installation The ASDF installation could be more streamlined:

Same: why would be the purpose of streamlining? What problem does it solve?

dduportal commented 23 hours ago

Use --mount=type=cache for apt-get We can use caching for apt-get to speed up builds:

This would provide a gain of... 3s on a 4 min build. We build in a US East Azure datacenter: we are close to APT mirrors. It would also be a great way to have package not up to date: how do you invalidate the cache? Finally, be aware that the CI agents are ephemeral so no local cache...

dduportal commented 22 hours ago

Some RUN commands can be combined to reduce the number of layers:

Why? There are good reasons why we separate the RUN instructions: