Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: docker build & push files #19

Merged
merged 4 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
../node_modules
**/node_modules/
.git
dist
19 changes: 19 additions & 0 deletions docker/dev/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Server Configuration
API_PORT=3377 # Port for the API server

# Database Configuration (for drizzle-orm and pg)
DB_USER=postgres
DB_PASSWORD=s3cH9KK1Lo0opzPo
DB_NAME=postgres
DB_EXPOSED_PORT=5232

# RabbitMQ Configuration
RABBITMQ_EXPOSED_PORT=5672 # Exposed AMQP port
RABBITMQ_MGMT_EXPOSED_PORT=15672 # Exposed management UI port
RABBITMQ_USER=guest # RabbitMQ username
RABBITMQ_PASSWORD=guest # RabbitMQ password
RABBITMQ_VHOST=/ # Virtual host (default is /)

# Optional: Application-specific settings
APP_NAME=credential-showcase-api # Example: App identifier
LOG_LEVEL=info # Example: Logging level (info/debug/error)
112 changes: 112 additions & 0 deletions docker/dev/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Credential Showcase Docker Build

This repository contains Docker configurations for the Credential Showcase system, which consists of an API server and a Traction adapter that communicate via RabbitMQ message broker.

## Components

- **credential-showcase-api-server**: Main API service that connects to PostgreSQL
- **credential-showcase-traction-adapter**: Adapter service that integrates with the Traction network
- **PostgreSQL**: Database for persistent storage
- **RabbitMQ**: Message broker for service communication

## Setup Instructions

### Prerequisites

- Docker and Docker Compose

### Configuration

1. Copy the example environment file to create your configuration:

```bash
cp .env.example .env
```

2. Modify the `.env` file with your desired configuration:

```
# Server Configuration
API_PORT=3377 # Port for the API server

# Database Configuration
DB_HOST=postgres
DB_PORT=5232
DB_USER=postgres
DB_PASSWORD=s3cH9KK1Lo0opzPo
DB_NAME=postgres

# RabbitMQ Configuration
RABBITMQ_HOST=rabbitmq
RABBITMQ_PORT=5672
RABBITMQ_MGMT_PORT=15672
RABBITMQ_USER=guest
RABBITMQ_PASSWORD=guest
RABBITMQ_VHOST=/

# Application settings
APP_NAME=credential-showcase-api
LOG_LEVEL=info
```

## Building and Running

Build and start the services using Docker Compose:

```bash
docker-compose up -d
```

This will start:
- The API server accessible on port defined in your `.env` file
- The Traction adapter service
- PostgreSQL database with persistent storage
- RabbitMQ with management interface

## Network Configuration

The system uses two isolated Docker networks:
- `messagebroker_net`: For RabbitMQ communication
- `db_net`: For database access

## Volumes

- `postgres_data`: Persistent volume for PostgreSQL data

## Pushing Images to Registry

### Prerequisites for Image Push

- Bash shell
- jq utility installed
- Access to the target Docker registry

### Push Process

To push the images to a Docker registry:

1. Ensure you have proper credentials and access to the registry
2. Run the push script:

```bash
./push.sh
```

The script:
- Checks that API server and Traction adapter versions match
- Tags images with the appropriate version number from package.json
- Pushes images to the configured registry (default: sphereonregistry.azurecr.io)

## Development Notes

- The Dockerfiles use a multi-stage build process for optimized image size
- The system uses pnpm for package management
- Environment variables control most aspects of the configuration

## Troubleshooting

- Check container logs: `docker-compose logs [service-name]`
- Verify network connectivity between services
- Ensure RabbitMQ credentials are correct
- Check PostgreSQL connection parameters
- If push script fails, verify your Docker registry credentials and connection
22 changes: 22 additions & 0 deletions docker/dev/credential-showcase-api-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Build stage
FROM node:20-bookworm AS builder
SHELL ["/bin/bash", "-c"]
ENV SHELL=bash
ENV PNPM_HOME=/usr/local/share/pnpm
ENV PATH=$PNPM_HOME:$PATH
RUN npm -g install pnpm && pnpm setup && source /root/.bashrc && pnpm self-update && pnpm setup && pnpm config set store-dir "/usr/local/share/pnpm/store/v10" --global

WORKDIR /build
COPY . /build
RUN rm -rf $(pnpm store path) && pnpm install -g rimraf typescript tslib
RUN pnpm install
RUN pnpm build
RUN pnpm deploy /deploy --filter credential-showcase-api-server --prod

# Runtime stage
FROM node:20-bookworm-slim
WORKDIR /app
COPY --from=builder /deploy /app
ENV NODE_ENV=production
RUN echo PORT=3000 > .env
CMD ["node", "dist/index.js"]
22 changes: 22 additions & 0 deletions docker/dev/credential-showcase-traction-adapter/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Build stage
FROM node:20-bookworm AS builder
SHELL ["/bin/bash", "-c"]
ENV SHELL=bash
ENV PNPM_HOME=/usr/local/share/pnpm
ENV PATH=$PNPM_HOME:$PATH
RUN npm -g install pnpm && pnpm setup && source /root/.bashrc && pnpm self-update && pnpm setup && pnpm config set store-dir "/usr/local/share/pnpm/store/v10" --global

WORKDIR /build
COPY . /build
RUN rm -rf $(pnpm store path) && pnpm install -g rimraf typescript tslib turbo
RUN pnpm install
RUN pnpm build
RUN pnpm deploy /deploy --filter credential-showcase-traction-adapter --prod

# Runtime stage
FROM node:20-bookworm-slim
WORKDIR /app
COPY --from=builder /deploy /app
ENV NODE_ENV=production
RUN echo PORT=3000 > .env
CMD ["node", "dist/index.js"]
93 changes: 93 additions & 0 deletions docker/dev/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
services:
credential-showcase-api-server:
build:
context: ../..
dockerfile: ./docker/dev/credential-showcase-api-server/Dockerfile
restart: unless-stopped
networks:
- api_net
- messagebroker_net
- db_net
environment:
# Only include variables this service needs
- NODE_PORT=3000
# - DATABASE_URL=${DATABASE_URL} uncomment when using URL instead of vars above
- DB_HOST=postgres
- DB_PORT=${DB_PORT:-5432}
- DB_USER=${DB_USER}
- DB_PASSWORD=${DB_PASSWORD}
- DB_NAME=${DB_NAME}
- APP_NAME=${APP_NAME}
- LOG_LEVEL=${LOG_LEVEL}
depends_on:
- postgres # Ensure Postgres starts first
ports:
- "${API_PORT}:3000" # remove when enabling proxy

credential-showcase-traction-adapter:
build:
context: ../..
dockerfile: ./docker/dev/credential-showcase-traction-adapter/Dockerfile
restart: unless-stopped
networks:
- messagebroker_net

environment:
- NODE_PORT=3000
- RABBITMQ_HOST=rabbitmq
- RABBITMQ_PORT=5672
- RABBITMQ_USER=${RABBITMQ_USER}
- RABBITMQ_PASSWORD=${RABBITMQ_PASSWORD}
- RABBITMQ_VHOST=${RABBITMQ_VHOST}
- APP_NAME=${APP_NAME}
- LOG_LEVEL=${LOG_LEVEL}
depends_on:
- rabbitmq # Ensure RabbitMQ starts first

postgres:
image: postgres:16
restart: unless-stopped
networks:
- db_net
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
ports:
- "${DB_EXPOSED_PORT}:5432" # Expose Postgres port, default 5432
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD", "pg_isready", "-U", "${DB_USER}"]
interval: 10s
timeout: 5s
retries: 5

rabbitmq:
image: rabbitmq:4-management # Using RabbitMQ 4.x with management plugin
restart: unless-stopped
networks:
- messagebroker_net
environment:
- RABBITMQ_DEFAULT_USER=${RABBITMQ_USER}
- RABBITMQ_DEFAULT_PASS=${RABBITMQ_PASSWORD}
ports:
- "${RABBITMQ_EXPOSED_PORT:-5672}:5672"
- "${RABBITMQ_MGMT_EXPOSED_PORT:-15672}:15672"

volumes:
postgres_data: # Named volume for Postgres data persistence

networks:
api_net:
driver: bridge
name: api_net
internal: false
messagebroker_net:
driver: bridge
name: messagebroker_net
internal: false # set to false to be able to expose for debugging
db_net:
driver: bridge
name: db_net
internal: false # set to false to be able to expose for debugging
15 changes: 15 additions & 0 deletions docker/dev/push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

source ./setup-env.sh

# Push API server image
docker tag ${API_SERVER_IMAGE}:latest ${DOCKER_REGISTRY}/${TAGGED_API_SERVER_IMAGE}:${PACKAGE_VERSION}
docker push ${DOCKER_REGISTRY}/${TAGGED_API_SERVER_IMAGE}:${PACKAGE_VERSION}
docker tag ${API_SERVER_IMAGE}:latest ${DOCKER_REGISTRY}/${TAGGED_API_SERVER_IMAGE}:latest
docker push ${DOCKER_REGISTRY}/${TAGGED_API_SERVER_IMAGE}:latest

# Push tract adapter image
docker tag ${TRACTION_ADAPTER_IMAGE}:latest ${DOCKER_REGISTRY}/${TAGGED_TRACTION_ADAPTER_IMAGE}:${PACKAGE_VERSION}
docker push ${DOCKER_REGISTRY}/${TAGGED_TRACTION_ADAPTER_IMAGE}:${PACKAGE_VERSION}
docker tag ${TRACTION_ADAPTER_IMAGE}:latest ${DOCKER_REGISTRY}/${TAGGED_TRACTION_ADAPTER_IMAGE}:latest
docker push ${DOCKER_REGISTRY}/${TAGGED_TRACTION_ADAPTER_IMAGE}:latest
31 changes: 31 additions & 0 deletions docker/dev/setup-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
set -euo pipefail

# Ensure jq is installed
if ! command -v jq >/dev/null; then
echo "Error: jq is not installed. Please install jq (e.g., from https://stedolan.github.io/jq/download/) and ensure it is available in your PATH." >&2
exit 1
fi


DOCKER_REGISTRY="${DOCKER_REGISTRY:-sphereonregistry.azurecr.io}"

# Extract versions from package.json files.
API_VERSION=$(jq -r .version ../../apps/credential-showcase-api-server/package.json)
ADAPTER_VERSION=$(jq -r .version ../../packages/credential-showcase-traction-adapter/package.json)

# Check that both versions match.
if [ "$API_VERSION" != "$ADAPTER_VERSION" ]; then
echo "Error: Version mismatch - API server version ($API_VERSION) does not match traction adapter version ($ADAPTER_VERSION)" >&2
exit 1
fi

export PACKAGE_VERSION="$API_VERSION"

# Source image names.
export API_SERVER_IMAGE="dev-credential-showcase-api-server"
export TRACTION_ADAPTER_IMAGE="dev-credential-showcase-traction-adapter"

# Remove the "dev-" prefix when tagging.
export TAGGED_API_SERVER_IMAGE="${API_SERVER_IMAGE#dev-}"
export TAGGED_TRACTION_ADAPTER_IMAGE="${TRACTION_ADAPTER_IMAGE#dev-}"
21 changes: 21 additions & 0 deletions docker/prod/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Server Configuration
API_PORT=3377 # Port for the API server

# COMPOSE_PROFILES=traefik # uncomment to enable reverse proxy
API_PUBLIC_HOSTS=Host(`api.bc.demo.sphereon.com`) # see https://doc.traefik.io/traefik/routing/routers/#rule

# Database Configuration (for drizzle-orm and pg)
DB_USER=postgres
DB_PASSWORD=s3cH9KK1Lo0opzPo
DB_NAME=postgres

# RabbitMQ Configuration
RABBITMQ_USER=guest # RabbitMQ username
RABBITMQ_PASSWORD=guest # RabbitMQ password
RABBITMQ_VHOST=/ # Virtual host (default is /)
RABBITMQ_EXPOSED_PORT=5672 # Exposed port
RABBITMQ_MGMT_EXPOSED_PORT=15672 # Exposed management UI port

# Optional: Application-specific settings
APP_NAME=credential-showcase-api # Example: App identifier
LOG_LEVEL=info # Example: Logging level (info/debug/error)
Loading