Skip to content

Commit 3eba547

Browse files
committed
Created docker files to allow simple docker creation
1 parent f06c04a commit 3eba547

7 files changed

Lines changed: 133 additions & 21 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
/coverage
66
/build
7+
/docker/resumes
78

89
backend/challenge_archives/*.zip
910

README.md

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,14 @@ Website repo for the Kent Hack It CTF
44
## What is this?
55
The frontend uses ReactJS that communicates to a ExpressJS backend that handles: user login/registration, team creation/joining, and flag submission.
66

7-
## :hammer: Install Dependencies
7+
## :computer: Development Installation
8+
### :hammer: Install Dependencies
89
```bash
910
# if you do not have NodeJS installed
1011
sudo apt update && sudo apt install nodejs npm
11-
12-
# use npm to install latest node
13-
sudo npm install -g n && sudo n stable
14-
15-
# if you do not want to keep the older nodejs binary on the system:
16-
# replace old nodejs for new nodejs
17-
$oldNode = $(which nodejs)
18-
sudo rm -f $oldNode
19-
20-
# create a symlink named nodejs to execute the node binary
21-
sudo ln -s $(which node) $oldNode
2212
```
2313

24-
## :wrench: Build
14+
### :wrench: Build
2515
```bash
2616
# installs dependencies/packages tracked by this project
2717
npm install .
@@ -31,16 +21,22 @@ npm start
3121

3222
Move the `.env` file into the project root directory, then run the following to run the backend locally.
3323
```bash
34-
# if you want to use the latest node binary replace nodejs -> node
35-
nodejs ./src/backend/server.js
24+
nodejs backend/server.mjs
3625
```
3726

3827
## :chart_with_upwards_trend: Production
39-
You will need to install a dependency to use `serve`, this is recommended after running `npm run build`.
28+
This project uses Docker :whale: for production set-up
29+
```bash
30+
sudo apt update && sudo apt install docker.io docker-compose
31+
```
32+
The following commands will build the docker via compose which builds the multi-docker system.
4033
```bash
41-
sudo npm install -g serve
34+
cd docker
35+
docker network create traefik
36+
docker-compose -p khi up --build
4237
```
43-
Prepare production build and serve as a static server.
38+
The following commands will build ONLY the khi website docker.
4439
```bash
45-
npm run build && server -s build
40+
docker build -f docker/Dockerfile -t image_name .
41+
docker run -d -p 8080:80 --name docker_name image_name
4642
```

docker/Dockerfile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
FROM nginx:latest
2+
WORKDIR /
3+
4+
# setup for mongo container:
5+
# docker run -d --name khi_db -e MONGO_INITDB_ROOT_USERNAME=env.user -e MONGO_INITDB_ROOT_PASSWORD=env.password -e MONGO_INITDB_DATABASE=env.db -v mongodb_data:/data/db -p 27017:27017 mongo:latest
6+
RUN apt-get update && apt-get install -y supervisor sudo nodejs npm net-tools
7+
8+
# move production build into docker and move
9+
# nginx conf file into the docker
10+
COPY build /var/www/khi
11+
COPY package.json /var/www/khi/package.json
12+
COPY .env /var/www/khi/.env
13+
COPY docker/khi /etc/nginx/conf.d/khi.conf
14+
15+
# move backend to web root
16+
COPY backend /var/www/khi/backend
17+
18+
# remove default nginx config
19+
RUN rm /etc/nginx/conf.d/default.conf
20+
21+
# give www-data ability to run commands and interact with web-root
22+
RUN chsh -s /bin/bash www-data
23+
RUN chown -R www-data:www-data /var/www
24+
# install needed nodejs packages to allow the backend to run
25+
RUN su www-data -s /bin/sh -c "cd /var/www/khi && npm install ."
26+
27+
EXPOSE 80
28+
29+
# start an auto restart service for the backend
30+
COPY docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
31+
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

docker/docker-compose.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
services:
2+
db:
3+
image: mongo:latest
4+
container_name: khi_db
5+
restart: always
6+
environment:
7+
- MONGO_INITDB_ROOT_USERNAME=dbu
8+
- MONGO_INITDB_ROOT_PASSWORD=YFt3fG6wWYJJx8Rj9NssVVmF
9+
- MONGO_INITDB_DATABASE=khi2025
10+
volumes:
11+
- db_data:/data/db
12+
healthcheck:
13+
test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
14+
interval: 10s
15+
timeout: 5s
16+
retries: 5
17+
networks:
18+
- app_internal
19+
20+
app:
21+
depends_on:
22+
db:
23+
condition: service_healthy
24+
build:
25+
context: ../
26+
dockerfile: docker/Dockerfile
27+
ports:
28+
- "8080:80"
29+
environment:
30+
- DATABASE_URL=mongodb://dbu:YFt3fG6wWYJJx8Rj9NssVVmF@khi_db:27017/khi2025
31+
- ORIGIN=http://0.0.0.0:80
32+
labels:
33+
- "traefik.enable=true"
34+
- "traefik.http.routers.khi.rule=Host(`khi.io`)"
35+
- "traefik.http.routers.khi.entrypoints=websecure"
36+
- "traefik.http.routers.khi.tls.certresolver=letsencrypt"
37+
- "traefik.http.services.khi.loadbalancer.server.port=80"
38+
volumes:
39+
- ./resumes:/app/resumes
40+
networks:
41+
- app_internal
42+
- traefik
43+
44+
networks:
45+
app_internal:
46+
traefik:
47+
external: true
48+
49+
volumes:
50+
db_data:

docker/khi

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
server {
2+
listen 80;
3+
server_name khi;
4+
5+
root /var/www/khi;
6+
index index.html;
7+
8+
location / {
9+
try_files $uri /index.html;
10+
}
11+
12+
# points to backend (reverse proxy)
13+
location /api/ {
14+
client_max_body_size 25M;
15+
proxy_pass http://127.0.0.1:4000/;
16+
proxy_http_version 1.1;
17+
proxy_set_header Host $host;
18+
proxy_set_header X-Real-IP $remote_addr;
19+
}
20+
}

docker/supervisord.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[supervisord]
2+
nodaemon=true
3+
4+
[program:nginx]
5+
command=/usr/sbin/nginx -g "daemon off;"
6+
autorestart=true
7+
8+
[program:backend]
9+
command=/usr/bin/nodejs /var/www/khi/backend/server.mjs
10+
directory=/var/www/khi
11+
autorestart=true
12+
user=www-data
13+
stdout_logfile=/var/log/backend.log
14+
stderr_logfile=/var/log/backend_err.log

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"web-vitals": "^2.1.4"
2424
},
2525
"engines": {
26-
"node": ">=18.19.1",
27-
"npm": ">=9.2.0"
26+
"node": ">=18.19",
27+
"npm": ">=9.2"
2828
},
2929
"scripts": {
3030
"start": "react-scripts start",

0 commit comments

Comments
 (0)