Skip to content

Commit

Permalink
feat: docker image (#6)
Browse files Browse the repository at this point in the history
* feat: docker image

* chore: fix derp
  • Loading branch information
webbertakken authored Apr 18, 2024
1 parent 8bb31cb commit 441d5a2
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 12 deletions.
9 changes: 9 additions & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"version": "0.2",
"ignorePaths": [],
"dictionaryDefinitions": [],
"dictionaries": [],
"words": ["hass"],
"ignoreWords": [],
"import": []
}
14 changes: 14 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.dockerignore
.editorconfig
.env
.git
.github
.gitignore
.idea
.yarn
.vscode
coverage*
tools
LICENSE
README.md
node_modules
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
/previous_deploy.tar.gz
/.yarn
/.idea
/.vscode
/.env*
!/.env*.dist
Binary file modified bun.lockb
Binary file not shown.
8 changes: 0 additions & 8 deletions cspell.config.yaml

This file was deleted.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
"type": "module",
"scripts": {
"dev": "bun --hot --watch src/main.ts",
"build": "bun build src/main.ts --target=bun --outfile=dist/server.js",
"start": "bun run dist/server.js",
"build": "docker build . -f tools/docker/Dockerfile -t automation-prod",
"build:dist": "bun build src/main.ts --target=bun --outfile=dist/server.js",
"start": "docker run --env-file .env automation-prod",
"test": "vitest",
"coverage": "vitest --coverage",
"lint": "eslint src/ --ext .js,.ts,.tsx,.mts --max-warnings 0",
"format": "prettier --write .",
"fix": "bun run format && bun run lint --fix",
"typecheck": "tsc --noEmit",
"types": "bun --env-file .env type-writer",
"prepare": "husky install"
"prepare": "husky install || true"
},
"trustedDependencies": [
"husky"
Expand All @@ -28,7 +29,7 @@
],
"*.@(ts|tsx|mts)": "bash -c 'bun tsc --skipLibCheck --noEmit'",
"*.@(ts|tsx|mts|js|jsx|mjs|cjs)": [
"bun eslint --max-warnings 0",
"eslint --max-warnings 0",
"vitest related --run"
],
"*.@(ts|tsx|mts|js|jsx|mjs|cjs|json|jsonc|json5|md|mdx|yaml|yml)": "prettier --write"
Expand Down
80 changes: 80 additions & 0 deletions tools/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
###########################################################
## ##
# # Base ##
## ##
###########################################################

# see all versions at https://hub.docker.com/r/oven/bun/tags
ARG BUN_VERSION=1

# Official Bun image
FROM oven/bun:${BUN_VERSION} as base

# Bun uses NODE_ENV for backward compatibility with Node
ENV NODE_ENV="production"

# Simplicity first
WORKDIR /app

###########################################################
## ##
## BUILDER ##
## ##
###########################################################

FROM base as builder

# Act as CI system: no interactive tty, no stdin/stdout, no watch processes
ENV CI="true"

# Copy only relevant files for dev and prod
COPY package.json bun.lockb /app/prod/
COPY package.json bun.lockb tsconfig.json vitest.config.ts .prettier* .eslint* .cspell.json /app/dev/

# Copy source code
COPY src/ /app/dev/src/

# Build and pre-flight checks
RUN cd /app/dev \
&& bun install --frozen-lockfile \
# Note: `--watch false` is a workaround for https://github.com/vitest-dev/vitest/issues/1288
&& bun test --watch false \
&& bun run prettier --check . \
&& bun run lint \
# Note: Can not do typecheck unless we pass in credentials during build time
# && bun run types \
# && bun run typecheck \
&& bun run build:dist

# Prod deps only
RUN cd /app/prod \
&& bun install --production --frozen-lockfile

###########################################################
## ##
## PRODUCTION ##
## ##
###########################################################

FROM base as prod

# Open Container Initiative (OCI) labels
LABEL org.opencontainers.image.title="Automation Standalone" \
org.opencontainers.image.description="This image contains an end-users automations application that communicates directly with a HomeAssistant instance" \
org.opencontainers.image.version="1.0.0" \
org.opencontainers.image.url="https://github.com/digital-alchemy/automation-standalone" \
org.opencontainers.image.documentation="https://docs.digital-alchemy.app/" \
org.opencontainers.image.source="https://github.com/digital-alchemy/automation-standalone" \
org.opencontainers.image.vendor="Digital Alchemy" \
org.opencontainers.image.authors="Webber Takken <webber@takken.io>" \
org.opencontainers.image.licenses="MIT"

# Copy the distributable files and production specific dependencies
COPY --from=builder /app/dev/dist .
COPY --from=builder /app/prod/package.json .
COPY --from=builder /app/prod/node_modules node_modules

# Run the app
USER bun
EXPOSE 3000
ENTRYPOINT [ "bun", "run", "server.js" ]

0 comments on commit 441d5a2

Please sign in to comment.