Skip to content

Commit bfe9b7c

Browse files
committed
add: docker file
1 parent b366a2d commit bfe9b7c

File tree

3 files changed

+109
-11
lines changed

3 files changed

+109
-11
lines changed

backend/.dockerignore

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Folders
2+
3+
coverage/
4+
dist/
5+
node_modules/
6+
out/
7+
test/
8+
9+
# Files
10+
11+
.dockerignore
12+
.env
13+
.eslintrc.js
14+
.gitignore
15+
.prettier
16+
Dockerfile
17+
nest-cli.json
18+
README.md

backend/Dockerfile

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Base image
2+
FROM node:20.18.1-alpine AS base
3+
RUN addgroup -S backend && \
4+
adduser -S backend -G backend
5+
RUN npm install -g pnpm
6+
WORKDIR /app
7+
EXPOSE 3000
8+
9+
#----------------------------------------------------------------
10+
11+
# Build layer
12+
FROM base AS build
13+
COPY package.json pnpm-lock.yaml tsconfig.json tsconfig.build.json ./
14+
RUN pnpm install --frozen-lockfile --ignore-scripts
15+
COPY ./src ./src
16+
RUN pnpm build --webpack
17+
18+
#----------------------------------------------------------------
19+
20+
# Production layer
21+
FROM base AS production
22+
ARG NODE_ENV=production
23+
ENV NODE_ENV=${NODE_ENV}
24+
WORKDIR /app
25+
COPY --from=build /app/node_modules ./node_modules
26+
COPY --from=build /app/dist ./dist
27+
COPY package.json ./
28+
ENTRYPOINT ["node", "dist/main.js"]

backend/README.md

+63-11
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,16 @@
1212

1313
Create a `.env` file there and add the following environment variables:
1414

15-
| # | Variable Name | Description | Example |
16-
| --- | ----------------------------- | --------------------------------------------------- | ----------------------------------------------- |
17-
| 1 | DATABASE_URL | The URL to the MongoDB database | localhost:27017/evora |
18-
| 2 | JWT_SECRET | The secret key for JWT | secret-key |
19-
| 3 | JWT_EXPIRES | The expiration time for JWT | 1h |
20-
| 4 | GOOGLE_OAUTH2_CLIENT_ID | The Google client ID | abcxyz |
21-
| 5 | GOOGLE_OAUTH2_CLIENT_SECRET | The Google client secret | secret-key |
22-
| 6 | GOOGLE_OAUTH2_CLIENT_CALLBACK | The Google client callback URL | http://localhost:3000/api/auth/user/callback |
23-
| 7 | ALLOWED_ORIGINS | The allowed origins for CORS (separated by commas ) | http://localhost:3000,https://localhost:3001 |
24-
| 8 | FRONTEND_URL | The URL to the frontend | http://localhost:5173 |
25-
15+
| # | Variable Name | Description | Example |
16+
| --- | ----------------------------- | --------------------------------------------------- | -------------------------------------------- |
17+
| 1 | DATABASE_URL | The URL to the MongoDB database | localhost:27017/evora |
18+
| 2 | JWT_SECRET | The secret key for JWT | secret-key |
19+
| 3 | JWT_EXPIRES | The expiration time for JWT | 1h |
20+
| 4 | GOOGLE_OAUTH2_CLIENT_ID | The Google client ID | abcxyz |
21+
| 5 | GOOGLE_OAUTH2_CLIENT_SECRET | The Google client secret | secret-key |
22+
| 6 | GOOGLE_OAUTH2_CLIENT_CALLBACK | The Google client callback URL | http://localhost:3000/api/auth/user/callback |
23+
| 7 | ALLOWED_ORIGINS | The allowed origins for CORS (separated by commas ) | http://localhost:3000,https://localhost:3001 |
24+
| 8 | FRONTEND_URL | The URL to the frontend | http://localhost:5173 |
2625

2726
## Development
2827

@@ -39,3 +38,56 @@ Create a `.env` file there and add the following environment variables:
3938
```
4039

4140
- Step 3: Open the browser and navigate to [http://localhost:3000](http://localhost:3000)
41+
42+
### Build & push container to Docker Hub
43+
44+
- Setup `Dockerfile`
45+
46+
```bash
47+
# Example file:
48+
49+
FROM node:lts-alpine
50+
WORKDIR /app
51+
COPY . .
52+
RUN yarn install --production
53+
CMD ["node", "src/index.js"]
54+
EXPOSE 3000
55+
```
56+
57+
- Build image Docker
58+
59+
```bash
60+
docker build -t your-dockerhub-username/image-name:[tag] .
61+
```
62+
63+
- Push image to Docker Hub
64+
65+
```bash
66+
docker push your-dockerhub-username/image-name:[tag]
67+
```
68+
69+
- Run docker container
70+
71+
```bash
72+
docker run -p 3000:3000 --env-file .env your-dockerhub-username/image-name:[tag]
73+
```
74+
75+
### Pull & run container
76+
77+
- Pull image từ Docker Hub:
78+
79+
```bash
80+
docker pull your-dockerhub-username/image-name:[tag]
81+
```
82+
83+
- Chạy container:
84+
85+
```bash
86+
docker run -d -p 3000:3000 --name evora-backend your-dockerhub-username/image-name:[tag]
87+
```
88+
89+
- Kiểm tra container đang chạy:
90+
91+
```bash
92+
docker ps
93+
```

0 commit comments

Comments
 (0)