Trouble Dockerizing MEAN stack application - node.js

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:

Related

Dev dependencies not getting installed in docker container

I am new to docker. I am trying to create a container for react and express and run both the containers on same network using docker compose.
Below is my dockerfile for frontend:
FROM node:alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm","run","start"]
Below is my dockerfile for backend
FROM node:alpine
WORKDIR /app
COPY package*.json ./
RUN NODE_ENV=development npm install
COPY . .
EXPOSE 5000
CMD ["npm","run","server"]
Below is my docker-compose.yml
version: '3'
services:
client:
build:
context: './frontend'
dockerfile: Dockerfile
ports:
- 3000:3000
container_name: react_cont
environment:
- WATCHPACK_POLLING=true
networks:
- mern
volumes:
- ./frontend:/app
depends_on:
- server
server:
build:
context: './backend'
dockerfile: Dockerfile
ports:
- 5000:5000
container_name: express_cont
networks:
- mern
volumes:
- ./backend:/app
networks:
mern:
react container is getting is created and running successfully but the express container is not getting created with an error
sh: nodemon: not found
I had installed nodemon as my dev dependency.
Any help is appreciated. Thanks in advance.
Answering my own Question.
I had installed nodemon globally in my machine but forgot to install it as a dependency for my current project.
Since nodemon was installed globally in my machine i was not getting any errors while i was trying to run my server.js using nodemon. nodemon server.js in scripts did not throw any error while i was in developing my project locally prior moving it to docker container.
But since neither my package.json had nodemon as my dependency and i had not installed it separately in my container nodemon did not get installed and it gave me error.
You can try to delete node_modules folder in your source code and add flag --production=false explicitly to the npm install command. I think it's caching problem.
You may need to install nodemon package globally in your Docker:
RUN NODE_ENV=development npm install && npm --global install nodemon

Wrong path when dockerizing a Vue Php

I am pretty new to Docker, so in order to train it a bit i would like to fully dockerize my app. App consists of 3 main folders:
Folder - backend - where i have php backend of my app
Folder - frontend - where i have vue frontend of my app
File docker-compose.yml - nothing to add i guess :D
Folder - docker -> where i have folders with Dockerfiles for each system -> vue,php,nginx
Dockerfile for nginx:
FROM nginx:1.17.8-alpine
# Copy the public directory
COPY . /app/
# Copy the nginx config file
COPY ./docker/nginx/nginx.conf /etc/nginx/conf.d/default.conf
Dockerfile for php:
FROM php:7.4.3-fpm-alpine3.11
RUN docker-php-ext-install pdo pdo_mysql
COPY ./sunday_league_backend/ /app
VOLUME ["/app"]
And Dockerfile for vue:
FROM node:14.5.0-alpine
WORKDIR /sunday_league_frontend
# Install VueCLI
RUN npm install -g #vue/cli
# Copy project files
COPY . sunday_league_frontend
# Install dependencies
RUN npm install
# Expose the app port
EXPOSE 8080
# Start the app
CMD ["npm", "run", "serve"]
The server and backend part of code are working great. However when i try to add frontend to it i`m getting errno error. It says that the path is wrong npm ERR! path /sunday_league_frontend/package.json
PS What i discovered by trying multiple configurations if i switch Dockerfile for vue like this:
FROM node:14.5.0-alpine
RUN mkdir -p /app
WORKDIR /app
# Install VueCLI
RUN npm install -g #vue/cli
# Copy project files
COPY . /app/
# Install dependencies
RUN npm install
# Expose the app port
EXPOSE 8080
# Start the app
CMD ["npm", "run", "serve"]
And move all files from frontend file into main app folder, so it would be on same level as Folders for backend, docker and docker-compose.yml it will work just great. However it makes the architecture of files look terrible. What am i still missing :/
docker-compose file for better visualisation:
version: '3'
services:
web:
build:
context: .
dockerfile: docker/nginx/Dockerfile
ports:
- '8080:80'
volumes:
- .:/app/
links:
- php
- vue
php:
build:
context: .
dockerfile: docker/php/Dockerfile
volumes:
- .:/app/
vue:
build:
context: .
dockerfile: docker/vue/Dockerfile
ports:
- '8081:8080'
volumes:
- .:/app/

Unable to enable hot reload for React on docker

I'm new to docker so I'm sure I'm missing something.
I'm trying to create a container with a react app. I'm using docker on a Windows 10 machine.
This is my docker file
FROM node:latest
EXPOSE 3000
ENV PATH /app/node_modules/.bin:$PATH
# install app dependencies
COPY package.json ./
COPY package-lock.json ./
RUN npm install --silent
RUN npm install react-scripts#3.4.1 -g --silent
COPY . /app
WORKDIR /app
CMD ["npm","run", "start"]
and this is my docker compose
version: '3.7'
services:
sample:
container_name: prova-react1
build:
context: .
dockerfile: Dockerfile
volumes:
- '.:/app'
- '/app/node_modules'
ports:
- 3000:3000
environment:
- CHOKIDAR_USEPOLLING=true
- COMPOSE_CONVERT_WINDOWS_PATHS=1
When i start the container if I go on the browser everything is working fine but when i go back to Visual Studio Code and I make a modification to the files and save nothing occurs to the container and even to the website

Add .env.example in dist folder during docker compose up

I want to generate an .env and and an .env.example file and push it to the docker image during the docker build with Dockerfile.
The problem is that the .env.example file is not copied to the docker image. I think it works with the normal env, because I copy the env.production and save it as an env.
However, I am using node.js and typescript. Most people know that there is a way to use env viriables in the code by generating an env.d.ts and env.example.
During docker compose i am getting unfortunately that error.
no such file or directory, open '.env.example'
apparently the env.example is not copied to the dist folder. I need this one though.
Dockerfile
# stage 1 building the code
FROM node as builder
WORKDIR /usr/app
COPY package*.json ./
RUN npm install
COPY . .
COPY .env.production .env
RUN npm run build
COPY .env.example ./dist
# stage 2
FROM node
WORKDIR /usr/app
COPY package*.json ./
RUN npm install --production
COPY --from=builder /usr/app/dist ./dist
COPY ormconfig.docker.json ./ormconfig.json
EXPOSE 4000
CMD node dist/index.js
.dockerignore
node_modules
docker-compose.yaml
version: "3.7"
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
volumes:
- ./pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"
web:
build:
context: ./
dockerfile: ./Dockerfile
depends_on:
- db
ports:
- "4000:4000"
volumes:
- ./src:/src
Could somebody please help me !

nestjs Docker build error: can not find tsconfig.build.json

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.

Resources