Skip to content

Commit

Permalink
fix: fix docker config
Browse files Browse the repository at this point in the history
add working docker configuration for dev, test, and production environments. change FRONT_URL to
CLIENT_URL. fix errors caused by CLIENT_URL

BREAKING CHANGE: yarn start does not build before starting. Use yarn start:build if you want to
build before starting. FRONT_URL has been replaced with CLIENT_URL as it is more descriptive

Running yarn docker:dev results in error while composing node-app saisilinus#28
  • Loading branch information
saisilinus committed Oct 29, 2022
1 parent ceb2c32 commit f762ea8
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 51 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ SMTP_PORT=587
SMTP_USERNAME=email-server-username
SMTP_PASSWORD=email-server-password
EMAIL_FROM=[email protected]
FRONT_URL=http://localhost:5000

# Cookie configs
COOKIE_SECRET=thisisasamplesecret

# URL of client application
CLIENT_URL=http://localhost:5000
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
bin
data
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ env:
JWT_REFRESH_EXPIRATION_DAYS: 30
JWT_RESET_PASSWORD_EXPIRATION_MINUTES: 10
JWT_VERIFY_EMAIL_EXPIRATION_MINUTES: 10
FRONT_URL: http://localhost:3000
CLIENT_URL: http://localhost:3000

jobs:

Expand Down
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ yarn-error.log
# Code coverage
coverage

# bunlde folder
dist
# build folder
dist

# docker volume
data
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules
coverage

data
27 changes: 19 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
FROM node:alpine
# development stage
FROM node:14-alpine as base

RUN mkdir -p /usr/dist/node-app && chown -R node:node /usr/dist/node-app
WORKDIR /usr/src/app

WORKDIR /usr/dist/node-app
COPY package.json yarn.lock tsconfig.json ecosystem.config.json ./

COPY package.json yarn.lock ./
COPY ./src ./src

USER node
RUN ls -a

RUN yarn install --pure-lockfile
RUN yarn install --pure-lockfile && yarn compile

COPY --chown=node:node . .
# production stage

EXPOSE 3000
FROM base as production

WORKDIR /usr/prod/app

ENV NODE_ENV=production

COPY package.json yarn.lock ecosystem.config.json ./

RUN yarn install --production --pure-lockfile

COPY --from=base /usr/src/app/dist ./dist
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ SMTP_PORT=587
SMTP_USERNAME=email-server-username
SMTP_PASSWORD=email-server-password
[email protected]
FRONT_URL=http://localhost:5000

# URL of client application
CLIENT_URL=http://localhost:5000
```

## Project Structure
Expand Down
12 changes: 10 additions & 2 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
version: '3'

services:
node-app:
container_name: node-app-dev
mongo:
volumes:
- ./data:/data/db

app:
container_name: ts-node-app-dev
command: yarn dev -L

volumes:
mongo-data:
driver: local
6 changes: 4 additions & 2 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
version: '3'

services:
node-app:
container_name: node-app-prod
app:
container_name: ts-node-app-prod
build:
target: production
command: yarn start
6 changes: 3 additions & 3 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: '3'

services:
node-app:
container_name: node-app-test
command: yarn test
app:
container_name: ts-node-app-test
command: yarn test:js
50 changes: 27 additions & 23 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
version: '3'

services:
node-app:
build: .
image: node-app
environment:
- MONGODB_URL=mongodb://mongodb:27017/node-app
mongo:
container_name: mongo
image: mongo:4.2.1-bionic
restart: always
ports:
- '3000:3000'
depends_on:
- mongodb
volumes:
- .:/usr/dist/node-app
- "27018:27017"
networks:
- node-app-network

mongodb:
image: mongo:4.2.1-bionic
- backend
app:
container_name: ts-node-app
build:
context: .
dockerfile: Dockerfile
target: base
restart: always
env_file: .env
expose:
- ${PORT}
ports:
- '27017:27017'
volumes:
- dbdata:/data/db
- ${PORT}:${PORT}
environment:
- MONGODB_URL=mongodb://mongo:27017/node-boilerplate
- CLIENT_URL=${CLIENT_URL}
links:
- mongo
depends_on:
- mongo
networks:
- node-app-network

volumes:
dbdata:
- backend

networks:
node-app-network:
driver: bridge
backend:
driver: bridge
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"node": ">=14.0.0"
},
"scripts": {
"start": "tsc --build && pm2 start ecosystem.config.json --no-daemon",
"start": "pm2 start ecosystem.config.json --no-daemon",
"start:build": "tsc --build && pm2 start ecosystem.config.json --no-daemon",
"compile": "tsc --build",
"compile:watch": "tsc --build --watch",
"pre:dev": "cross-env NODE_ENV=development nodemon --experimental-modules --es-module-specifier-resolution=node dist/index.js",
Expand Down
4 changes: 2 additions & 2 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const envVarsSchema = Joi.object()
SMTP_USERNAME: Joi.string().description('username for email server'),
SMTP_PASSWORD: Joi.string().description('password for email server'),
EMAIL_FROM: Joi.string().description('the from field in the emails sent by the app'),
FRONT_URL: Joi.string().required().description('Front url'),
CLIENT_URL: Joi.string().required().description('Client url'),
})
.unknown();

Expand Down Expand Up @@ -64,7 +64,7 @@ const config = {
},
from: envVars.EMAIL_FROM,
},
frontUrl: envVars.FRONT_URL,
clientUrl: envVars.CLIENT_URL,
};

export default config;
8 changes: 4 additions & 4 deletions src/modules/email/email.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const sendEmail = async (to: string, subject: string, text: string, html:
export const sendResetPasswordEmail = async (to: string, token: string): Promise<void> => {
const subject = 'Reset password';
// replace this url with the link to the reset password page of your front-end app
const resetPasswordUrl = `http://${config.frontUrl}/reset-password?token=${token}`;
const resetPasswordUrl = `http://${config.clientUrl}/reset-password?token=${token}`;
const text = `Hi,
To reset your password, click on this link: ${resetPasswordUrl}
If you did not request any password resets, then ignore this email.`;
Expand All @@ -62,7 +62,7 @@ export const sendResetPasswordEmail = async (to: string, token: string): Promise
export const sendVerificationEmail = async (to: string, token: string, name: string): Promise<void> => {
const subject = 'Email Verification';
// replace this url with the link to the email verification page of your front-end app
const verificationEmailUrl = `http://${config.frontUrl}/verify-email?token=${token}`;
const verificationEmailUrl = `http://${config.clientUrl}/verify-email?token=${token}`;
const text = `Hi ${name},
To verify your email, click on this link: ${verificationEmailUrl}
If you did not create an account, then ignore this email.`;
Expand All @@ -82,7 +82,7 @@ export const sendVerificationEmail = async (to: string, token: string, name: str
export const sendSuccessfulRegistration = async (to: string, token: string, name: string): Promise<void> => {
const subject = 'Email Verification';
// replace this url with the link to the email verification page of your front-end app
const verificationEmailUrl = `http://${config.frontUrl}/verify-email?token=${token}`;
const verificationEmailUrl = `http://${config.clientUrl}/verify-email?token=${token}`;
const text = `Hi ${name},
Congratulations! Your account has been created.
You are almost there. Complete the final step by verifying your email at: ${verificationEmailUrl}
Expand All @@ -107,7 +107,7 @@ export const sendSuccessfulRegistration = async (to: string, token: string, name
export const sendAccountCreated = async (to: string, name: string): Promise<void> => {
const subject = 'Account Created Successfully';
// replace this url with the link to the email verification page of your front-end app
const loginUrl = `http://${config.frontUrl}/auth/login`;
const loginUrl = `http://${config.clientUrl}/auth/login`;
const text = `Hi ${name},
Congratulations! Your account has been created successfully.
You can now login at: ${loginUrl}
Expand Down

0 comments on commit f762ea8

Please sign in to comment.