strapi / strapi-docker

Install and run your first Strapi project using Docker
https://strapi.io
MIT License
1.17k stars 445 forks source link

Strapi base example image size too big with node_modules #223

Closed rams23 closed 4 years ago

rams23 commented 4 years ago

The size of the docker image created using the Dockerfile example of the strapi/base is greater than 1GB because it contains all the node_modules required. Is there a way to build the strapi backend and simply run using node a builded file (like a bundle tool output or so)? this will reduce drammatically the image size in production i think.

Step to reproduce:

alexandrebodin commented 4 years ago

Hello. You can create your own image for sure :) Closing as nothing prevents you from doing what you want :)

s-soroosh commented 3 years ago

@alexandrebodin It's true that nothing stops the users to do that, but still would be valuable to explain the best practices here in the strapi-docker repository and update the example it has with base images. It's a pity to see that following the example outlined in this repository creates such a big image. Otherwise one can say that the base image is too simple to deserve having a file in this repository :)

Phhofm commented 3 years ago

Maybe this might help someone to currently reduce the resulting image size so I post it here (I know its an old issue, but recently googeling to reduce the image size still brought me here, maybe it might help someone who got here too). My image size with strapi/base was 1.5 GB (it includes some sample data), and my currently produced image size is now 927 MB. When inspecting the new image with

$ docker run -it image sh
# du -hcs node_modules

the node_modules folder takes 637.7M total of those 927. It would be great if there were a way to reduce this folders size as mentioned by the original poster. To reduce the image size, I built on another base layer that was smaller than strapi/base. Instead of

FROM strapi/base

I used

FROM node:12-alpine
RUN apk add --no-cache build-base gcc autoconf automake zlib-dev libpng-dev nasm bash

It works and it reduced the resulting image size by almost 40%. I chose the node:12-alpine, because with the newer versions, something did not work quite right when building the image. But maybe someone else will find a better solution.

The full Dockerfile I use for reference:

# syntax=docker/dockerfile:1
FROM alpine AS cloner
RUN apk add --no-cache git
RUN git clone https://gitlab.com/rwf-dev-public/timeline.git

#FROM strapi/base - this is the old base layer (1.5G total), replaced by the next two statements (927M total)
FROM node:12-alpine
RUN apk add --no-cache build-base gcc autoconf automake zlib-dev libpng-dev nasm bash
COPY --from=cloner /timeline/backend /
RUN npm install
ENV NODE_ENV production
RUN npm run build
CMD ["npm", "start"]

Hope this helps someone :)

lphuctai commented 3 years ago

Hi, I want share my Dockerfile, run on stock Strapi v3.6.3.

Hope this can help you. ^^

FROM node:14-alpine as BUILD_IMAGE

WORKDIR /strapi

# Resolve node_modules for caching
COPY ./package.json ./
COPY ./yarn.lock ./
RUN yarn install --production=true --frozen-lockfile

# Copy all for build and release cache if package.json update
COPY . .
ENV NODE_ENV=production

RUN yarn build

#------------------------------------------------------------------------------------

# Create new namespace for final Docker Image
FROM node:14-alpine

# Only copy your source code without system file
COPY --from=BUILD_IMAGE /strapi /strapi

WORKDIR /strapi

EXPOSE 1337

ENV NODE_ENV=production
ENV STRAPI_LOG_LEVEL=debug

CMD ["yarn", "start"]

After compress, the image size about 150MB image