Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/dockerfile #59

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.github
build
coverage
data
docs
home
node_modules
test
.eslintrc
docker-compose*
indocker
vitest.config.ts
15 changes: 15 additions & 0 deletions .github/workflows/docker-image-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Build and publish docker image

on:
push:
tags:
- 'v*'

jobs:
build:
uses: shlinkio/github-actions/.github/workflows/docker-publish-image.yml@main
secrets: inherit
with:
image-name: shlinkio/shlink-dashboard
version-arg-name: VERSION
platforms: 'linux/arm64/v8,linux/amd64'
35 changes: 35 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
FROM node:22.2-alpine as builder
ARG VERSION="latest"
ENV VERSION ${VERSION}

COPY . /shlink-dashboard
WORKDIR /shlink-dashboard
# Install dev dependencies and build project
RUN npm ci && node --run build


FROM node:22.2-alpine
ARG UID=101
ARG VERSION="latest"
ENV VERSION ${VERSION}
LABEL maintainer="Alejandro Celaya <alejandro@alejandrocelaya.com>"
ENV NODE_ENV "production"

USER root
COPY --from=builder /shlink-dashboard/build /shlink-dashboard
COPY package.json /shlink-dashboard/package.json
COPY package-lock.json /shlink-dashboard/package-lock.json
COPY LICENSE /shlink-dashboard/LICENSE
COPY README.md /shlink-dashboard/README.md

WORKDIR /shlink-dashboard
RUN npm ci --omit dev
RUN mkdir data && chown $UID:0 data

# Expose default port
EXPOSE 3005

# Switch to non-privileged UID as the last step
USER $UID

ENTRYPOINT ["node", "./server.js"]
1 change: 1 addition & 0 deletions app/utils/env.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type DbEngine = typeof supportedDbEngines[number];

const envVariables = z.object({
NODE_ENV: z.enum(['production', 'development', 'test']).optional(),
SHLINK_DASHBOARD_PORT: z.number().optional().default(3005),

// Database connection options
SHLINK_DASHBOARD_DB_DRIVER: z.enum(supportedDbEngines).optional(),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license": "MIT",
"type": "module",
"scripts": {
"build": "react-router build",
"build": "react-router build && tsc --project tsconfig.build-server.json",
"test": "vitest run",
"test:ci": "npm run test -- --coverage",
"lint": "npm run lint:js",
Expand Down
11 changes: 7 additions & 4 deletions server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createRequestHandler } from '@react-router/express';
import express from 'express';
import { serverContainer } from './app/container/container.server';
import { isProd } from './app/utils/env.server';
import { env, isProd } from './app/utils/env.server';

const viteDevServer = isProd()
? null
Expand All @@ -16,16 +16,19 @@ const app = express();
app.use(
viteDevServer
? viteDevServer.middlewares
: express.static('build/client'),
: express.static('client'),
);

const build = viteDevServer
? () => viteDevServer.ssrLoadModule('virtual:react-router/server-build')
// @ts-expect-error This code branch is used only when that file is built
: await import('./build/server/index.js');
: await import('./server/index.js');

// Fork entity manager on every request
app.use(serverContainer.emForkMiddleware);
app.all('*', createRequestHandler({ build }));

app.listen(3005, () => console.log('App listening on http://localhost:3005'));
app.listen(
env.SHLINK_DASHBOARD_PORT,
() => console.log(`App listening on http://localhost:${env.SHLINK_DASHBOARD_PORT}`),
);
8 changes: 8 additions & 0 deletions tsconfig.build-server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"include": ["./server.ts"],
"compilerOptions": {
"noEmit": false,
"outDir": "./build"
}
}