Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
fix!: optimize container layers and reduce image size
Browse files Browse the repository at this point in the history
Every individual RUN, COPY and ADD action creates an extra container layer, so there was plenty of room for improvement in our Containerfile.

This optimization gets rid of 4 useless layers from our final container image, and shrinks the final OCI download size as follows:

- Removing the "mkdir /tmp/scripts" layer. It's not necessary to manually create the target directory for the container copy action.

- Removing the manual "chmod +x" for the scripts, and putting that step inside "build.sh" instead.

- Removing the manual copying of "build.sh", by instead placing it at "scripts/build.sh" so that it's automatically copied together with all the other scripts in one layer instead.

- Removing the separate "chmod +x build.sh && run build script" step by merging it with the "cleanup temp files and then finalize the container" step, so that we don't create a pointless extra filesystem layer just for the build.sh script execution.

These changes also reduce the size of the final image, because we're cleaning up the image in the exact same step that we run the "build.sh". If we didn't combine these steps, we'd still be keeping a useless extra layer with all the /tmp/ and /var/ junk files that were left over after the build.

Most seriously, the "/var/cache" folder contained copies of ALL RPM FILES that build.sh installed via "rpm-ostree install". This meant that we were generating a very big layer with a lot of junk data that shipped in the final image.

Our build now only generates 7 layers (instead of 11), and users will have a much smaller OCI download since we aren't shipping the cached RPM "build leftovers" or temp files via useless extra layers anymore.
  • Loading branch information
Arcitec authored and xynydev committed May 20, 2023
1 parent 878ea2f commit e8b5be6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
22 changes: 8 additions & 14 deletions Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,18 @@ ARG RECIPE
# See issue #28 (https://github.com/ublue-os/startingpoint/issues/28).
COPY usr /usr

# Copy recipe.
# Copy the recipe that we're building.
COPY ${RECIPE} /usr/share/ublue-os/recipe.yml

# "yq" used in build.sh and the setup-flatpaks recipe to read the recipe.yml.
# "yq" used in build.sh and the "setup-flatpaks" just-action to read recipe.yml.
# Copied from the official container image since it's not available as an RPM.
COPY --from=docker.io/mikefarah/yq /usr/bin/yq /usr/bin/yq

# Copy scripts.
RUN mkdir /tmp/scripts
# Copy the build script and all custom scripts.
COPY scripts /tmp/scripts
RUN find /tmp/scripts -type f -exec chmod +x {} \;

# Copy and run the build script.
COPY build.sh /tmp/build.sh
RUN chmod +x /tmp/build.sh && /tmp/build.sh

# Clean up and finalize container build.
RUN rm -rf \
/tmp/* \
/var/* && \
ostree container commit
# Run the build script, then clean up temp files and finalize container build.
RUN chmod +x /tmp/scripts/build.sh && \
/tmp/scripts/build.sh && \
rm -rf /tmp/* /var/* && \
ostree container commit
3 changes: 3 additions & 0 deletions build.sh → scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ if [[ ${#repos[@]} -gt 0 ]]; then
echo "---"
fi

# Ensure that all script files are executable.
find /tmp/scripts -type f -exec chmod +x {} \;

# Run "pre" scripts.
run_scripts() {
script_mode="$1"
Expand Down

0 comments on commit e8b5be6

Please sign in to comment.