plex-media-server-exporter
plex-media-server-exporter copied to clipboard
Optimized Dockerfile and Updated the Project
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:
-
Split the Dockerfile into two stages (build and runtime). Splitting the Dockerfile into two stages (
BUILD
andRUNTIME
) drastically reduces the size of the final Docker image, as only the necessary dependencies are moved to theRUNTIME
stage. This eliminates the redundancy caused by having the development and build tools present in the production-ready image. -
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 newer3.2.2-alpine
version. The newer image is more up-to-date and the 'alpine' variant is much smaller. -
Used Alpine's apk to manage packages. Instead of
apt-get
, the Dockerfile now usesapk
(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. -
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 thebundle install
. This way, the build dependencies don't end up in the final Docker image, thus keeping the image clean and compact. -
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. -
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 withaddgroup
andadduser
commands. Furthermore, ownership of the$ROOT
directory is handed over to the user and the group. Moreover, precise permissions (755) have been set toconfig.ru
. -
Removed the MAINTAINER instruction. The
MAINTAINER
instruction is deprecated now as per Docker's official documentation. Hence, it has been removed. -
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