Skip to content

Commit 8b32314

Browse files
committed
Made docker containers that deploys the stack correctly and narrowly"
- The nginx image is just an example. - The Dockerfile really tries to narrow down the dependencies included in the backend image - it is an example meant for multiple packages to be built and deployed together, although currently we just build the backend one.
1 parent 976b9e6 commit 8b32314

File tree

7 files changed

+209
-1
lines changed

7 files changed

+209
-1
lines changed

.dockerignore

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Include any files or directories that you don't want to be copied to your
2+
# container here (e.g., local build artifacts, temporary files, etc.).
3+
#
4+
# For more help, visit the .dockerignore file reference guide at
5+
# https://docs.docker.com/go/build-context-dockerignore/
6+
7+
**/.classpath
8+
**/.dockerignore
9+
**/.env
10+
**/.envrc
11+
**/.git
12+
**/.gitignore
13+
**/.gitattributes
14+
**/.project
15+
**/.settings
16+
**/.toolstarget
17+
**/.vs
18+
**/.vscode
19+
**/.next
20+
**/.cache
21+
**/*.*proj.user
22+
**/*.dbmdl
23+
**/*.jfm
24+
**/charts
25+
**/docker-compose*
26+
**/compose*
27+
**/Dockerfile*
28+
**/node_modules
29+
**/*.log
30+
**/obj
31+
**/secrets.dev.yaml
32+
**/values.dev.yaml
33+
**/build
34+
**/build-*
35+
**/dist
36+
LICENSE
37+
README.md

Dockerfile

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# syntax=docker/dockerfile:1
2+
3+
# Comments are provided throughout this file to help you get started.
4+
# If you need more help, visit the Dockerfile reference guide at
5+
# https://docs.docker.com/go/dockerfile-reference/
6+
7+
ARG NODE_VERSION=22.9.0
8+
9+
FROM node:${NODE_VERSION}-alpine AS build
10+
11+
WORKDIR /usr/src/app
12+
13+
# Download dependencies as a separate step to take advantage of Docker's caching.
14+
# Leverage a cache mount to /root/.npm to speed up subsequent builds.
15+
# Leverage a bind mounts to package.json and package-lock.json to avoid having to copy them into
16+
# into this layer.
17+
RUN --mount=type=bind,source=package.json,target=package.json \
18+
--mount=type=bind,source=.npmrc,target=.npmrc \
19+
--mount=type=bind,source=packages/interface/package.json,target=packages/interface/package.json \
20+
--mount=type=bind,source=packages/backend/package.json,target=packages/backend/package.json \
21+
--mount=type=bind,source=packages/frontend/package.json,target=packages/frontend/package.json \
22+
--mount=type=bind,source=package-lock.json,target=package-lock.json \
23+
--mount=type=cache,target=/root/.npm \
24+
npm ci -ws --include-workspace-root
25+
26+
27+
# Copy the rest of the source files into the image.
28+
COPY --link . .
29+
30+
RUN npm run build --ws
31+
32+
FROM node:${NODE_VERSION}-alpine AS production-base
33+
34+
WORKDIR /usr/src/app
35+
36+
# Use production node environment by default.
37+
ENV NODE_ENV=production
38+
39+
COPY --link package.json package.json
40+
41+
# Copy shared interface package on all production images.
42+
COPY --link ./packages/interface ./packages/interface
43+
COPY --link --from=build /usr/src/app/packages/interface/dist ./packages/interface/dist
44+
45+
FROM production-base AS backend
46+
47+
RUN --mount=type=bind,source=packages/backend/package.json,target=packages/backend/package.json \
48+
--mount=type=bind,source=package-lock.json,target=package-lock.json \
49+
--mount=type=cache,target=/root/.npm \
50+
npm ci -w=backend --omit=dev
51+
52+
# Run the application as a non-root user.
53+
USER node
54+
55+
COPY --link ./packages/backend ./packages/backend
56+
COPY --link --from=build /usr/src/app/packages/backend/dist ./packages/backend/dist
57+
58+
59+
ENV PORT=3000
60+
EXPOSE 3000
61+
62+
# Run the application.
63+
CMD ["node", "./packages/backend", "serve"]
64+
65+
FROM nginx:stable-alpine AS frontend
66+
# This is temporary, you would just export the distribution and send it to a CDN as static assets
67+
COPY --link --from=build /usr/src/app/packages/frontend/dist /usr/share/nginx/html
68+
COPY --link ./packages/frontend/nginx/default.conf /etc/nginx/templates/default.conf.template
69+
70+
ENV PORT=8080
71+
EXPOSE 8080

README.Docker.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
### Building and running your application
2+
3+
When you're ready, start your application by running:
4+
`docker compose up --build`.
5+
6+
Your application will be available at http://localhost:3000.
7+
8+
### Deploying your application to the cloud
9+
10+
First, build your image, e.g.: `docker build -t myapp .`.
11+
If your cloud uses a different CPU architecture than your development
12+
machine (e.g., you are on a Mac M1 and your cloud provider is amd64),
13+
you'll want to build the image for that platform, e.g.:
14+
`docker build --platform=linux/amd64 -t myapp .`.
15+
16+
Then, push it to your registry, e.g. `docker push myregistry.com/myapp`.
17+
18+
Consult Docker's [getting started](https://docs.docker.com/go/get-started-sharing/)
19+
docs for more detail on building and pushing.
20+
21+
### References
22+
* [Docker's Node.js guide](https://docs.docker.com/language/nodejs/)

compose.yaml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Comments are provided throughout this file to help you get started.
2+
# If you need more help, visit the Docker compose reference guide at
3+
# https://docs.docker.com/go/compose-spec-reference/
4+
5+
# Here the instructions define your application as a service called "server".
6+
# This service is built from the Dockerfile in the current directory.
7+
# You can add other services your application may depend on here, such as a
8+
# database or a cache. For examples, see the Awesome Compose repository:
9+
# https://github.com/docker/awesome-compose
10+
services:
11+
backend:
12+
build:
13+
context: .
14+
target: backend
15+
environment:
16+
NODE_ENV: production
17+
PORT: 3000
18+
ports:
19+
- 3000:3000
20+
frontend:
21+
build:
22+
context: .
23+
target: frontend
24+
environment:
25+
BACKEND_URL: http://backend:3000
26+
PORT: 8080
27+
ports:
28+
- 8080:8080
29+
30+
# The commented out section below is an example of how to define a PostgreSQL
31+
# database that your application can use. `depends_on` tells Docker Compose to
32+
# start the database before your application. The `db-data` volume persists the
33+
# database data between container restarts. The `db-password` secret is used
34+
# to set the database password. You must create `db/password.txt` and add
35+
# a password of your choosing to it before running `docker-compose up`.
36+
# depends_on:
37+
# db:
38+
# condition: service_healthy
39+
# db:
40+
# image: postgres
41+
# restart: always
42+
# user: postgres
43+
# secrets:
44+
# - db-password
45+
# volumes:
46+
# - db-data:/var/lib/postgresql/data
47+
# environment:
48+
# - POSTGRES_DB=example
49+
# - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
50+
# expose:
51+
# - 5432
52+
# healthcheck:
53+
# test: [ "CMD", "pg_isready" ]
54+
# interval: 10s
55+
# timeout: 5s
56+
# retries: 5
57+
# volumes:
58+
# db-data:
59+
# secrets:
60+
# db-password:
61+
# file: db/password.txt

packages/backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"scripts": {
88
"test": "jest",
99
"dev": "tsc-watch --onSuccess 'node .'",
10-
"start": "npm run build && node . serve",
10+
"start": "npm run build && npm run ",
1111
"build": "npm run build:prod",
1212
"build:debug": "npm run build:debug:node && npm run build:debug:test",
1313
"build:debug:node": "tsc --build ./tsconfig.node.json && tsc-alias -p tsconfig.node.json",

packages/frontend/nginx/default.conf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
server {
2+
listen $PORT;
3+
server_name localhost;
4+
location / {
5+
root /usr/share/nginx/html;
6+
index index.html;
7+
try_files $uri $uri/ /index.html;
8+
}
9+
location /api/ {
10+
proxy_pass $BACKEND_URL;
11+
}
12+
}

packages/frontend/vite.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ import { defineConfig } from 'vite';
44
// https://vitejs.dev/config/
55
export default defineConfig({
66
plugins: [react()],
7+
server: {
8+
proxy: {
9+
'/api': { target: 'http://localhost:3000', changeOrigin: true },
10+
},
11+
},
712
});

0 commit comments

Comments
 (0)