Reducing Strapi Docker Image Size

Don’t forget to add the .git directory to dockerignore. Since it is also copied into the image, with all the commits history, branches and etc. If you have a big repo with 10-20 branches of your project that can increase the image’s size a lot.

I don’t have a complete Dockerfile for alpine, since I’m using Debian, but they must be pretty similar. You can take a look at one of my docker files for google cloud by using debian, I’ve also left a few comments inside it:

#use debian image
FROM debian:10

#install all the dependencies that are need on debian to run strapi project with nginx and pm2.
RUN apt-get -y update
RUN apt-get install -y git ca-certificates nginx-full bash nano ssl-cert curl gzip zip python make gcc g++ wget gnupg2 dialog apt-utils
RUN curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install -y yarn
RUN apt-get install -y build-essential
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - \
    && apt-get install -y nodejs

#copy only the files that I need to run the project 
RUN mkdir www/
COPY api/ /www/api
COPY admin/ /www/admin
COPY config/ /www/config/
COPY extensions/ /www/extensions
COPY public/ /www/public
COPY docker/ /www/docker/
COPY plugins/ /www/plugins/
COPY components/ /www/components/
COPY package.json /www/
COPY ecosystem.config.js /www/
COPY /docker/nginx/nginx.conf /etc/nginx/

WORKDIR /www

#making the sh files executable, you can avoid this part.
RUN chmod +x ./docker/entrypoint.sh
RUN chmod +x ./docker/cloud_sql_proxy

RUN echo "unsafe-perm = true" >> ~/.npmrc

#install only the production packages, also reduces the size/time of install
RUN yarn install --only=production && yarn global add pm2

#building the admin panel for production 
RUN NODE_ENV=production yarn build

VOLUME /etc/www

EXPOSE 1337
EXPOSE 80
STOPSIGNAL SIGTERM

RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf

RUN make-ssl-cert generate-default-snakeoil --force-overwrite

#In entrypoint we start nginx/gcloud SQL proxy. And as the last step it runs the pm2 process with ecosystem config file configured for strapi.
#that part can also be avoided, you can use yarn start if you don't want to use pm2.
ENTRYPOINT /www/docker/entrypoint.sh
1 Like