Skip to content

Commit 759875e

Browse files
Merge pull request #340 from Moustafa-Elgammal/master
Dockerizing, provide production build load balanced with NGINX #332
2 parents fc262e3 + e19ea3e commit 759875e

File tree

9 files changed

+198
-1
lines changed

9 files changed

+198
-1
lines changed

.docker/Dockerfile_dependencies

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Base image
2+
FROM node:18
3+
4+
# Set the working directory in the container
5+
WORKDIR /app
6+
7+
COPY ../package.json ./
8+
9+
# Install app dependencies
10+
RUN npm install -g pnpm
11+
12+
RUN pnpm install
13+
14+

.docker/Dockerfile_develop

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Base image
2+
FROM node:18
3+
4+
RUN npm install -g pnpm
5+
6+
# Set the working directory in the container
7+
WORKDIR /app
8+
9+
COPY .. .
10+
11+
EXPOSE 8080

.docker/Dockerfile_production

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Base image
2+
FROM node:18
3+
4+
# Set the working directory in the container
5+
WORKDIR /app
6+
7+
# copy src
8+
COPY .. .
9+
10+
# install pnpm
11+
RUN npm install -g pnpm
12+
13+
# install initial depenancies
14+
RUN pnpm install
15+
16+
# install production depencancies
17+
RUN pnpm install --production
18+
19+
# build
20+
RUN node build
21+
22+
23+
#pull nginx as webser and load balancer
24+
FROM nginx:stable-alpine3.20-slim
25+
26+
RUN apk update && apk upgrade --no-cache
27+
28+
# copy the build files to nginx entry point
29+
COPY ./public /usr/share/nginx/html

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ VITE_MTPROTO_AUTO=1
1111
VITE_MTPROTO_HAS_HTTP=1
1212
VITE_MTPROTO_HAS_WS=1
1313
VITE_SAFARI_PROXY_WEBSOCKET=
14+
PRODUCTION_BUILD_VERSION=2.1.4
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: Docker Image Build CI
2+
3+
on:
4+
push:
5+
tags:
6+
- 'tag/build-docker-image/*'
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v4
15+
16+
- name: Set up Docker Buildx
17+
uses: docker/setup-buildx-action@v1
18+
19+
- name: Set up Node.js
20+
uses: actions/setup-node@v2
21+
with:
22+
node-version: '14'
23+
24+
- name: Read version from .env
25+
id: env
26+
run: echo "::set-output name=PRODUCTION_BUILD_VERSION::$(grep ^PRODUCTION_BUILD_VERSION= .env | cut -d'=' -f2)"
27+
28+
- name: Build the Docker image for latest
29+
run: docker build -f ./.docker/Dockerfile_production -t ${{ vars.DOCKERHUB_USERNAME}}/tweb:latest .
30+
31+
- name: Build the Docker image for specific version
32+
run: |
33+
version=${{ steps.env.outputs.PRODUCTION_BUILD_VERSION }}
34+
docker build -f ./.docker/Dockerfile_production -t ${{ vars.DOCKERHUB_USERNAME}}/tweb:${version} .
35+
36+
- name: Login to Docker Hub
37+
run: echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u "${{ vars.DOCKERHUB_USERNAME}}" --password-stdin
38+
39+
- name: Push versioned image
40+
run: |
41+
version=${{ steps.env.outputs.PRODUCTION_BUILD_VERSION }}
42+
docker push ${{ vars.DOCKERHUB_USERNAME}}/tweb:${version}
43+
44+
- name: Push latest image
45+
46+
run: |
47+
docker push ${{ vars.DOCKERHUB_USERNAME}}/tweb:latest
48+
49+
50+
test:
51+
needs: build
52+
runs-on: ubuntu-latest
53+
54+
steps:
55+
- name: Checkout code
56+
uses: actions/checkout@v4
57+
58+
- name: Read version from .env
59+
id: env
60+
run: echo "::set-output name=PRODUCTION_BUILD_VERSION::$(grep ^PRODUCTION_BUILD_VERSION= .env | cut -d'=' -f2)"
61+
62+
- name: Pull versioned image
63+
run: |
64+
version=${{ steps.env.outputs.PRODUCTION_BUILD_VERSION }}
65+
docker pull ${{ vars.DOCKERHUB_USERNAME}}/tweb:${version}
66+
67+
- name: Pull latest image
68+
run: docker pull ${{ vars.DOCKERHUB_USERNAME}}/tweb:latest

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,30 @@ Open http://localhost:8080/ in your browser.
1919

2020
Run `node build` to build the minimized production version of the app. Copy `public` folder contents to your web server.
2121

22+
### Running in docker
23+
24+
#### Developing:
25+
* Install dependencies `docker-compose up tweb.dependencies`.
26+
* Run develop container `docker-compose up tweb.develop `.
27+
* Open http://localhost:8080/ in your browser.
28+
29+
#### Production:
30+
* Run `docker-compose up tweb.production -d` nginx image and container to serve the build
31+
* Open http://localhost:80/ in your browser.
32+
33+
34+
I also created an image https://hub.docker.com/r/elgammalx/tweb/tags based on Nginx ready to be deployed.
35+
36+
You can use `docker build -f ./.docker/Dockerfile_production -t {dockerhub-username}/{imageName}:{latest} .` to build your production ready image.
37+
38+
My build use in `docker-compose.yaml` file
39+
```yaml
40+
services:
41+
tweb.production:
42+
image: elgammalx/tweb
43+
ports:
44+
- 80:80
45+
```
2246
2347
### Dependencies
2448
* [BigInteger.js](https://github.com/peterolson/BigInteger.js) ([Unlicense](https://github.com/peterolson/BigInteger.js/blob/master/LICENSE))

SECURITY.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Security Policy
2+
3+
## Supported Versions
4+
5+
Use this section to tell people about which versions of your project are
6+
currently being supported with security updates.
7+
8+
| Version | Supported |
9+
| ------- | ------------------ |
10+
| 5.1.x | :white_check_mark: |
11+
| 5.0.x | :x: |
12+
| 4.0.x | :white_check_mark: |
13+
| < 4.0 | :x: |
14+
15+
## Reporting a Vulnerability
16+
17+
Use this section to tell people how to report a vulnerability.
18+
19+
Tell them where to go, how often they can expect to get an update on a
20+
reported vulnerability, what to expect if the vulnerability is accepted or
21+
declined, etc.

docker-compose.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
version: "3.8"
2+
services:
3+
tweb.dependencies:
4+
container_name: tweb.dependencies
5+
build:
6+
context: .
7+
dockerfile: .docker/Dockerfile_dependencies
8+
volumes:
9+
- "./node_modules:/app/node_modules"
10+
command: pnpm install
11+
12+
tweb.develop:
13+
container_name: tweb.develop
14+
build:
15+
context: .
16+
dockerfile: .docker/Dockerfile_develop
17+
volumes:
18+
- "./:/app"
19+
ports:
20+
- 8080:8080
21+
command: pnpm start
22+
23+
tweb.production:
24+
container_name: tweb.production
25+
build:
26+
context: .
27+
dockerfile: .docker/Dockerfile_production
28+
ports:
29+
- 80:80

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"preinstall": "npx only-allow pnpm",
8-
"start": "vite --force",
8+
"start": "vite --force --host",
99
"serve": "pnpm run build; node server.js",
1010
"build": "pnpm run generate-changelog && vite build",
1111
"test": "vitest",

0 commit comments

Comments
 (0)