Skip to content

Commit

Permalink
feat(docker): add fullstack docker setup
Browse files Browse the repository at this point in the history
Signed-off-by: Charles Madjeri <[email protected]>
  • Loading branch information
Charles Madjeri committed Dec 5, 2024
1 parent e749213 commit 4243b5e
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 97 deletions.
9 changes: 8 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
VITE_PORT=8081
VITE_PORT=8081

# Server database environment variables
DB_NAME=area
DB_PASSWORD=change-me
DB_HOST=mariadb
DB_PORT=3306
DB_USER=root
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,5 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

ssl/
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ TARGET_MAX_CHAR_NUM=20

.PHONY: start build stop restart reset logs clean help

PROJECT_IMAGES = area-client-web area-client-mobile
PROJECT_IMAGES = area-client-web area-client-mobile area-server mariadb rabbitmq

## Show help
help:
Expand Down
36 changes: 13 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ With AREA, you can create automated workflows that integrate various services an
## Table of Contents

- [Getting Started](#getting-started)
- [Prerequisites](#prerequisites)
- [Installation & Usage](#installation--usage)
- [Prerequisites](#prerequisites)
- [Installation & Usage](#installation--usage)
- [Documentation](#documentation)
- [Requirements](#requirements)
- [Usage](#usage)
- [Requirements](#requirements)
- [Usage](#usage)
- [Tests](#tests)
- [License](#license)
- [Contributors](#contributors)
Expand All @@ -19,50 +19,39 @@ With AREA, you can create automated workflows that integrate various services an

### Prerequisites

- vite js
- go
- docker
- make

### Installation & Usage

<details>
<summary>Click to expand</summary>

1. Clone the repo

```sh
git clone [email protected]:ASM-Studios/AREA.git
```

2. Create .env files

- Run the following command to create private env files

```sh
cp .env.example .env
cp server/.env.server.example server/.env.server
cp client_web/.env.local.example .env.local
cp client_mobile/.env.mobile.example .env.mobile
```

- Fill the .env, .env.web and .env.mobile files

3. Install NPM packages
```sh
cd AREA/client-web
npm install
```
4. Run the project

3. Install Go packages
```sh
cd AREA/server
<TODO>
make start
```

4. Run the project
```sh
cd AREA/client-web
npm run start
```
```sh
cd AREA/server
go run ./...
```
</details>

### Documentation
Expand All @@ -87,6 +76,7 @@ The documentation is automatically built and deployed to GitHub Pages when a pus
You can consult the documentation online at [AREA Documentation](https://asm-studios.github.io/AREA/).

You can build the documentation locally by running the following command:

```sh
cd AREA/docs
make docs
Expand Down
14 changes: 0 additions & 14 deletions client_mobile/.env.mobile.example
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
VITE_PORT=8081
VITE_ENDPOINT=http://localhost:8080

VITE_GOOGLE_CLIENT_ID=
VITE_GOOGLE_CLIENT_SECRET=

VITE_MICROSOFT_CLIENT_ID=

VITE_LINKEDIN_CLIENT_ID=
VITE_LINKEDIN_CLIENT_SECRET=

VITE_SPOTIFY_CLIENT_ID=
VITE_SPOTIFY_CLIENT_SECRET=

# Server URLs
API_URL=http://localhost:8080
WEB_CLIENT_URL=http://localhost:8081
Expand Down
23 changes: 17 additions & 6 deletions client_mobile/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
FROM ghcr.io/cirruslabs/flutter:stable
FROM ghcr.io/cirruslabs/flutter:3.24.5 AS builder

WORKDIR /app

# Fix git permissions and Flutter SDK issues
RUN git config --system --add safe.directory /sdks/flutter && \
git config --system --add safe.directory /app && \
chmod -R 777 /sdks/flutter

# Copy pubspec files first to leverage cache
COPY pubspec.* ./
RUN flutter pub get

# Copy the rest of the source code
COPY . .

# Build arguments
ARG VITE_PORT
ARG VITE_ENDPOINT
ARG VITE_GOOGLE_CLIENT_ID
Expand All @@ -17,10 +30,8 @@ ARG MOBILE_CLIENT_URL
ARG GITHUB_CLIENT_ID
ARG GITHUB_CLIENT_SECRET

COPY . .

RUN flutter pub get
# Build APK
RUN flutter build apk --release

RUN mv build/app/outputs/flutter-apk/app-release.apk build/app/outputs/flutter-apk/client.apk
RUN chmod -R 755 build/app/outputs/flutter-apk/
# Rename the APK
RUN mv build/app/outputs/flutter-apk/app-release.apk build/app/outputs/flutter-apk/client.apk
65 changes: 43 additions & 22 deletions client_web/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
###----------------------- Certificate generation stage -----------------------###
FROM alpine:3.19 AS cert-builder
FROM alpine:3.20.3 AS cert-builder

# Install mkcert dependencies
RUN apk add --no-cache \
curl \
nss \
nss-tools
# Install OpenSSL
RUN apk add --no-cache openssl

# Install mkcert
RUN curl -JLO "https://dl.filippo.io/mkcert/latest?for=linux/amd64" \
&& chmod +x mkcert-v*-linux-amd64 \
&& mv mkcert-v*-linux-amd64 /usr/local/bin/mkcert \
&& mkcert -install \
&& mkcert localhost
# Generate self-signed certificate
RUN mkdir -p /etc/nginx/ssl && \
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/nginx/ssl/private.key \
-out /etc/nginx/ssl/certificate.crt \
-subj "/C=FR/ST=Paris/L=Paris/O=Area/OU=IT/CN=localhost" \
-addext "subjectAltName = DNS:localhost" && \
chmod 644 /etc/nginx/ssl/certificate.crt /etc/nginx/ssl/private.key


###----------------------- Build stage for Node.js application -----------------------###
FROM node:latest AS builder
FROM node:20-alpine AS builder

WORKDIR /app

# Copy package files first to leverage cache
COPY package*.json ./
RUN npm ci

# Copy source files and build arguments
COPY . .

ARG VITE_PORT
ARG VITE_ENDPOINT
ARG VITE_GOOGLE_CLIENT_ID
Expand All @@ -35,20 +41,35 @@ ARG MOBILE_CLIENT_URL
ARG GITHUB_CLIENT_ID
ARG GITHUB_CLIENT_SECRET

COPY ./package*.json ./
RUN npm install
COPY . .

RUN npm run build


###----------------------- Production stage -----------------------###
FROM nginx:alpine AS production
FROM nginx:1.25.3-alpine

# Create non-root user and set up directories
RUN adduser -D nginxuser && \
mkdir -p /usr/share/nginx/html/mobile_builds && \
mkdir -p /etc/nginx/ssl && \
chown -R nginxuser:nginxuser /usr/share/nginx/html && \
chown -R nginxuser:nginxuser /etc/nginx/ssl && \
chown -R nginxuser:nginxuser /var/cache/nginx && \
chown -R nginxuser:nginxuser /var/log/nginx && \
touch /var/run/nginx.pid && \
chown -R nginxuser:nginxuser /var/run/nginx.pid

# Copy SSL certificates with proper permissions
COPY --from=cert-builder --chown=nginxuser:nginxuser /etc/nginx/ssl /etc/nginx/ssl

# Copy built files and configuration
COPY --from=builder --chown=nginxuser:nginxuser /app/dist /usr/share/nginx/html
COPY --chown=nginxuser:nginxuser ./nginx.conf /etc/nginx/conf.d/default.conf

USER nginxuser

COPY --from=builder /app/dist /usr/share/nginx/html
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
RUN mkdir -p /usr/share/nginx/html/mobile_builds
EXPOSE 8081

EXPOSE ${VITE_PORT}
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8081/health || exit 1

CMD ["nginx", "-g", "daemon off;"]
7 changes: 6 additions & 1 deletion client_web/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
server {
listen 8081;
listen 8081 ssl;
server_name localhost;

ssl_certificate /etc/nginx/ssl/certificate.crt;
ssl_certificate_key /etc/nginx/ssl/private.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
Expand Down
5 changes: 0 additions & 5 deletions client_web/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import fs from 'fs'
import path from 'path'

export default defineConfig(({ mode }) => {
Expand All @@ -10,10 +9,6 @@ export default defineConfig(({ mode }) => {
plugins: [react()],
server: {
port: parseInt(env.VITE_PORT) || 8081,
https: {
key: fs.readFileSync(path.resolve(__dirname, 'localhost-key.pem')),
cert: fs.readFileSync(path.resolve(__dirname, 'localhost.pem')),
},
},
resolve: {
alias: {
Expand Down
79 changes: 73 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,67 @@
services:
rabbitmq:
image: rabbitmq:4.0.4-management-alpine
ports:
- "8082:15672"
- "5000:5673"
networks:
- area_network
volumes:
- ./rabbit-mq/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf:ro
healthcheck:
test: [ "CMD", "rabbitmqctl", "status" ]
interval: 5s
timeout: 15s
retries: 5
restart: unless-stopped

mariadb:
image: mariadb:11.4.4
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_DATABASE: ${DB_NAME}
MYSQL_ROOT_HOST: "%"
volumes:
- mariadb_data:/var/lib/mysql
- ./server/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
networks:
- area_network
healthcheck:
test: [ "CMD", "mariadb-admin", "ping", "-h", "localhost", "-u", "root", "-p${DB_PASSWORD}" ]
interval: 10s
timeout: 5s
retries: 5
env_file:
- .env
restart: unless-stopped

area-server:
build:
context: ./server
dockerfile: Dockerfile
ports:
- "8080:8080"
volumes:
- ./ssl:/app/ssl:ro
environment:
DB_HOST: ${DB_HOST}
DB_PORT: ${DB_PORT}
DB_USER: ${DB_USER}
DB_NAME: ${DB_NAME}
DB_PASSWORD: ${DB_PASSWORD}
depends_on:
mariadb:
condition: service_healthy
rabbitmq:
condition: service_healthy
networks:
- area_network
env_file:
- server/.env.server
restart: unless-stopped

area-client-mobile:
build:
context: ./client_mobile
Expand All @@ -19,9 +82,9 @@ services:
- GITHUB_CLIENT_ID
- GITHUB_CLIENT_SECRET
volumes:
- area-client-data:/app/build/app/outputs/flutter-apk
- area_client_data:/app/build/app/outputs/flutter-apk
networks:
- area-network
- area_network
env_file:
- ./client_mobile/.env.mobile

Expand All @@ -47,17 +110,21 @@ services:
ports:
- "${VITE_PORT}:${VITE_PORT}"
volumes:
- area-client-data:/usr/share/nginx/html/mobile_builds
- area_client_data:/usr/share/nginx/html/mobile_builds:ro
depends_on:
- area-client-mobile
- area-server
networks:
- area-network
- area_network
env_file:
- ./client_web/.env.local
restart: unless-stopped

volumes:
area-client-data:
area_client_data:
mariadb_data:


networks:
area-network:
area_network:
driver: bridge
Loading

0 comments on commit 4243b5e

Please sign in to comment.