plex-media-server-exporter icon indicating copy to clipboard operation
plex-media-server-exporter copied to clipboard

Optimized Dockerfile and Updated the Project

Open fabsau opened this issue 1 year ago • 0 comments

Hello,

This Pull Request contains changes for updating the project and optimizing the existing Dockerfile to dramatically reduce the final image size like #11 tried to do.

Here's a summary of what I've changed, followed by their explanations:

  1. Split the Dockerfile into two stages (build and runtime). Splitting the Dockerfile into two stages (BUILD and RUNTIME) drastically reduces the size of the final Docker image, as only the necessary dependencies are moved to the RUNTIME stage. This eliminates the redundancy caused by having the development and build tools present in the production-ready image.

  2. Updated the FROM image and Gemfile dependencies I updated the dependencies inside the Gemfile to use the latest versions and the ruby base image from the older version (3.0.5) to the newer 3.2.2-alpine version. The newer image is more up-to-date and the 'alpine' variant is much smaller.

  3. Used Alpine's apk to manage packages. Instead of apt-get, the Dockerfile now uses apk (available in the alpine variant) for package management. I also added the --no-cache flag which prevents the package manager from caching packages. This helps to keep the image lightweight.

  4. Added specific versions of build dependencies. With apk add --virtual .build-deps, only the necessary build dependencies are installed, and they are removed right after their use i.e., after the bundle install. This way, the build dependencies don't end up in the final Docker image, thus keeping the image clean and compact.

  5. Implemented Cleaning commands. After package installations, I added commands that delete cache files (rm -rf /usr/local/bundle/cache/*.gem) and resource-heavy unnecessary files from the gem directory.

  6. User and Group creation and permission management. Specifying a non-root user (here app) to run the server process is a good security practice, and this is now being implemented in the Dockerfile with addgroup and adduser commands. Furthermore, ownership of the $ROOT directory is handed over to the user and the group. Moreover, precise permissions (755) have been set to config.ru.

  7. Removed the MAINTAINER instruction. The MAINTAINER instruction is deprecated now as per Docker's official documentation. Hence, it has been removed.

  8. Included .dockerignore. I have added a .dockerignore file which helps to exclude unnecessary files and directories (like the example folder) to be included into the final Docker image. As a result, the build context that needs to be sent to the Docker daemon is smaller and an overall cleaner Docker image.

Please consider merging these changes. With this optimization, the total image size got reduced dramatically from 940 MB to 190 MB, a nearly 5x shrink in size.

Thank you for your time and hit me up if you need any clarification. I have tested the image and have not encountered any issues.

Best Regards

fabsau avatar Dec 22 '23 19:12 fabsau