Skip to content

Commit 4390aa1

Browse files
committed
build WebApp inside Dockerfile
1 parent de788c7 commit 4390aa1

File tree

5 files changed

+57
-52
lines changed

5 files changed

+57
-52
lines changed

.github/workflows/buildAndRelease.yaml

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,18 @@ jobs:
3636
echo "VERSION=${VERSION}" >> $GITHUB_ENV
3737
echo Using name=$NAME and version=$VERSION
3838
39-
- name: Set up Node.js
40-
uses: actions/setup-node@v4
41-
42-
- name: Set up Helm
43-
uses: azure/setup-helm@v4.2.0
44-
4539
- name: Set up Docker
4640
uses: docker/setup-buildx-action@v2
47-
48-
- name: Install Npm dependencies
49-
run: npm install
50-
51-
- name: Run build script
52-
run: node build.mjs
53-
54-
- name: Build Web app
55-
run: |
56-
cd out
57-
zip -r ../$NAME-$VERSION.zip *
58-
59-
- name: Build Helm package
60-
run: helm package ./helm
61-
41+
42+
- name: Set up Helm
43+
uses: azure/setup-helm@v4.2.0
44+
6245
- name: Build Docker image
6346
run: |
6447
docker build -t ghcr.io/${{ github.repository_owner }}/$NAME:$VERSION .
48+
49+
- name: Build Helm package
50+
run: helm package ./helm
6551

6652
- name: Create GitHub Release
6753
uses: softprops/action-gh-release@v2
@@ -72,7 +58,6 @@ jobs:
7258
prerelease: false
7359
#body_path: CHANGELOG.md
7460
files: |
75-
*.zip
7661
*.tgz
7762
7863
- name: Log in to GitHub Container Registry

Dockerfile

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1+
ARG NODE_VERSION=22
2+
ARG BUSYBOX_VERSION=1.36.1
13
ARG ALPINE_VERSION=3.20
24

3-
FROM alpine:${ALPINE_VERSION} AS builder
5+
#################################################################
6+
####################### NodeJs ##################################
7+
#################################################################
8+
FROM node:${NODE_VERSION}-alpine as webapp
49

5-
ARG BUSYBOX_VERSION=1.36.1
10+
11+
COPY . /webapp
12+
WORKDIR /webapp
13+
14+
RUN npm install && node build.mjs
15+
16+
##################################################################
17+
####################### HTTPD ####################################
18+
##################################################################
19+
FROM alpine:${ALPINE_VERSION} AS webserver
620

721
# Install all dependencies required for compiling busybox
822
RUN apk add gcc musl-dev make perl
923

1024
# Download busybox sources
25+
ARG BUSYBOX_VERSION
1126
RUN wget https://busybox.net/downloads/busybox-${BUSYBOX_VERSION}.tar.bz2 \
1227
&& tar xf busybox-${BUSYBOX_VERSION}.tar.bz2 \
1328
&& mv /busybox-${BUSYBOX_VERSION} /busybox
@@ -23,26 +38,25 @@ RUN make && ./make_single_applets.sh
2338
# Create a non-root user to own the files and run our server
2439
RUN adduser -D static
2540

26-
# Switch to the scratch image
41+
##################################################################
42+
####################### WebApp ###################################
43+
##################################################################
2744
FROM scratch
2845

2946
# Copy over the user
30-
COPY --from=builder /etc/passwd /etc/passwd
31-
32-
# Copy the static binary
33-
COPY --from=builder /busybox/busybox_HTTPD /httpd
47+
COPY --from=webserver /etc/passwd /etc/passwd
3448

3549
# Use our non-root user
3650
USER static
3751
WORKDIR /home/static
3852

39-
# Copy the static website
40-
# Use the .dockerignore file to control what ends up inside the image!
41-
# NOTE: Commented out since this will also copy the .config file
42-
COPY out .
53+
# Copy webserver files
54+
COPY --from=webserver --chown=static /busybox/busybox_HTTPD httpd
55+
# Copy webapp files
56+
COPY --from=webapp --chown=static /webapp/out .
4357

4458
# port httpd runs on
4559
EXPOSE 3000
4660

4761
# Run busybox httpd
48-
CMD ["/httpd", "-f", "-p", "3000"]
62+
CMD ["/home/static/httpd", "-f", "-p", "3000"]

Makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ ARGS = $(foreach a,$($(subst -,_,$1)_args),$(if $(value $a),$a="$($a)"))
66
build:
77
npm install
88
node build.mjs
9-
docker build -t ${NAME}:latest .
10-
@echo "🛠️ - Docker build successful"
119

1210
serve:
13-
npm install
1411
node build.mjs serve
1512

13+
docker_build:
14+
docker build -t ${NAME}:latest .
15+
@echo "🛠️ - Docker build successful"
16+
1617
docker_run: build
1718
@docker run --name ${NAME} -dt --rm --init -p ${PORT}:3000 ${NAME}:latest > /dev/null
1819
@docker container ls

README.md

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,72 @@
22
![GitHub Release](https://img.shields.io/github/v/release/jamowei/spa-simple)
33

44

5-
# 😎 WebApp made simple
5+
# 😎 WebApp made simple 🎉
66
Simple WebApp (SPA) using [esbuild](https://esbuild.github.io/), [jsx-dom](https://www.npmjs.com/package/jsx-dom) and [tailwindcss](https://www.npmjs.com/package/tailwindcss)
77

88
# 📑 Requirements
9-
* [NodeJS](https://nodejs.org/) or similar javascript engine
10-
* [Docker](https://www.docker.com/) or similar container engine
9+
* [Docker](https://www.docker.com/) for building the image
10+
* [NodeJS](https://nodejs.org/) for developing the webapp
1111
* [Helm](https://helm.sh/) for deploying to [Kubernetes](https://kubernetes.io/)
1212

1313
# 🏗️ Structure
1414
All source-files for the app resides in `./src` folder.
1515
Where the `./src/main/app.jsx` file is your main entry point for `esbuild`.
1616
Any custom css goes into the `app.css` file and any static resource (e.g. html, ico, etc.) goes into the `./src/resource` folder. That's all! 😉
1717

18-
# 🛠️ Build App (for Production)
18+
# 💻 Development
1919
Just run `make` or following commands
2020
```
2121
npm install
2222
node build.mjs
23-
docker build -t ${NAME}:latest .
2423
```
25-
Docker image size is `~114 kB`! 🤯
24+
This produces all files for the browser inside the `out` folder.
2625

27-
# 💻 Serve App (for Development)
28-
Just run `make serve` or following commands
26+
If you want to enable [live-reload](https://esbuild.github.io/api/#live-reload),
27+
just run `make serve` or
2928
```
30-
npm install
3129
node build.mjs serve
3230
```
3331

3432
# 🐋 Docker
35-
## Run Docker Container
33+
## 🐋 Build 🛠️
34+
Just run `make docker_build` or following commands
35+
```
36+
docker build -t ${NAME}:latest .
37+
```
38+
Docker image size is `~112 kB`! 🤯
39+
40+
## 🐋 Run
3641
Just run `make docker_run` or following command
3742
```
3843
docker run --name ${NAME} -dt --rm --init -p ${PORT}:3000 ${NAME}:latest
3944
```
4045
Alternatively, you can also use `docker compose up` to start the container, by using the `./docker-compose.yaml`.
4146

42-
## Stop Docker Container
47+
## 🐋 Stop
4348
Just run `make docker_stop` or following command
4449
```
4550
docker container stop ${NAME}
4651
```
4752

4853
Or use `docker compose stop` to stop the container, when using the `./docker-compose.yaml`.
4954

50-
## Docker Image Cleanup
55+
## 🐋 Cleanup 🧹
5156
Just run `make docker_clean` or following command
5257
```
5358
docker image prune
5459
```
5560

5661
# ☸️ Kubernetes
57-
## Run Kubernetes Pod
62+
## ☸️ Install
5863
There is a helm chart provided under `./helm` directory.
5964

6065
Just run `make helm_install` or following command
6166
```
6267
helm upgrade --install --wait --create-namespace --namespace ${NAME} ${NAME} ./helm
6368
```
6469

65-
## Stop Kubernetes Pod
70+
## ☸️ Uninstall
6671
Just run `make helm_uninstall` or following command
6772
```
6873
helm uninstall --namespace ${NAME} ${NAME}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0",
44
"main": "",
55
"scripts": {
6-
"serve": "node esbuild.mjs"
6+
"build": "node esbuild.mjs"
77
},
88
"repository": {
99
"type": "git",

0 commit comments

Comments
 (0)