Skip to content

Commit

Permalink
🚧 Improve dockerfile and start script and fix permission issue by add…
Browse files Browse the repository at this point in the history
…ing new user with permission to read / write to /data folder
  • Loading branch information
Meierschlumpf committed Nov 12, 2023
1 parent 351aa47 commit 2a1f733
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 27 deletions.
30 changes: 26 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
FROM node:20.5-slim
WORKDIR /app

ARG UID=1001
ARG GID=1001
RUN groupadd -g $GID homarr-group
RUN useradd -r -u $UID -g $GID homarr

# Define node.js environment variables
ARG PORT=7575

Expand All @@ -18,17 +23,30 @@ COPY .next/standalone ./
COPY .next/static ./.next/static
COPY ./scripts/run.sh ./scripts/run.sh
COPY ./drizzle ./drizzle

COPY ./drizzle/migrate ./migrate
COPY ./tsconfig.json ./migrate/tsconfig.json

RUN mkdir /data
COPY ./src/migrate.ts ./src/migrate.ts
RUN chown -R homarr:homarr-group /data

# Install dependencies
RUN apt-get update -y && apt-get install -y openssl wget

# Required for migration
# Move node_modules to temp location to avoid overwriting
RUN mv node_modules _node_modules
RUN rm package.json
RUN yarn add typescript ts-node dotenv [email protected] [email protected] @types/better-sqlite3
RUN mv node_modules node_modules_migrate

# Install dependencies for migration
RUN cp ./migrate/package.json ./package.json
RUN yarn

# Copy better_sqlite3 build for current platform
RUN cp /app/node_modules/better-sqlite3/build/Release/better_sqlite3.node /app/_node_modules/better-sqlite3/build/Release/better_sqlite3.node

# Copy node_modules for migration to migrate folder for migration script
RUN mv node_modules ./migrate/node_modules
# Copy temp node_modules of app to app folder
RUN mv _node_modules node_modules

# Expose the default application port
Expand All @@ -43,4 +61,8 @@ ENV NEXTAUTH_SECRET NOT_IN_USE_BECAUSE_JWTS_ARE_UNUSED
HEALTHCHECK --interval=10s --timeout=5s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:${PORT} || exit 1

USER homarr

CMD ["sh", "./scripts/run.sh"]

# TEMPORARY: "db:migrate": "ts-node ./migrate.ts ../drizzle"
5 changes: 4 additions & 1 deletion src/migrate.ts → drizzle/migrate/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import dotenv from 'dotenv';
import { drizzle } from 'drizzle-orm/better-sqlite3';
import { migrate } from 'drizzle-orm/better-sqlite3/migrator';

// TODO: Remove
// const migrationsFolder = process.argv[2] ?? './drizzle';

dotenv.config({ path: __dirname + '/../.env' });

const sqlite = new Database(process.env.DATABASE_URL!.replace('file:', ''));

const db = drizzle(sqlite);

const migrateDatabase = async () => {
await migrate(db, { migrationsFolder: './drizzle' });
await migrate(db, { migrationsFolder: '../drizzle' });
};

migrateDatabase();
13 changes: 13 additions & 0 deletions drizzle/migrate/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"scripts": {
"db:migrate": "ts-node ./migrate.ts"
},
"dependencies": {
"@types/better-sqlite3": "^7.6.7",
"better-sqlite3": "8.6.0",
"drizzle-orm": "^0.28.6",
"dotenv": "^16.3.1",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"test:coverage": "SKIP_ENV_VALIDATION=1 vitest run --coverage",
"docker:build": "turbo build && docker build . -t homarr:local-dev",
"docker:start": "docker run -p 7575:7575 --name homarr-development homarr:local-dev",
"db:migrate": "ts-node src/migrate.ts"
"db:migrate": "ts-node drizzle/migrate/migrate.ts"
},
"dependencies": {
"@auth/drizzle-adapter": "^0.3.2",
Expand Down Expand Up @@ -232,4 +232,4 @@
]
}
}
}
}
19 changes: 1 addition & 18 deletions scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,11 @@

echo "Exporting hostname..."
export NEXTAUTH_URL_INTERNAL="http://$HOSTNAME:7575"
rm -rf _node_modules
mv node_modules _node_modules
rm -rf node_modules
mv node_modules_migrate node_modules

echo "Migrating database..."
yarn ts-node src/migrate.ts & PID=$!
cd ./migrate; yarn db:migrate & PID=$!
# Wait for migration to finish
wait $PID

echo "Reverting to production node_modules..."
# Copy specific sqlite3 binary to node_modules
cp /app/node_modules/better-sqlite3/build/Release/better_sqlite3.node /app/_node_modules/better-sqlite3/build/Release/better_sqlite3.node

# Remove node_modules and copy cached node_modules
rm -rf node_modules_migrate
mv node_modules node_modules_migrate
rm -rf node_modules
mv _node_modules node_modules

cp ./temp_package.json package.json
cp ./temp_yarn.lock yarn.lock

echo "Starting production server..."
node /app/server.js
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7152,7 +7152,7 @@ __metadata:
rss-parser: ^3.12.0
sabnzbd-api: ^1.5.0
sass: ^1.56.1
ts-node: latest
ts-node: ^10.9.1
turbo: ^1.10.12
typescript: 5.1.6
uuid: ^9.0.0
Expand Down Expand Up @@ -11590,7 +11590,7 @@ __metadata:
languageName: node
linkType: hard

"ts-node@npm:latest":
"ts-node@npm:^10.9.1":
version: 10.9.1
resolution: "ts-node@npm:10.9.1"
dependencies:
Expand Down

0 comments on commit 2a1f733

Please sign in to comment.