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
Related
I have a issue.
I'm using docker to host my node apps while I'm studying.
It runs normally when build for first time, but, if I add a new package it not been installed in host.
So the question is, i have to run "docker-compose up --build" every time I install new package or change my package.json?
Here is an example of my Dockerfile and docker-compose.yml
Dockerfile
FROM node:alpine
WORKDIR /home/app_node
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 3333
CMD ["npm", "start"]
docker-compose.yml
version: "3"
services:
app:
build: .
image: testeeee
command: npm start
ports:
- "3333:3333"
volumes:
- .:/home/app_node
- /home/app_node/node_modules
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/
I am trying to dockerize nestjs application. I have to use approach of our devops, so I can't give all details of configuration.
Scripts in package.json typical for any nestjs application.
I have Dockerfile.backend:
FROM some.registry.net/docker/node16 as builder
WORKDIR /opt/app
COPY --chown=app:app ./nestjs/nest-project .
RUN yarn install --non-interactive --production --frozen-lockfile
FROM some.registry.net/docker/node16 as serve
WORKDIR /opt/app
ENV NODE_ENV=production
ENV APP_CONFIG=/opt/app/config/config.yaml
COPY --chown=app:app ./build/Procfile /opt/startup/Procfile
COPY --chown=app:app ./build/config.yaml ./config/config.yaml
COPY --chown=app:app --from=builder /opt/app/ ./
COPY --chown=app:app --from=builder /opt/app/node_modules ./node_modules
USER root
##RUN npm install pm2 -g
##RUN npm install -g nodemon
RUN npm run build
CMD ["/opt/startup/entrypoint.sh"]
And I have docker-compose.yml file:
version: "2"
services:
backend:
build:
context: .
dockerfile: ./build/Dockerfile.backend
command: npm run start
##volumes:
##- ./nestjs/nest-project:/app
##- /app/node_modules
ports:
- 4001:4001
- 9229:9229
environment:
- NODE_ENV=development
- PORT=4001
- REACT_APP_PROD=0
- REACT_APP_BACKEND_URL=http://127.0.0.1:4001
- FRONTEND_URL=http://localhost:4000
- APP_CONFIG=/opt/app/config/config.yaml
frontend:
build: ./frontend
command: npm start
##volumes:
##- ./frontend:/app
##- /app/node_modules
environment:
- NODE_ENV=development
- DISABLE_ESLINT_PLUGIN=true
- REACT_APP_BACKEND_URL=http://127.0.0.1:4001/backend
- PORT=4000
- REACT_APP_PROD=0
ports:
- 4000:4000
So the most interesting point lies in backend command part. I am able to start container only with npm run start:dev command. When I enter there npm start or npm run start:prod
container executes (I see in logs that nestjs app starts, successfully connects to database) and exits without any errors. I tried node dist/main and got the same result. I tried nodemon, with nodemon dist/main nodemon, even with verbose flag shows red line something like app crashed... and gives no more information. I tried pm2 with this command pm2 --name nestjs start npm -- start pm2 successfully starts and container exits without any information.
So far I see problem lies somewhere in my configuration, but I have no clue where to seek. Thanks in advance.
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
Between the following tutorials;
Dockerizing create-react-app
Developing microservices - Node, react & docker
I have been able to convert my nodejs app to dockerized micro-services which is up and running and connecting to services. However, my app uses Sqlite/Sequelize and this was working perfectly prior to dockerizing.
With the new setup, I get error;
/usr/src/app/node_modules/sequelize/lib/dialects/sqlite/connection-manager.js:31
throw new Error('Please install sqlite3 package manually');
Error: Please install sqlite3 package manually at new ConnectionManager
(/usr/src/app/node_modules/sequelize/lib/dialects/sqlite/connection-manager.js:31:15)
My question is;
Is it possible to use Sqlite3 with Docker
If so, anyone able to share sample docker-compose.yml and Dockerfile combo that works for this please.
My docker-compose.yml
version: '3.5'
services:
user-service:
container_name: user-service
build: ./services/user/
volumes:
- './services/user:/usr/src/app'
- './services/user/package.json:/usr/src/package.json'
ports:
- '9000:9000' # expose ports - HOST:CONTAINER
web-service:
container_name: web-service
build:
context: ./services/web
dockerfile: Dockerfile
volumes:
- './services/web:/usr/src/app'
- '/usr/src/app/node_modules'
ports:
- '3000:3000' # expose ports - HOST:CONTAINER
environment:
- NODE_ENV=development
depends_on:
- user-service
My user/ Dockerfile
FROM node:latest
# set working directory
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
# add `/usr/src/node_modules/.bin` to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH
# install and cache app dependencies
ADD package.json /usr/src/package.json
RUN npm install
# start app
CMD ["npm", "start"]
My web/ Dockerfile
FROM node:latest
# set working directory
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
# add `/usr/src/app/node_modules/.bin` to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY package.json /usr/src/app/package.json
RUN npm install
RUN npm install react-scripts#1.1.4
RUN npm install gulp -g
# start app
CMD ["npm", "start"]
Many thanks.
Got it. The issue was that my local node_modules were being copied to the host container. Hence in the sqlite3 lib/binding, node-v57-darwin-x64 was there instead of what is expected - node-v57-linux-x64. Hence the mess.
I updated the Dockerfiles and docker-compose.yml as follows:
My docker-compose.yml
services:
user-service:
container_name: user-service
build:
context: ./services/user/
dockerfile: Dockerfile
volumes:
- './services/user:/usr/src/app'
- '/usr/src/node_modules'
ports:
- '9000:9000' # expose ports - HOST:CONTAINER
web-service:
container_name: web-service
build:
context: ./services/web/
dockerfile: Dockerfile
volumes:
- './services/web:/usr/src/app'
- '/usr/src/app/node_modules'
ports:
- '3000:3000' # expose ports - HOST:CONTAINER
environment:
- NODE_ENV=development
depends_on:
- user-service
My user/ Dockerfile
FROM node:latest
# set working directory
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
# add `/usr/src/node_modules/.bin` to $PATH
ENV PATH /usr/src/node_modules/.bin:$PATH
# install and cache app dependencies
ADD package.json /usr/src/package.json
RUN npm install
# start app
CMD ["npm", "start"]
Helpful posts
Getting npm packages to be installed with docker-compose