Skip to content

Commit c6b2456

Browse files
committed
build: containerize apps
1 parent 6056dd5 commit c6b2456

File tree

10 files changed

+100
-9
lines changed

10 files changed

+100
-9
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
# Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud
5050
# - run: npx nx-cloud record -- echo Hello World
5151
# Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected
52-
- run: npx nx affected -t lint test build typecheck docker-build
52+
- run: npx nx affected -t lint test build typecheck
5353

5454
- name: Version
5555
env:
@@ -75,3 +75,10 @@ jobs:
7575
npx nx release publish --group packages --yes
7676
env:
7777
THDK_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
78+
79+
# Excluding dependendencies as they should have been already executed and
80+
# we can't rely on the cache to detect the dependencies no longer need to be ran
81+
# since it is busted by writing versions to generated pacakge.json files
82+
- name: Containerize
83+
run: |
84+
npx nx run-many --target docker-build --exclude-task-dependencies

apps/books-api/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ RUN chown -R books-api:books-api .
1919

2020
# You can remove this install step if you build with `--bundle` option.
2121
# The bundled output will include external dependencies.
22-
RUN npm --prefix books-api --omit=dev -f install
22+
RUN --mount=type=cache,target=/root/.npm \
23+
npm --prefix books-api ci
2324

2425
CMD [ "node", "books-api" ]

apps/books-api/project.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929
}
3030
}
3131
},
32-
"docker-build": {
33-
"dependsOn": ["build"],
34-
"command": "docker build -f apps/books-api/Dockerfile . -t books-api"
35-
}
32+
"docker-build": {},
33+
"docker-run": {}
3634
}
3735
}

apps/books-app/Dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This file is generated by Nx.
2+
#
3+
# Build the docker image with `npx nx docker-build books-app`.
4+
# Tip: Modify "docker-build" options in project.json to change docker build args.
5+
#
6+
# Run the container with `docker run -p 3000:3000 -t books-app`.
7+
FROM docker.io/node:lts-alpine
8+
9+
ENV HOST=0.0.0.0
10+
ENV PORT=3000
11+
12+
WORKDIR /app
13+
14+
RUN addgroup --system books-app && \
15+
adduser --system -G books-app books-app
16+
17+
COPY dist/apps/books-app books-app/
18+
RUN chown -R books-app:books-app .
19+
20+
# You can remove this install step if you build with `--bundle` option.
21+
# The bundled output will include external dependencies.
22+
RUN --mount=type=cache,target=/root/.npm \
23+
npm --prefix books-app ci
24+
25+
WORKDIR /app/books-app
26+
CMD [ "npm", "run", "start" ]

apps/books-app/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"@remix-run/serve": "^2.14.0",
1111
"isbot": "^4.4.0",
1212
"react": "^18.2.0",
13-
"react-dom": "^18.2.0"
13+
"react-dom": "^18.2.0",
14+
"ms": "^2.1.3"
1415
},
1516
"devDependencies": {
1617
"@remix-run/dev": "^2.14.0",

apps/books-app/project.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
"generateLockfile": true,
1515
"includeDevDependenciesInPackageJson": false
1616
}
17+
},
18+
"docker-build": {},
19+
"docker-run": {
20+
"options": {
21+
"command": [
22+
"docker run -it --rm --init",
23+
"-p 3002:3002",
24+
"--env API_URL",
25+
"--env PORT",
26+
"books-app"
27+
]
28+
}
1729
}
1830
}
1931
}

apps/books-app/vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default defineConfig({
1212
root: __dirname,
1313
cacheDir: '../../node_modules/.vite/apps/books-app',
1414
server: {
15-
port: 4200,
15+
port: process.env.PORT ? Number(process.env.PORT) : 4200,
1616
host: 'localhost',
1717
},
1818
preview: {

apps/hello-api/Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This file is generated by Nx.
2+
#
3+
# Build the docker image with `npx nx docker-build hello-api`.
4+
# Tip: Modify "docker-build" options in project.json to change docker build args.
5+
#
6+
# Run the container with `docker run -p 3000:3000 -t hello-api`.
7+
FROM docker.io/node:lts-alpine
8+
9+
ENV HOST=0.0.0.0
10+
ENV PORT=3000
11+
12+
WORKDIR /app
13+
14+
RUN addgroup --system hello-api && \
15+
adduser --system -G hello-api hello-api
16+
17+
COPY dist/apps/hello-api hello-api/
18+
RUN chown -R hello-api:hello-api .
19+
20+
# You can remove this install step if you build with `--bundle` option.
21+
# The bundled output will include external dependencies.
22+
RUN --mount=type=cache,target=/root/.npm \
23+
npm --prefix hello-api ci
24+
25+
CMD [ "node", "hello-api" ]

apps/hello-api/project.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
"options": {
3434
"passWithNoTests": true
3535
}
36-
}
36+
},
37+
"docker-build": {},
38+
"docker-run": {}
3739
}
3840
}

nx.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@
8484
"cache": true,
8585
"dependsOn": ["^build"],
8686
"inputs": ["production", "^production"]
87+
},
88+
"docker-build": {
89+
"dependsOn": ["build"],
90+
"executor": "nx:run-commands",
91+
"options": {
92+
"command": "docker build -f {projectRoot}/Dockerfile . -t {projectName}"
93+
}
94+
},
95+
"docker-run": {
96+
"dependsOn": ["docker-build"],
97+
"executor": "nx:run-commands",
98+
"options": {
99+
"command": [
100+
"docker run -it --rm --init",
101+
"-p $PORT:$PORT",
102+
"--env PORT",
103+
"{projectName}"
104+
]
105+
}
87106
}
88107
},
89108
"release": {

0 commit comments

Comments
 (0)