I've been trying to setup a docker-compose for an nestjs application, mysql and redis for a while now. I already got the mysql, redis, and nestjs development containers to work fine. The issues come when I try to setup an additional container for nestjs in production, where I've been getting some problems along the way.
In a nutshell, the most common error that I've been getting is that npm is not been able to find the package.json in the current workspace, although I copied it before running the command that causes the error (which are either npm install or npm run build).
/docker-compose.yml
version: '3.8'
networks:
nesjs-network:
driver: bridge
services:
redis:
container_name: nestjs_redis
image: redis
environment:
- ALLOW_EMPTY_PASSWORD=yes
networks:
- nesjs-network
ports:
- '${FORWARD_REDIS_PORT:-5003}:6379'
dev:
container_name: nestjs_dev
image: nestjs-api-dev:1.0.0
build:
context: ./Docker
target: development
dockerfile: Dockerfile
command: npm run start:dev
ports:
- 3000:3000
- 9229:9229
networks:
- nesjs-network
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
restart: unless-stopped
env_file: '.env'
depends_on:
- database
- redis
links:
- database
- redis
prod:
container_name: nestjs_prod
image: nestjs-api-prod:1.0.0
build:
context: ./Docker
target: production
dockerfile: Dockerfile
# command: npm run start:prod
ports:
- 3000:3000
- 9229:9229
networks:
- nesjs-network
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
restart: unless-stopped
env_file: '.env'
depends_on:
- database
- redis
links:
- database
- redis
database:
build:
context: ./Docker
dockerfile: mysql8.Dockerfile
image: mysql/mysql-server:latest
container_name: database
restart: unless-stopped
tty: true
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
env_file: '.env'
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --authentication_policy=mysql_native_password --host_cache_size=0
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_NAME}'
MYSQL_USER: '${DB_USER}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
TZ: '${APP_TIMEZONE-America/New_York}'
networks:
- nesjs-network
volumes:
- dbdata:/var/lib/mysql:rw,delegated
#Volumes
volumes:
dbdata:
driver: local
/docker/DockerFile
###################
# BUILD FOR LOCAL DEVELOPMENT
###################
FROM node:18-alpine AS development
WORKDIR /usr/src/app
COPY package*.json ./
# RUN apk add --nocache udev ttf-freefont chromium git
RUN apk add udev ttf-freefont chromium git
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
ENV CHROMIUM_PATH /usr/bin/chromium-browser
RUN npm install -g [email protected]
RUN npm install glob rimraf
# RUN npm install --only=development
RUN npm ci
COPY . .
# Nest line needs to be tested
EXPOSE 3000
EXPOSE 9229
###################
# PRODUCTION
###################
FROM node:18-alpine as production
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /usr/src/app
COPY package*.json ./
RUN apk update \
&& apk add ca-certificates wget \
&& update-ca-certificates
# RUN apk add --nocache udev ttf-freefont chromium git
RUN apk add udev ttf-freefont chromium git
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD true
ENV CHROMIUM_PATH /usr/bin/chromium-browser
# RUN npm install -g [email protected]
# RUN npm install glob rimraf
# RUN npm install --only=production
RUN npm ci
COPY . .
EXPOSE 3000
EXPOSE 9229
COPY --from=development /usr/src/app/dist ./dist
# RUN npm run build
CMD ["node", "dist/main"]
No matter how many tweaks I add or change, I keep getting the same kind of errors. I also tried using --chown=node:node every time I copy files, and changing to the node user (USER node), but nothing changes.
The most common error I get:
=> ERROR [build 7/8] RUN pm run build
> [build 7/8] RUN nom run build:
#0 0.518 pm ERR! code ENOENT
#0 0.519 nom ERR! syscall open
#0 0.519 pm ERR! path /usr/src/app/package.json
#0 0.520 nom ERR!
errno
-2
#0 0.521 pm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
#0 0.521 pm ERR! enoent This is related to pm not being able to find a file.
#0 0.521 pm ERR! enoent
#0 0.522
#0 0.522 pm ERR! A complete log of this run can be found in:
#0 0.522 nom ERR!
/root/.npm/_logs/2022-10-15T19_02_39_449Z-debug-0.log
failed to solve: executor failed running [/bin/sh -c pm run build]: exit code: 254
Would anyone know what I might be doing wrong? All the containers work fine including the dev one for nestjs, but no luck with making the one for production.
CodePudding user response:
Just as David suggested in the question's comments, using ./Docker in the docker-compose build context was causing these path issues inside the Dockerfile:
prod:
container_name: nestjs_prod
image: nestjs-api-prod:1.0.0
build:
context: ./Docker
target: production
dockerfile: Dockerfile
Once I changed it to:
prod:
container_name: nestjs_prod
image: nestjs-api-prod:1.0.0
build:
context: .
target: production
dockerfile: docker/Dockerfile
the error didn't happen again and npm was able to find the package.json file with no issues!