strapi / documentation

Strapi Documentation
https://docs.strapi.io
Other
1.02k stars 1.09k forks source link

Strapi Dockerfile and Build #2012

Closed djneely closed 7 months ago

djneely commented 7 months ago

Link to the documentation page or resource

https://docs.strapi.io/dev-docs/installation/docker

Describe the bug

I've copied and pasted the following Dockerfile to build for production but receiving errors when bulding

# Creating multi-stage build for production
FROM node:18-alpine as build
RUN apk update && apk add --no-cache build-base gcc autoconf automake zlib-dev libpng-dev vips-dev git > /dev/null 2>&1
ENV NODE_ENV=production

WORKDIR /opt/
COPY package.json yarn.lock ./
RUN yarn global add node-gyp
RUN yarn config set network-timeout 600000 -g && yarn install --production
ENV PATH /opt/node_modules/.bin:$PATH
WORKDIR /opt/app
COPY . .
RUN yarn build

# Creating final production image
FROM node:18-alpine
RUN apk add --no-cache vips-dev
ENV NODE_ENV=production
WORKDIR /opt/
COPY --from=build /opt/node_modules ./node_modules
WORKDIR /opt/app
COPY --from=build /opt/app ./
ENV PATH /opt/node_modules/.bin:$PATH

RUN chown -R node:node /opt/app
USER node
EXPOSE 1337
CMD ["yarn", "start"]

The issue seems to arise with the RUN yarn config set network-timeout 600000 -g && yarn install --production where I'm getting the following errors:

#15 217.4 error /opt/node_modules/sharp: Command failed.
#15 217.4 Exit code: 1
#15 217.4 Command: (node install/libvips && node install/dll-copy && prebuild-install) || (node install/can-compile && node-gyp rebuild && node install/dll-copy)
#15 217.4 Arguments: 
#15 217.4 Directory: /opt/node_modules/sharp
#15 217.4 Output:
#15 217.4 sharp: Detected globally-installed libvips v8.15.0
#15 217.4 sharp: Building from source via node-gyp
#15 217.4 gyp info it worked if it ends with ok
#15 217.4 gyp info using node-gyp@10.0.1
#15 217.4 gyp info using node@18.19.0 | linux | x64
#15 217.4 gyp info find Python using Python version 3.11.6 found at "/usr/bin/python3"
#15 217.4 
#15 217.4 gyp http GET https://unofficial-builds.nodejs.org/download/release/v18.19.0/node-v18.19.0-headers.tar.gz
#15 217.4 gyp http 200 https://unofficial-builds.nodejs.org/download/release/v18.19.0/node-v18.19.0-headers.tar.gz
#15 217.4 gyp http GET https://unofficial-builds.nodejs.org/download/release/v18.19.0/SHASUMS256.txt
#15 217.4 gyp http 200 https://unofficial-builds.nodejs.org/download/release/v18.19.0/SHASUMS256.txt
#15 217.4 gyp info spawn /usr/bin/python3
#15 217.4 gyp info spawn args [
#15 217.4 gyp info spawn args '/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py',
#15 217.4 gyp info spawn args 'binding.gyp',
#15 217.4 gyp info spawn args '-f',
#15 217.4 gyp info spawn args 'make',
#15 217.4 gyp info spawn args '-I',
#15 217.4 gyp info spawn args '/opt/node_modules/sharp/build/config.gypi',
#15 217.4 gyp info spawn args '-I',
#15 217.4 gyp info spawn args '/usr/local/lib/node_modules/npm/node_modules/node-gyp/addon.gypi',
#15 217.4 gyp info spawn args '-I',
#15 217.4 gyp info spawn args '/root/.cache/node-gyp/18.19.0/include/node/common.gypi',
#15 217.4 gyp info spawn args '-Dlibrary=shared_library',
#15 217.4 gyp info spawn args '-Dvisibility=default',
#15 217.4 gyp info spawn args '-Dnode_root_dir=/root/.cache/node-gyp/18.19.0',
#15 217.4 gyp info spawn args '-Dnode_gyp_dir=/usr/local/lib/node_modules/npm/node_modules/node-gyp',
#15 217.4 gyp info spawn args '-Dnode_lib_file=/root/.cache/node-gyp/18.19.0/<(target_arch)/node.lib',
#15 217.4 gyp info spawn args '-Dmodule_root_dir=/opt/node_modules/sharp',
#15 217.4 gyp info spawn args '-Dnode_engine=v8',
#15 217.4 gyp info spawn args '--depth=.',
#15 217.4 gyp info spawn args '--no-parallel',
#15 217.4 gyp info spawn args '--generator-output',
#15 217.4 gyp info spawn args 'build',
#15 217.4 gyp info spawn args '-Goutput_dir=.'
#15 217.4 gyp info spawn args ]
#15 217.4 gyp info spawn make
#15 217.4 gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
#15 217.4 make: Entering directory '/opt/node_modules/sharp/build'
#15 217.4   CC(target) Release/obj.target/nothing/../node-addon-api/nothing.o
#15 217.4 rm -f Release/obj.target/../node-addon-api/nothing.a Release/obj.target/../node-addon-api/nothing.a.ar-file-list; mkdir -p `dirname Release/obj.target/../node-addon-api/nothing.a`
#15 217.4 ar crs Release/obj.target/../node-addon-api/nothing.a @Release/obj.target/../node-addon-api/nothing.a.ar-file-list
#15 217.4   COPY Release/nothing.a
#15 217.4   TOUCH Release/obj.target/libvips-cpp.stamp
#15 217.4   CXX(target) Release/obj.target/sharp-linuxmusl-x64/src/common.o
#15 217.4   CXX(target) Release/obj.target/sharp-linuxmusl-x64/src/metadata.o
#15 217.4   CXX(target) Release/obj.target/sharp-linuxmusl-x64/src/stats.o
#15 217.4   CXX(target) Release/obj.target/sharp-linuxmusl-x64/src/operations.o
#15 217.4   CXX(target) Release/obj.target/sharp-linuxmusl-x64/src/pipeline.o
#15 217.4 ../src/pipeline.cc: In member function 'virtual void PipelineWorker::OnOK()':
#15 217.4 ../src/pipeline.cc:1230:30: error: aggregate 'PipelineWorker::OnOK()::stat64 st' has incomplete type and cannot be defined
#15 217.4  1230 |         struct STAT64_STRUCT st;
#15 217.4       |                              ^~
#15 217.4 ../src/pipeline.cc:1231:55: error: invalid use of incomplete type 'struct PipelineWorker::OnOK()::stat64'
#15 217.4  1231 |         if (STAT64_FUNCTION(baton->fileOut.data(), &st) == 0) {
#15 217.4       |                                                       ^
#15 217.4 ../src/pipeline.cc:33:23: note: forward declaration of 'struct PipelineWorker::OnOK()::stat64'
#15 217.4    33 | #define STAT64_STRUCT stat64
#15 217.4       |                       ^~~~~~
#15 217.4 ../src/pipeline.cc:1230:16: note: in expansion of macro 'STAT64_STRUCT'
#15 217.4  1230 |         struct STAT64_STRUCT st;
#15 217.4       |                ^~~~~~~~~~~~~
#15 217.4 make: *** [sharp-linuxmusl-x64.target.mk:167: Release/obj.target/sharp-linuxmusl-x64/src/pipeline.o] Error 1
#15 217.4 make: Leaving directory '/opt/node_modules/sharp/build'
#15 217.4 gyp ERR! build error 
#15 217.4 gyp ERR! stack Error: `make` failed with exit code: 2
#15 217.4 gyp ERR! stack at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:209:23)
#15 217.4 gyp ERR! System Linux 6.5.11-linuxkit
#15 217.4 gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
#15 217.4 gyp ERR! cwd /opt/node_modules/sharp
#15 217.4 gyp ERR! node -v v18.19.0
#15 217.4 gyp ERR! node-gyp -v v10.0.1
#15 217.4 gyp ERR! not ok
#15 ERROR: process "/bin/sh -c yarn config set network-timeout 600000 -g && yarn install --production" did not complete successfully: exit code: 1
------
 > [build 6/9] RUN yarn config set network-timeout 600000 -g && yarn install --production:
217.4 make: Leaving directory '/opt/node_modules/sharp/build'
217.4 gyp ERR! build error 
217.4 gyp ERR! stack Error: `make` failed with exit code: 2
217.4 gyp ERR! stack at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:209:23)
217.4 gyp ERR! System Linux 6.5.11-linuxkit
217.4 gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
217.4 gyp ERR! cwd /opt/node_modules/sharp
217.4 gyp ERR! node -v v18.19.0
217.4 gyp ERR! node-gyp -v v10.0.1
217.4 gyp ERR! not ok
------
Dockerfile-strapi.dev:29
--------------------
  27 |     COPY ./strapi-api/package.json ./strapi-api/yarn.lock ./
  28 |     RUN yarn global add node-gyp
  29 | >>> RUN yarn config set network-timeout 600000 -g && yarn install --production
  30 |     ENV PATH /opt/node_modules/.bin:$PATH
  31 |     WORKDIR /opt/app
--------------------
ERROR: failed to solve: process "/bin/sh -c yarn config set network-timeout 600000 -g && yarn install --production" did not complete successfully: exit code: 1

Additional context

No response

Suggested improvements or fixes

I'm trying to work through the errors being reported around the gyp or sharp module so that I can make it past the build phase.

Related issue(s)/PR(s)

No response

Eventyret commented 7 months ago

Nothing to do with the documentation rather I would suggest making sure you installed sharp and gyp and build tools for your machine etc that does the building. This depends on your OS and should be documented here https://github.com/nodejs/node-gyp once that is installed docker will use that to setup dockers build environment as well.

djneely commented 7 months ago

Using the same machine to build, this seems to work fine using node:18-bullseye which is what we've wound up going with for now instead of node:18-alpine?

Eventyret commented 7 months ago

So the reason that bullseye might work is that is the full version of debian and not alpine. With alpine you get a small footprint but also you need to add all dependencies etc as needed. I took the Dockerfile you had in this issue and copied into mine and it built fine. image

Did you add a .dockerignore file to prevent copying things over from the host machine which can cause the issue. https://docs.strapi.io/dev-docs/installation/docker#optional-dockerignore

djneely commented 7 months ago

I want to thank you a bunch. I didn't even realize the .dockerignore was not there anymore (I need to have a talk with one of the devs). Thank you again and apologies for the bother.