Skip to content

Commit f31bc5c

Browse files
authored
Merge pull request #1 from fenix-hub/dev/dockerize
Dockerize project
2 parents 37dea10 + 44acfb5 commit f31bc5c

File tree

6 files changed

+89
-2
lines changed

6 files changed

+89
-2
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
npm-debug.log

.env.example

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1-
MONGO_URI=<mongo uri>
1+
MONGO_USERNAME=
2+
MONGO_PASSWORD=
3+
MONGO_HOST=mongo
4+
MONGO_PORT=27017
5+
MONGO_DB=ezlb
6+
MONGO_USE_SRV=false
27
PORT=6999

Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM node:latest
2+
3+
WORKDIR /usr/src/app
4+
COPY package*.json ./
5+
6+
RUN npm install
7+
COPY . .
8+
9+
EXPOSE 6999
10+
CMD [ "npm", "start" ]

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,23 @@ To make the application reachable and always online, you will want to host it so
117117
- [Repl.it](https://replit.com/languages/nodejs)
118118
- Repl.it has an always-on tier of subscription. You can fork this repo and Replit can easily import it so you can edit it live. You may also be able to modify the app to use [Replit's key/value DB by using the code from this Replit that adds a layer of abstraction](https://github.com/adrenallen/replit-db-orm)
119119

120+
## 🫙 Dockerized Setup
121+
If you have [Docker](https://www.docker.com/) installed, you can simply run the `docker compose up -d` command to run the whole project in a containerized environment.
122+
Even though this setup is best-suited for local development, it can be used in production too on your own server (VPS, AWS, Digital Ocean...).
123+
124+
You can also use `docker compose --profile=admin-panel up -d` to run a local admin panel for your mongo database.
125+
126+
Docker will use the same .env file used by the Node web server to resolve internal variables. In this way the containers are highly customizable together with the web server itself.
127+
128+
```
129+
# example configuration of .env in development
130+
MONGO_USERNAME=root
131+
MONGO_PASSWORD=example
132+
MONGO_HOST=mongo
133+
MONGO_PORT=27017
134+
MONGO_DB=mygame
135+
PORT=6999
136+
```
120137

121138
## :wrench: Customization
122139

docker-compose.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
version: '3.1'
2+
3+
volumes:
4+
easyleaderboard-data:
5+
6+
networks:
7+
easyleaderboard-network:
8+
9+
10+
services:
11+
12+
server:
13+
container_name: ezlb-server
14+
build:
15+
context: .
16+
dockerfile: Dockerfile
17+
restart: always
18+
ports:
19+
- ${PORT}:${PORT}
20+
depends_on:
21+
- mongo
22+
networks:
23+
- easyleaderboard-network
24+
25+
mongo:
26+
container_name: ezlb-db
27+
image: mongo
28+
restart: always
29+
environment:
30+
MONGO_INITDB_ROOT_USERNAME: root
31+
MONGO_INITDB_ROOT_PASSWORD: example
32+
volumes:
33+
- easyleaderboard-data:/data/db
34+
networks:
35+
- easyleaderboard-network
36+
37+
mongo-express:
38+
container_name: ezlb-mongo-panel
39+
profiles:
40+
- admin-panel
41+
image: mongo-express
42+
restart: always
43+
ports:
44+
- 127.0.0.1:8081:8081/tcp
45+
environment:
46+
ME_CONFIG_MONGODB_ADMINUSERNAME: ${MONGO_USERNAME}
47+
ME_CONFIG_MONGODB_ADMINPASSWORD: ${MONGO_PASSWORD}
48+
ME_CONFIG_MONGODB_URL: mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOST}:${MONGO_PORT}/
49+
networks:
50+
- easyleaderboard-network

mongoose_factory.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const mongoose = require('mongoose');
22

3-
mongoose.connect(process.env.MONGO_URI, {useNewUrlParser: true, useUnifiedTopology: true});
3+
mongoose.connect(
4+
`mongodb${process.env.MONGO_USE_SRV ? '+srv' : ''}://${process.env.MONGO_USERNAME}:${process.env.MONGO_PASSWORD}@${process.env.MONGO_HOST}${process.env.MONGO_USE_SRV ? '' : ':'+process.env.MONGO_PORT}/${process.env.MONGO_DB}`,
5+
{useNewUrlParser: true, useUnifiedTopology: true}
6+
);
47

58
mongoose.set('useFindAndModify', false);
69

0 commit comments

Comments
 (0)