Skip to content

Commit d73b6bc

Browse files
committed
Update docker build (works locally at least)
1 parent 583a567 commit d73b6bc

6 files changed

Lines changed: 94 additions & 101 deletions

File tree

.dockerignore

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
1-
node_modules
2-
dist
3-
build
4-
.cache
5-
.yarn
6-
.pnp.*
7-
*.log
8-
.dockerignore
9-
.gitignore
10-
Dockerfile
11-
docker-compose.yml
12-
node_modules
13-
dist
14-
built
15-
log
16-
data.json
1+
node_modules/
2+
dist/
3+
built/
4+
.cache/
5+
.yarn/
176
.env
18-
.vscode
19-
docs
20-
/public/css/spectre.min.css
21-
/public/css/spectre-exp.min.css
22-
/public/css/spectre-icons.min.css
7+
log/
8+
docs/

.env.example

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,18 @@ LISTEN=tcp
2727
# Set to 0 if you want a system-allocated dynamic port. (32768–60999 on many Linux kernels, 49152–65535 per IANA recommendation)
2828
# If you set this, you must also set the LISTEN to "tcp".
2929
PORT=8080
30-
HOST=127.0.0.1
30+
HOST=0.0.0.0
3131

3232
# See: http://expressjs.com/en/4x/api.html#trust.proxy.options.table
3333
TRUST_PROXY=false
3434

3535
###############################################################################
3636
## DATABASE SETTINGS
3737

38-
# PostgreSQL database connection string.
39-
DATABASE_URL=postgres://username:password@localhost:5432/database
38+
# PostgreSQL database connection string.
39+
# If running via Docker Compose, the host should be "db" (the name of the postgres service).
40+
# Example: postgres://[user]:[password]@db:5432/[database]
41+
DATABASE_URL=postgres://postgres:supersecretpassword@db:5432/infernal_db
4042

4143
# Determines if the database driver will log queries.
4244
DATABASE_LOG=false
@@ -45,4 +47,4 @@ DATABASE_LOG=false
4547
## API KEYS
4648

4749
# The API key for YouTube Data API v3.
48-
YOUTUBE_API_KEY=
50+
YOUTUBE_API_KEY=

Dockerfile

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,38 @@
1-
FROM node:alpine AS build
1+
FROM node:22-alpine AS build
22

33
WORKDIR /app
4+
5+
COPY package.json yarn.lock ./
6+
COPY scripts ./scripts
7+
COPY patches ./patches
8+
RUN yarn install --frozen-lockfile --non-interactive
9+
410
COPY . .
511

6-
RUN yarn install --frozen-lockfile --non-interactive --verbose && \
7-
yarn cache clean
812
RUN yarn build:only
913

14+
FROM node:22-alpine AS production
15+
16+
ENV NODE_ENV=production
17+
18+
WORKDIR /app
19+
20+
COPY package.json yarn.lock ./
21+
COPY scripts ./scripts
22+
COPY patches ./patches
23+
24+
RUN yarn install --frozen-lockfile --non-interactive --production && \
25+
yarn cache clean
26+
27+
COPY --from=build /app/dist ./dist
28+
COPY --from=build /app/built ./built
29+
COPY --from=build /app/public ./public
30+
31+
RUN addgroup -g 1001 -S nodejs && \
32+
adduser -S nodeuser -u 1001 && \
33+
chown -R nodeuser:nodejs /app
34+
USER nodeuser
35+
36+
EXPOSE 8080
37+
1038
ENTRYPOINT [ "yarn", "start" ]

deploy.sh

Lines changed: 10 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,13 @@
11
#!/bin/sh
2-
# This script is run without arguments, in the deployment user's home directory.
3-
# This is a modified version of https://github.com/SwanX1/cernavskis.dev/blob/main/deploy.sh
4-
# It does not have administrative privileges.
52

63
SERVICE_NAME="webapp"
7-
SERVICE_USER="github"
8-
GIT_REMOTE="https://github.com/infernalstudios.org.git"
94
GIT_BRANCH="stable"
105

11-
# Check if the script is run as root
126
if [ "$(id -u)" -eq 0 ]; then
137
echo "This script must not be run as root."
148
exit 1
159
fi
1610

17-
# Check if bun is installed
18-
if [ -z $(command -v yarn) ]; then
19-
echo "yarn is not installed. Please install it first."
20-
exit 1
21-
fi
22-
23-
# Check if screen is installed
24-
if [ -z $(command -v screen) ]; then
25-
echo "screen is not installed. Please install it first."
26-
exit 1
27-
fi
28-
29-
echo "Running as $(whoami) in $(pwd)."
30-
3111
# Find service path
3212
if [ ! -f "$(pwd)/.service_paths" ]; then
3313
echo "Did you initialize the service using server-init.sh?: .service_paths not found."
@@ -36,60 +16,25 @@ fi
3616

3717
SERVICE_PATH=$(cat $(pwd)/.service_paths | grep $SERVICE_NAME | cut -d' ' -f2)
3818

39-
if [ -z "$SERVICE_PATH" ]; then
40-
echo "Did you initialize the service using server-init.sh?: Service path not found in .service_paths."
41-
exit 1
42-
fi
43-
44-
if [ ! -d "$SERVICE_PATH" ]; then
45-
echo "Did you initialize the service using server-init.sh?: Service path does not exist."
19+
if [ -z "$SERVICE_PATH" ] || [ ! -d "$SERVICE_PATH" ]; then
20+
echo "Service path invalid or missing."
4621
exit 1
4722
fi
4823

4924
cd $SERVICE_PATH
5025

51-
# Checkout the latest commit on GIT_BRANCH
26+
# Checkout the latest commit
5227
git fetch origin -v
53-
if [ "$!" -ne 0 ]; then
54-
echo "Failed to fetch."
55-
exit 1
56-
fi
5728
git checkout origin/$GIT_BRANCH
58-
if [ "$!" -ne 0 ]; then
59-
echo "Failed to checkout the latest commit."
60-
exit 1
61-
fi
6229

63-
# Install the dependencies
64-
yarn install --frozen-lockfile --non-interactive --verbose
30+
# Use Docker Compose
31+
echo "Building and starting Docker containers..."
32+
docker compose up -d --build
6533

66-
if [ "$!" -ne 0 ]; then
67-
echo "Failed to install the dependencies."
34+
if [ "$?" -ne 0 ]; then
35+
echo "Docker compose failed to start the service."
6836
exit 1
6937
fi
7038

71-
# We use build:only here, because we don't want to run any checks beforehand. Those are handled by GH Actions.
72-
yarn build:only
73-
74-
if [ "$!" -ne 0 ]; then
75-
echo "Failed to build, this should be unreachable."
76-
echo "Ignoring this, since tsc and webpack transpiles everything regardless of errors."
77-
echo "This should have been caught by GH Actions."
78-
fi
79-
80-
if [ ! -z "$(screen -ls $SERVICE_NAME | grep $SERVICE_NAME)" ]; then
81-
echo "Service is already running, stopping it."
82-
screen -S $SERVICE_NAME -X quit
83-
if [ "$!" -ne 0 ]; then
84-
echo "Failed to stop the service."
85-
exit 1
86-
fi
87-
fi
88-
89-
echo "Starting the service."
90-
screen -dmqS $SERVICE_NAME yarn start
91-
92-
if [ "$!" -ne 0 ]; then
93-
echo "Failed to start the service."
94-
exit 1
95-
fi
39+
echo "Deployment successful."
40+
docker image prune -f

docker-compose.yml

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
11
services:
22
app:
3-
build:
4-
context: .
5-
dockerfile: Dockerfile
6-
container_name: app
3+
build:
4+
context: .
5+
dockerfile: Dockerfile
6+
container_name: app
7+
restart: unless-stopped
8+
ports:
9+
- "8080:8080"
10+
volumes:
11+
- ./.env:/app/.env:ro
12+
- ./log:/app/log
13+
depends_on:
14+
db:
15+
condition: service_healthy
16+
environment:
17+
- NODE_ENV=production
18+
- LISTEN=tcp
19+
- HOST=0.0.0.0
20+
- DATABASE_URL=postgres://postgres:supersecretpassword@db:5432/infernal_db
21+
22+
db:
23+
image: postgres:15-alpine
24+
container_name: app_db
25+
restart: unless-stopped
26+
environment:
27+
POSTGRES_USER: postgres
28+
POSTGRES_PASSWORD: supersecretpassword
29+
POSTGRES_DB: infernal_db
30+
ports:
31+
- "5432:5432"
732
volumes:
8-
- ./.env:/app/.env:ro
9-
network_mode: "host"
33+
- pgdata:/var/lib/postgresql/data
34+
healthcheck:
35+
test: ["CMD-SHELL", "pg_isready -U postgres"]
36+
interval: 5s
37+
timeout: 5s
38+
retries: 5
39+
40+
volumes:
41+
pgdata:

server-init.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ git checkout origin/$GIT_BRANCH
4747

4848
chown -R $SERVICE_USER $SERVICE_PATH
4949

50-
if [ -z $(command -v screen) ]; then
51-
echo "screen is not installed. Please install it before deployment."
50+
if [ -z $(command -v docker) ]; then
51+
echo "docker is not installed. Please install Docker and Docker Compose before deployment."
5252
fi

0 commit comments

Comments
 (0)