Skip to content

Commit

Permalink
fix(docker): Improved flexibility by enabling custom users with the …
Browse files Browse the repository at this point in the history
…Docker 'user' directive. The previous implementation restricted this to the 'node' user

* fix(docker): Improved flexibility by enabling custom users with the Docker 'user' directive. The previous implementation restricted this to the 'node' user

* refactor(docker): Moved the application's container location from /opt to /opt/app. /opt/data is unaffected

* refactor: App now tries to create the data directory structure

* refactor(docker): Exit app when we can't create the data directory
  • Loading branch information
jorenn92 authored Jan 25, 2024
1 parent 2c29467 commit 496401f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 24 deletions.
45 changes: 27 additions & 18 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
FROM node:20-alpine3.19 as BUILDER
LABEL Description="Contains the Maintainerr Docker image"

WORKDIR /opt
WORKDIR /opt/app/

ARG TARGETPLATFORM
ENV TARGETPLATFORM=${TARGETPLATFORM:-linux/amd64}

COPY server/ /opt/server/
COPY ui/ /opt/ui/
COPY docs/ /opt/docs/
COPY package.json /opt/package.json
COPY yarn.lock /opt/yarn.lock
COPY jsdoc.json /opt/jsdoc.json
COPY .yarnrc.yml /opt/.yarnrc.yml

WORKDIR /opt/
COPY server/ ./server/
COPY ui/ ./ui/
COPY docs/ ./docs/
COPY package.json ./package.json
COPY yarn.lock ./yarn.lock
COPY jsdoc.json ./jsdoc.json
COPY .yarnrc.yml ./.yarnrc.yml

# enable correct yarn version
RUN corepack install && \
Expand Down Expand Up @@ -55,34 +53,45 @@ RUN rm -rf node_modules .yarn
RUN yarn workspaces focus --production

RUN rm -rf .yarn && \
mkdir /opt/data && \
chown -R node:node /opt
rm -rf /opt/yarn-* && \
chown -R node:node /opt/ && \
chmod -R 755 /opt/ && \
# data dir
mkdir -m 777 /opt/data && \
mkdir -m 777 /opt/data/logs && \
chown -R node:node /opt/data

# Final build
FROM node:20-alpine3.19

ARG TARGETPLATFORM
ENV TARGETPLATFORM=${TARGETPLATFORM:-linux/amd64}

ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}

ARG DEBUG=false
ENV DEBUG=${DEBUG}

# set global yarn vars to a folder read/write able for all users
ENV YARN_INSTALL_STATE_PATH=/tmp/.yarn/install-state.gz
ENV YARN_GLOBAL_FOLDER=/tmp/.yarn/global
ENV YARN_CACHE_FOLDER=/tmp/.yarn/cache

# Temporary workaround for https://github.com/libuv/libuv/pull/4141
ENV UV_USE_IO_URING=0

WORKDIR /opt

COPY --from=builder --chown=node:node /opt /opt

WORKDIR /opt/app

COPY supervisord.conf /etc/supervisord.conf

# enable correct yarn version, add supervisor & chown root /opt dir
RUN corepack install && \
corepack enable && \
apk add supervisor && \
chown node:node /opt
chown node:node /opt && \
# This is required for docker user directive to work
chmod 777 /opt && \
mkdir -m 777 /.cache

USER node

Expand Down
2 changes: 1 addition & 1 deletion server/src/app/config/typeOrmConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const ormConfig: TypeOrmModuleOptions = {
subscribers: ['./**/*.subscriber{.ts,.js}'],
migrations:
process.env.NODE_ENV === 'production'
? ['/opt/server/database/migrations/**/*{.js,.ts}']
? ['/opt/app/server/database/migrations/**/*{.js,.ts}']
: ['./dist/database/migrations/**/*{.js,.ts}'],
autoLoadEntities: true,
migrationsRun: true,
Expand Down
23 changes: 22 additions & 1 deletion server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import winston from 'winston';
import DailyRotateFile from 'winston-daily-rotate-file';
import chalk from 'chalk';
import path from 'path';
import * as fs from 'fs';

async function bootstrap() {
const app = await NestFactory.create(AppModule, {
Expand Down Expand Up @@ -50,7 +51,7 @@ async function bootstrap() {
new DailyRotateFile({
filename: path.join(
__dirname,
`${process.env.NODE_ENV !== 'production' ? '../' : ''}../data/logs/maintainerr-%DATE%.log`,
`../../data/logs/maintainerr-%DATE%.log`,
),
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
Expand All @@ -70,4 +71,24 @@ async function bootstrap() {
app.enableCors();
await app.listen(3001);
}

function createDataDirectoryStructure(): void {
try {
const dir = path.join(__dirname, `../../data/logs`);

if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, {
recursive: true,
mode: 0o777,
});
}
} catch (err) {
console.error(
'Could not create data directory. Make sure your permissions are set correctly.',
);
process.exit(0);
}
}

createDataDirectoryStructure();
bootstrap();
8 changes: 4 additions & 4 deletions supervisord.conf
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[supervisord]
nodaemon=true
logfile=/opt/data/supervisord.log
logfile=/tmp/supervisord.log
pidfile=/tmp/supervisord.pid
logfile_maxbytes=20971520
user=node

[program:server]
command=yarn node /opt/server/main.js
command=yarn node /opt/app/server/main.js
autorestart=true
startretries=100
stdout_logfile=/dev/fd/1
Expand All @@ -14,7 +14,7 @@ redirect_stderr=true

[program:ui]
environment=PORT=80
command=yarn node /opt/ui/server.js
command=yarn node /opt/app/ui/server.js
autorestart=true
startretries=100
stdout_logfile=/dev/fd/1
Expand Down

0 comments on commit 496401f

Please sign in to comment.