I'm still pretty new to docker, but I finally understood the differences between docker-compose and plain docker command, I would like to deploy some containers for my dev environment which includes a backend with nest.js and a postgress database (Further down the line I will include a React App and maybe PGAdmin).
The image for my backend server works fine when I run it with docker run
but it looks like it has trouble with docker-compose
where some dependencies are missing (even though they are being installed with npm install
?).
Here's the docker file for my backend server.
FROM node:14-alpine
WORKDIR /usr/src/app
COPY . .
# If I don't install nest's cli the app won't start telling me nest was not found.
RUN npm install -g @nestjs/cli
# Install all dependencies
RUN npm install
# package.json start script
CMD [ "npm", "run", "start:dev"]
And this is my docker-compose
file.
version: '3.7'
services:
backend:
container_name: nest_backend
build: .
volumes:
- .:/usr/src/app
- /usr/src/app/node_module
ports:
- ${PORT}:${PORT}
command: npm run start:dev
env_file:
- .env
networks:
- webnet
depends_on:
- postgres
postgres:
container_name: postgres
image: postgres:12
networks:
- webnet
environment:
POSTGRES_PASSWORD: ${RDS_PASSWORD}
POSTGRES_USER: ${RDS_USERNAME}
POSTGRES_DB: ${RDS_DB_NAME}
PG_DATA: /var/lib/postgresql/data
volumes:
- pgdata:/var/lib/postgresql/data
ports:
- 5433:5432
networks:
webnet:
volumes:
pgdata:
This is the output when I check the logs on the container for my backend image.
11:38:22 PM] Starting compilation in watch mode...
node_modules/@types/react-dom/index.d.ts:27:8 - error TS2307: Cannot find module 'react' or its corresponding type declarations.
27 } from 'react';
~~~~~~~
src/app.module.ts:1:24 - error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
1 import { Module } from '@nestjs/common';
~~~~~~~~~~~~~~~~
src/app.module.ts:3:31 - error TS2307: Cannot find module '@nestjs/typeorm' or its corresponding type declarations.
3 import { TypeOrmModule } from '@nestjs/typeorm';
~~~~~~~~~~~~~~~~~
src/app.module.ts:6:30 - error TS2307: Cannot find module '@nestjs/config' or its corresponding type declarations.
6 import { ConfigModule } from '@nestjs/config';
~~~~~~~~~~~~~~~~
src/app.module.ts:10:26 - error TS2307: Cannot find module '@ntegral/nestjs-s3' or its corresponding type declarations.
10 import { S3Module } from '@ntegral/nestjs-s3';
My project is using a monorepo with yarn workspaces, could this be causing any trouble?
You have a couple of problems. The first is that your anonymous volume mount has a typo. It should be node_modules
(with an "s")
volumes:
- ".:/usr/src/app"
- "/usr/src/app/node_modules"
The second is that you are copying all your files into the container. This is unnecessary since you intend on volume mounting them.
Just copy the files required to install dependencies
FROM node:14-alpine
WORKDIR /usr/src/app
# If I don't install nest's cli the app won't start telling me nest was not found.
RUN npm install -g @nestjs/cli
COPY package.json package-lock.json .
# Install all dependencies
RUN npm install
# package.json start script
CMD [ "npm", "run", "start:dev"]