nestjs Docker build error: can not find tsconfig.build.json - node.js

here is my Dockerfile:
FROM node AS builder
WORKDIR /app
COPY package*.json ./
COPY prisma ./prisma/
COPY tsconfig.build.json ./
COPY tsconfig.json ./
RUN npm install
COPY . .
RUN npm run build
FROM node
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package*.json ./
COPY --from=builder /app/dist ./dist
EXPOSE 3000
CMD [ "npm", "run", "start:dev" ]
and here is my docker-compose.yml:
version: "3.7"
services:
web:
image: Dockerfile
build:
context: ./
dockerfile: Dockerfile.development
volumes:
- ./:/app:z
environment:
NODE_ENV : development
TZ: "${TZ:-America/Los_Angeles}"
ports:
- "3000:3000"
After I run the docker-compose up -d, I can have the error from the console :
Error Could not find TypeScript configuration file "tsconfig.build.json". Please, ensure that you are running this command in the appropriate directory (inside Nest workspace).
I have tried to copy and paste tsconfig.build.json to the docker, but it still does not work.
please help.

In your last step, you never copy over the tsconfig.build.json file or the tsconfig.json. Though, I don't see why you're using start:dev when you've already built the server in the docker image. You should just be calling node dist/main

I don't use Docker, but I've got the same error:
Error Could not find TypeScript configuration file "tsconfig.build.json". Please, ensure that you are running this command in the appropriate directory (inside Nest workspace).
I solved this by deleting the dist folder and npm run start:dev again.

Related

Trouble Dockerizing MEAN stack application

I have trouble dockerizing my MEAN stack application
My angular part (named storage):
FROM node:16.13.0-alpine as node
WORKDIR /usr/src/app
COPY package.json package-lock.json ./
RUN npm i -g #angular/cli
RUN npm i bootstrap
COPY . .
RUN npm run build
FROM nginx:1.18.0-alpine
COPY --from=node /usr/src/app/dist /usr/share/nginx/html
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
My node and express and database directory named backend
FROM node:16.13.0-alpine as node
WORKDIR /usr/src/app
COPY package.json package-lock.json ./
RUN npm i -g #angular/cli
RUN npm i bootstrap
COPY . .
RUN npm run build
FROM nginx:1.18.0-alpine
COPY --from=node /usr/src/app/dist /usr/share/nginx/html
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
And my docker-compose.yml file
version: '3'
services:
angular:
hostname: localhost
build: storage
ports:
- "4200:80"
express:
build: backend
ports:
- "4000:4000"
links:
- database
database:
image: mongo
ports:
- "27017:27017"
I would be very happy if you can check and my code if I am missing something because I making docker-image for the first time..
When I run docker-compose build I get this errors:

After adding volumes to docker-compose, changes are not being picked up for frontend files

So I have this working as expected with flask where I used...
volumes:
- ./api:/app
And any files that I change in the api are picked up by the running session. I'd like to do the same for the frontend code.
For node/nginx, I used the below configuration. The only way for the file changes to be picked up is if I rebuild. I'd like for file changes to be picked up as they do for python but a bit stuck on why similar set up is not working for src files. Anyone know why this might be happening?
local path structure
public\
src\
Dockerfile.client
docker--compose.yml
docker file...
FROM node:16-alpine as build-step
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json ./
COPY ./src ./src
COPY ./public ./public
RUN yarn install
RUN yarn build
FROM nginx:alpine
COPY --from=build-step /app/build /usr/share/nginx/html
COPY nginx/nginx.conf /etc/nginx/nginx.conf
docker-compose
client:
build:
context: .
dockerfile: Dockerfile.client
volumes:
- ./src:/src
restart: always
ports:
- "80:80"
depends_on:
- api
This is happening because you are building the application.
...
RUN yarn build
...
and them using your build folder:
FROM nginx:alpine
COPY --from=build-step /app/build /usr/share/nginx/html
I believe that what you are looking for is a live reload. You can find a good example here.
But basically what you need is a Dockerfile like this:
# Dockerfile
# Pull official Node.js image from Docker Hub
FROM node:12
# Create app directory
WORKDIR /usr/src/app
# Install dependencies
COPY package*.json ./
RUN npm install
# Bundle app source
COPY . .
# Expose container port 3000
EXPOSE 3000
# Run "start" script in package.json
CMD ["npm", "start"]
your npm start script:
"start": "nodemon -L server/index.js"
and your volume:
volumes:
- ./api:/usr/src/app/serve

How to dockerize with NX (react and node app)?

I have created a nx workspace with 2 react app (client/admin) et 1 server with node.
For each, I have created a DockerFile and at the root of the project I have my docker-compose.yml.
My issue : when I use the command docker-compose up --build, the 3 app runs but for the react-app, I have a blank page whereas with the command nx serve <name_app>, we can see the app. Here is my code :
Dockerfile
FROM node:14-alpine
WORKDIR /home/node/app
COPY ./apps/front-client/src .
COPY ./package.json package.json
RUN npm install --production
EXPOSE 3000
USER node
CMD ["npx", "serve", "-s", ".", "-l", "3000"]
docker-compose.yml
services:
client:
build:
context: .
dockerfile: ./apps/front-client/Dockerfile
ports:
- 3000:3000
volumes:
- ./apps/front-client/src:/app/src
...
The root div doesn't have anything inside. Do you know why ?
Your WORKDIR is set to /home/node/app, which means that the source is placed in /home/nome/app/src.
The npx server -s . -l 3000 is executed in the /home/nome/app/src.
When you mount the volume in docker-compose, you are mounting the application on /app/src, but there is no npx serving from this folder.
Align both folders. By an unwritten convention the proper place is /app/src so change the build file to be WORKDIR /app

NGINX fails at COPY --from=node /app/dist/comp-lib /usr/share/nginx/html,

COPY failed: stat /var/lib/docker/overlay2/1e9a0e53a11b406c13d4fc790336f37285927a1b87d1bac4d0e889c6d3cfed9b/merged/app/dist/comp-lib: no such file or directory
I tried running docker system prune, and restarted Docker a bunch of times. I also gave a shot at rm -rf /var/lib/docker in the docker VM, somehow that doesn't remove the directory.
Node version: v10.15.1
Docker version: 18.09.2, build 6247962
Dockerfile:
# stage-1
FROM node as builder
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
# stage -2
FROM nginx:alpine
COPY --from=node /app/dist/comp-lib /usr/share/nginx/html
I expect the build to be successful but the above mentioned is the error I'm experiencing.
In your stage 2
COPY --from=node /app/dist/comp-lib /usr/share/nginx/html
should be
COPY --from=builder /app/dist/comp-lib /usr/share/nginx/html
since stage 1 is called builder and not node.
This is the dockerfile that I use for my Angular apps:
FROM johnpapa/angular-cli as angular-built
WORKDIR /usr/src/app
COPY package.json package.json
RUN npm install --silent
COPY . .
RUN ng build --prod
FROM nginx:alpine
LABEL author="Preston Lamb"
COPY --from=angular-built /usr/src/app/dist /usr/share/nginx/html
EXPOSE 80 443
CMD [ "nginx", "-g", "daemon off;" ]
I've never had any issues with this configuration. There's more information as well in this article.

Run 2 different containers from same node project

I have a node project that has a web server and a service on the root.
--myNodeProj
--app.js //the web server
--service.js //an update service
In my package.json I have the following:
"scripts": {
"start": "node app.js",
"service": "node service.js"
},
For my DockerFile I have:
FROM node:8
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm install --only=production
# Bundle app source
COPY . .
EXPOSE 8080
CMD [ "npm", "start" ]
The CMD will run the app.js (webserver). How do I build another container with the service? Do I create another Dockerfile? Would the docker build command look different?
You can override the command -
docker run <image> node service.js
https://docs.docker.com/engine/reference/run/#general-form
I ended up using docker-compose.
You need to create a docker-compose.yml file with the following code:
version: '3'
services:
web:
# will build ./docker/web/Dockerfile
build:
context: .
dockerfile: ./docker/web/Dockerfile
ports:
- "3000:3000"
env_file:
- web.env
service:
# will build ./docker/service/Dockerfile
build:
context: .
dockerfile: ./docker/service/Dockerfile
env_file:
- service.env
This files reference 2 Dockerfiles that build the containers:
For service
FROM node:8
# Create app directory
WORKDIR /usr/src/service
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm install --only=production
# Bundle app source
COPY . .
CMD [ "node", "service.js" ]
For web:
FROM node:8
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm#5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm install --only=production
# Bundle app source
COPY . .
#EXPOSE 8080
CMD [ "npm", "start" ]
Notice that I can only do one NPM start. I call the service directly using node.
When I want to build containers, I issue the command:
docker-compose build

Resources