I'm trying to wrap my node express backend with Docker. However, when running my docker-compose file. I encountered error when running npm install
stage throughout the dockerfile, saying it doesnt found the package.json copied in my local directory to the directory inside my container. Below is my project directory structure. Trying to proxy my node express backend API using nginx. Any sources that can help what I'm trying to achieve much appreciated.
error log:
> [4/5] RUN npm install:
#8 0.697 npm ERR! code ENOENT
#8 0.697 npm ERR! syscall open
#8 0.697 npm ERR! path /app/package.json
#8 0.698 npm ERR! errno -2
#8 0.699 npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
project dir:
express-backend
├── docker
| ├── nginx
| | ├── nginx.conf
| | └── dockerfile
| └── dockerfile
├── src
| ├── model
| | └── model.ts
| ├── controller
| | └── controller.ts
| ├── middleware
| | └── middleware.ts
| └── server.ts
| .dockerignore
| .gitignore
| docker-compose.yml
| package-lock.json
| package.json
| tsconfig.json
docker/dockerfile:
FROM node:alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . ./
EXPOSE 5000
RUN npm run build
CMD ["node", "/app/build/server.js", "npm run start"]
docker/nginx/dockerfile
FROM nginx
COPY nginx.conf /etc/nginx/conf.d/default.conf
docker/nginx/nginx.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name nodeserver;
location / {
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:5000;
}
}
docker-compose.yml
version: "3.8"
services:
express:
container_name: express_container
build:
context: ./docker
environment:
ports:
- "5000:5000"
nginx:
container_name: nginx_container
restart: always
build:
context: ./docker/nginx
ports:
- "80:80"
postgres:
container_name: postgres_container
image: postgres
restart: always
environment:
ports:
- "5432:5432"
pgadmin:
container_name: pgadmin_container
image: dpage/pgadmin4
restart: always
environment:
ports:
- "5050:80"
CodePudding user response:
You've set the context:
context: ./docker
So when you run
COPY . ./
You are copying files from that context, which doesn't include the package.json
. You could specify the Dockerfile:
express:
container_name: express_container
build:
dockerfile: ./docker/Dockerfile
environment:
ports:
- "5000:5000"
But I'd suggest restructuring the folders to keep each image separate where that makes sense.