Next.js build in Dockerfile failing because of missing .next/cache folder - docker

I'm experiencing an strange situation where the same Dockerfile and CodeBuild project config is failing prod but not in dev.
FROM public.ecr.aws/docker/library/node:14-alpine AS deps
WORKDIR /app
COPY package*.json yarn.lock ./
RUN yarn install --production=false --pure-lockfile --ignore-engines
FROM public.ecr.aws/docker/library/node:14-alpine AS builder
WORKDIR /app
RUN apk add --no-cache libc6-compat curl
COPY . .
COPY --from=deps /app/package.json .
COPY --from=deps /app/node_modules ./node_modules
RUN DEBUG=graphql:errors,graphql:queries,cache:redis DEBUG_COLORS=true npm run build --debug
RUN addgroup --system --gid 1001 app_user \
&& adduser --system --uid 1001 --shell /bin/bash -D app_user \
&& chown -R app_user:app_user /app \
&& echo "export PATH=$PATH:/usr/bin/curl" >> /etc/profile
USER app_user
HEALTHCHECK --interval=30s --timeout=5s --retries=3 CMD curl -f http://localhost:8080/blog || exit 1
EXPOSE 8080 6379
ENTRYPOINT [ "npm", "start"]
I'm getting the error: [Error: ENOENT: no such file or directory, stat '/app/.next/cache']
In detail this is the error output:
Build error occurred
[Error: ENOENT: no such file or directory, stat '/app/.next/cache'] {
errno: -2,
code: 'ENOENT',
syscall: 'stat',
path: '/app/.next/cache'
}
npm ERR! code ELIFECYCLE
I've thought about permissions issue, but by the time build command runs, root user is still in action.

By default, if a directory isn't present, Docker doesn't create it automatically. So you need to create it and give app_user write permission to it.
# COPY commands
RUN mkdir -p .next/cache && chown -R app_user .next/cache && chmod -R 644 .next/cache
# Debug command

Related

How to yarn build on Dockerfile

I try to build a Docker image executing the code:
docker build . -t <YOUR_DOCKER_HUB_USERNAME>/my-nuxt-project
It is about a nuxt.js project but when I run the code I receive the following error:
Step 5/13 : RUN yarn build
---> Running in 4dd3684952ba
yarn run v1.22.19
$ nuxt build
ℹ Production build
ℹ Bundling for server and client side
ℹ Target: server
ℹ Using components loader to optimize imports
ℹ Discovered Components: .nuxt/components/readme.md
✔ Builder initialized
✔ Nuxt files generated
ℹ Compiling Client
node:internal/crypto/hash:71
this[kHandle] = new _Hash(algorithm, xofLen);
^
Error: error:0308010C:digital envelope routines::unsupported
at new Hash (node:internal/crypto/hash:71:19)
at Object.createHash (node:crypto:133:10)
at module.exports (/app/node_modules/webpack/lib/util/createHash.js:135:53)
at NormalModule._initBuildHash (/app/node_modules/webpack/lib/NormalModule.js:417:16)
at handleParseError (/app/node_modules/webpack/lib/NormalModule.js:471:10)
at /app/node_modules/webpack/lib/NormalModule.js:503:5
at /app/node_modules/webpack/lib/NormalModule.js:358:12
at /app/node_modules/loader-runner/lib/LoaderRunner.js:373:3
at iterateNormalLoaders (/app/node_modules/loader-runner/lib/LoaderRunner.js:214:10)
at Array.<anonymous> (/app/node_modules/loader-runner/lib/LoaderRunner.js:205:4)
at Storage.finished (/app/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:55:16)
at /app/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:91:9
at /app/node_modules/graceful-fs/graceful-fs.js:123:16
at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:68:3) {
opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],
library: 'digital envelope routines',
reason: 'unsupported',
code: 'ERR_OSSL_EVP_UNSUPPORTED'
}
Node.js v18.12.0
error Command failed with exit code 1.
This is the Dockerfile that I am using. I am following the nuxt documentation to build the image.
FROM node:lts as builder
WORKDIR /app
COPY . .
RUN yarn install \
--prefer-offline \
--frozen-lockfile \
--non-interactive \
--production=false
RUN yarn build
RUN rm -rf node_modules && \
NODE_ENV=production yarn install \
--prefer-offline \
--pure-lockfile \
--non-interactive \
--production=true
FROM node:lts
WORKDIR /app
COPY --from=builder /app .
ENV HOST 0.0.0.0
EXPOSE 3000
CMD [ "yarn", "start" ]
Anyone knows how to debug it?
Try adding NODE_OPTIONS=--openssl-legacy-provider as docker environment variable. So your Dockerfile should be like this. Try rebuilding your Dockerfile after this change.
FROM node:lts as builder
WORKDIR /app
ENV NODE_OPTIONS=--openssl-legacy-provider
COPY . .
RUN yarn install \
--prefer-offline \
--frozen-lockfile \
--non-interactive \
--production=false
RUN yarn build
RUN rm -rf node_modules && \
NODE_ENV=production yarn install \
--prefer-offline \
--pure-lockfile \
--non-interactive \
--production=true
FROM node:lts
WORKDIR /app
COPY --from=builder /app .
ENV HOST 0.0.0.0
EXPOSE 3000
CMD [ "yarn", "start" ]

Permission denied when trying to run an executable in a Docker container

I'm trying to make an Express server with access to sqlPackage for DACPAC-deployments.
This is the final stage in my Docker build:
FROM node:16-alpine
WORKDIR /usr/app
ARG SQLPACKAGE_URL=https://download.microsoft.com/download/f/0/9/f091c731-45be-48fa-ae84-bc28388e3ef8/sqlpackage-linux-x64-en-16.0.6161.0.zip
# Install sqlPackage
RUN apk add --no-cache wget unzip
RUN wget -progress=bar:force -q -O sqlpackage.zip $SQLPACKAGE_URL \
&& unzip -qq sqlpackage.zip -d /usr/app/sqlpackage \
&& chmod 777 /usr/app/sqlpackage \
&& chown -R node.node /usr/app/sqlpackage
COPY --from=ts-remover /usr/app ./
USER node
CMD ["main.js"]
ARG PORT=8080
EXPOSE $PORT
My node project cannot run the executable though. I've tried running it manually from within the container, but I get permission denied there too.
/usr/app $ whoami
node
/usr/app $ ls -l
total 41272
drwxrwxrwx 2 node node 20480 Sep 12 14:12 sqlpackage
/usr/app $ ./sqlpackage
/bin/sh: ./sqlpackage: Permission denied
Why can I not execute this file, even though the permissions seem to be correct?

Error while building the Docker image: "chown: cannot access 'app/csv/': No such file or directory"

I am trying to create a Docker image for a python application and getting the following error
=> ERROR [7/9] RUN chown -R myuser:mygrp app/csv/ resources/datafeed.id 0.4s
------
> [7/9] RUN chown -R myuser:mygrp app/csv/ resources/datafeed.id:
#11 0.325 chown: cannot access 'app/csv/': No such file or directory
#11 0.325 chown: cannot access 'resources/datafeed.id': No such file or directory
------
executor failed running [/bin/sh -c chown -R myuser:mygrp app/csv/ resources/datafeed.id]: exit code: 1
My Dockerfile is as follows :
FROM python:3.8-slim-buster
WORKDIR app/
COPY requirements.txt .
RUN pip install -r requirements.txt
RUN groupadd -r -g 2000 mygrp && useradd -u 2000 -r -g mygrp myuser
RUN chown -R myuser:mygrp app/csv/ resources/datafeed.id
RUN chmod -R 700 app/csv/
COPY app .
USER myuser
CMD [ "python", "./main_async.py" ]
Also my project structure does have these files:
Project-folder/app/csv
Project-folder/resources/datafeed.id
You have your commands in the wrong order.
WORKDIR app/
...
RUN chown -R myuser:mygrp app/csv/ resources/datafeed.id
RUN chmod -R 700 app/csv/
COPY app .
The RUN <process> command will run whatever <process> is inside the container. In your current Dockerfile, you are chown-ing files you haven't copied yet inside the container.
Do the COPY first.
Make sure you are passing the correct arguments to COPY, which should generally be:
COPY <source on the host> <dest on the container>
So, if you are building from the Project-folder directory:
WORKDIR app
...
# Copy files from the host into the container.
# Since `WORKDIR` is already set, this copies
# all files in the current directory on
# host to under WORKDIR
COPY . .
RUN chown -R myuser:mygrp app/csv/ resources/datafeed.id
RUN chmod -R 700 app/csv/

sh: Syntax error: "&&" unexpected from chgrp -R 0 <app/csv> &&

I am trying to run following docker, but getting the error /bin/sh: 1: Syntax error: "&&" unexpected. Its coming from the last && when i try to run chgrp
Here is my docker file.
# The builder from node image
FROM node:14.5.0-buster as swi
RUN apt-get update && apt-get install
# Move our files into directory name "app"
WORKDIR /app
COPY package.json /app/
RUN cd /app && npm install
COPY . /app
RUN cd /app && npm run build
RUN chgrp -R 0 <app/csv> && \
chmod -R g=u <app/csv>
EXPOSE 5001
CMD [ "node", "server.js" ]
Tried removing && as \ means run next command but getting the same error
The angle brackets are wrong - you should just use the directory name as-is:
RUN chgrp -R 0 app/csv && \
chmod -R g=u app/csv
You should remove the angle brackets from the paths. Furthermore, since the WORKDIR is set to /app, we can just chgroup and chown the folder csv. I would also suggest to remove the linebreak and wirte it as one-liner:
RUN chgrp -R 0 csv && chmod -R g=u csv
We can simplify the docker file overall by using the fact that the WORKDIR is set to /app:
# The builder from node image
FROM node:14.5.0-buster as swi
RUN apt-get update && apt-get install
# Move our files into directory name "app"
WORKDIR /app
COPY package.json .
npm install
COPY . .
RUN npm run build
RUN chgrp -R 0 csv && chmod -R g=u csv
EXPOSE 5001
CMD [ "node", "server.js" ]

How to change permission of a folder to 777 in Dockerfile?

I have a project directory like this:
|-var/www
|-docker-compose.yml
|-app
|--uploads
|---photos
|-Dockerfile
This is my docker-compose.yml file:
myapp:
build:
context: myfolder
dockerfile: Dockerfile
container_name: flask
image: api/v1:01
restart: unless-stopped
environment:
APP_ENV: "prod"
APP_DEBUG: "False"
APP_PORT: 5000
volumes:
- appdata:/var/www
What I want:
I want to change app/uploads/photos this folder's permission to 777.This is an upload folder,so user can upload to this folder.
My Dockerfile now is look like this:
FROM python:3.6.8-alpine3.9
ENV GROUP_ID=1000 \
USER_ID=1000
WORKDIR /var/www/
ADD . /var/www/
RUN apk add --no-cache build-base libffi-dev openssl-dev ncurses-dev
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
RUN addgroup -g $GROUP_ID www
RUN adduser -D -u $USER_ID -G www www -s /bin/sh
USER www
EXPOSE 5000
After looking in this question,In order to achieve what I want,I tried below:
FROM python:3.6.8-alpine3.9
ENV GROUP_ID=1000 \
USER_ID=1000
WORKDIR /var/www/
ADD . /var/www/
RUN apk add --no-cache build-base libffi-dev openssl-dev ncurses-dev
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
RUN addgroup -g $GROUP_ID www
RUN adduser -D -u $USER_ID -G www www -s /bin/sh
RUN chown -R www:www /var/www
RUN chmod -R 777 /var/www/uploads
RUN chmod -R 777 /var/www/uploads/photos
USER www
EXPOSE 5000
But seems like the chmod command in my dockerfile is not taking effect.Cause whenever I upload some files to app/uploads/photos in my code,my nginx server keep getting this error:
PermissionError: [Errno 13] Permission denied: '/var/www/uploads/photos/myfilename.png'
Somebody please help.Please provide a solution for how to change the permission of a folder in Dockerfile.
UPDATE:
I tried to change the permission of /var/www/uploads after build the container and the container is running by doing below:
docker exec -it myapp /bin/sh
then run
chmod -R 777 /var/www/uploads
What I get is chmod: /var/www/uploads: Operation not permitted
Therefore I suspect the this error will also happened when the docker is building,then according to this answer from serverfault, I tried to modify the dockerfile to this:
FROM python:3.6.8-alpine3.9
ENV GROUP_ID=1000 \
USER_ID=1000
WORKDIR /var/www/
ADD . /var/www/
USER root
RUN chmod -R 777 /var/www/uploads
RUN addgroup -g $GROUP_ID www
RUN adduser -D -u $USER_ID -G www www -s /bin/sh
USER www
RUN apk add --no-cache build-base libffi-dev openssl-dev ncurses-dev
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
EXPOSE 5000
But it still doesnt work.Look like my above approach is also wrong.Cause I already run in root in the dockerfile.But at the same time,when I access the container in host using docker exec,also getting Operation not permitted.
I am very new in Docker.Just cant figure it out how to get this done.
What I hope to know:
1) How to change the folder var/www/uploads to permission 777?
2) What is the problem causing I cant change the permission from my approach?
3) What is the better way to achieve this? (If any)

Resources