Skip to content

Commit 39856e8

Browse files
committed
Add Docker deployment scripts and Dockerfile for App Runner alternative deployment
1 parent d6dd82c commit 39856e8

3 files changed

Lines changed: 162 additions & 0 deletions

File tree

Dockerfile.apprunner

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Dockerfile optimized for AWS App Runner
2+
FROM python:3.11-slim
3+
4+
WORKDIR /opt/app
5+
6+
# Install system dependencies
7+
RUN apt-get update && apt-get install -y \
8+
build-essential \
9+
curl \
10+
&& rm -rf /var/lib/apt/lists/*
11+
12+
# Copy requirements first for better caching
13+
COPY requirements.txt .
14+
15+
# Install Python dependencies
16+
RUN pip install --no-cache-dir --upgrade pip && \
17+
pip install --no-cache-dir -r requirements.txt
18+
19+
# Copy application code
20+
COPY . .
21+
22+
# Create necessary directories
23+
RUN mkdir -p data/eea/renewable-energy data/eea/pollution logs reference
24+
25+
# Set environment variables
26+
ENV FLASK_ENV=production
27+
ENV FLASK_DEBUG=0
28+
ENV PYTHONPATH=/opt/app
29+
ENV PYTHONUNBUFFERED=1
30+
ENV PORT=8000
31+
32+
# Create non-root user
33+
RUN useradd --create-home --shell /bin/bash appuser && \
34+
chown -R appuser:appuser /opt/app
35+
USER appuser
36+
37+
# Expose port
38+
EXPOSE 8000
39+
40+
# Health check
41+
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
42+
CMD curl -f http://localhost:8000/health || exit 1
43+
44+
# Run application
45+
CMD ["python", "run_server.py"]

deploy-docker.ps1

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# PowerShell script for Docker deployment to AWS App Runner
2+
param(
3+
[string]$AWSRegion = "ap-southeast-2",
4+
[string]$ECRRepository = "permit-api",
5+
[string]$ImageTag = "latest"
6+
)
7+
8+
Write-Host "🚀 Building and deploying Permit API to AWS App Runner via ECR" -ForegroundColor Green
9+
Write-Host "Region: $AWSRegion" -ForegroundColor Yellow
10+
Write-Host "Repository: $ECRRepository" -ForegroundColor Yellow
11+
12+
# Get AWS Account ID
13+
try {
14+
$AWSAccountID = aws sts get-caller-identity --query Account --output text 2>$null
15+
if ($LASTEXITCODE -ne 0) { throw }
16+
Write-Host "Account: $AWSAccountID" -ForegroundColor Yellow
17+
}
18+
catch {
19+
Write-Host "❌ AWS CLI not configured or not authenticated" -ForegroundColor Red
20+
Write-Host "Please run: aws configure" -ForegroundColor Yellow
21+
exit 1
22+
}
23+
24+
# Create ECR repository if it doesn't exist
25+
Write-Host "📦 Creating ECR repository..." -ForegroundColor Cyan
26+
$repoExists = aws ecr describe-repositories --repository-names $ECRRepository --region $AWSRegion 2>$null
27+
if ($LASTEXITCODE -ne 0) {
28+
aws ecr create-repository --repository-name $ECRRepository --region $AWSRegion
29+
}
30+
31+
# Get ECR login token
32+
Write-Host "🔐 Authenticating Docker with ECR..." -ForegroundColor Cyan
33+
$loginPassword = aws ecr get-login-password --region $AWSRegion
34+
if ($LASTEXITCODE -eq 0) {
35+
echo $loginPassword | docker login --username AWS --password-stdin "$AWSAccountID.dkr.ecr.$AWSRegion.amazonaws.com"
36+
}
37+
38+
# Build Docker image
39+
Write-Host "🔨 Building Docker image..." -ForegroundColor Cyan
40+
docker build -f Dockerfile.apprunner -t "${ECRRepository}:${ImageTag}" .
41+
42+
if ($LASTEXITCODE -ne 0) {
43+
Write-Host "❌ Docker build failed" -ForegroundColor Red
44+
exit 1
45+
}
46+
47+
# Tag image for ECR
48+
$ECRURI = "$AWSAccountID.dkr.ecr.$AWSRegion.amazonaws.com/${ECRRepository}:${ImageTag}"
49+
docker tag "${ECRRepository}:${ImageTag}" $ECRURI
50+
51+
# Push to ECR
52+
Write-Host "📤 Pushing image to ECR..." -ForegroundColor Cyan
53+
docker push $ECRURI
54+
55+
if ($LASTEXITCODE -eq 0) {
56+
Write-Host "✅ Docker image pushed successfully!" -ForegroundColor Green
57+
Write-Host "ECR URI: $ECRURI" -ForegroundColor Yellow
58+
Write-Host ""
59+
Write-Host "Next steps:" -ForegroundColor Green
60+
Write-Host "1. Go to AWS App Runner console" -ForegroundColor White
61+
Write-Host "2. Choose 'Container registry' as source" -ForegroundColor White
62+
Write-Host "3. Use this image URI: $ECRURI" -ForegroundColor Cyan
63+
Write-Host "4. Configure service settings" -ForegroundColor White
64+
} else {
65+
Write-Host "❌ Docker push failed" -ForegroundColor Red
66+
exit 1
67+
}

deploy-docker.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
# Docker deployment script for AWS App Runner
3+
4+
# Configuration
5+
AWS_REGION="ap-southeast-2"
6+
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text 2>/dev/null)
7+
ECR_REPOSITORY="permit-api"
8+
IMAGE_TAG="latest"
9+
10+
echo "🚀 Building and deploying Permit API to AWS App Runner via ECR"
11+
echo "Region: $AWS_REGION"
12+
echo "Account: $AWS_ACCOUNT_ID"
13+
echo "Repository: $ECR_REPOSITORY"
14+
15+
# Check if AWS CLI is configured
16+
if [ -z "$AWS_ACCOUNT_ID" ]; then
17+
echo "❌ AWS CLI not configured or not authenticated"
18+
echo "Please run: aws configure"
19+
exit 1
20+
fi
21+
22+
# Create ECR repository if it doesn't exist
23+
echo "📦 Creating ECR repository..."
24+
aws ecr describe-repositories --repository-names $ECR_REPOSITORY --region $AWS_REGION >/dev/null 2>&1 || \
25+
aws ecr create-repository --repository-name $ECR_REPOSITORY --region $AWS_REGION
26+
27+
# Get ECR login token
28+
echo "🔐 Authenticating Docker with ECR..."
29+
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
30+
31+
# Build Docker image
32+
echo "🔨 Building Docker image..."
33+
docker build -f Dockerfile.apprunner -t $ECR_REPOSITORY:$IMAGE_TAG .
34+
35+
# Tag image for ECR
36+
ECR_URI="$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$ECR_REPOSITORY:$IMAGE_TAG"
37+
docker tag $ECR_REPOSITORY:$IMAGE_TAG $ECR_URI
38+
39+
# Push to ECR
40+
echo "📤 Pushing image to ECR..."
41+
docker push $ECR_URI
42+
43+
echo "✅ Docker image pushed successfully!"
44+
echo "ECR URI: $ECR_URI"
45+
echo ""
46+
echo "Next steps:"
47+
echo "1. Go to AWS App Runner console"
48+
echo "2. Choose 'Container registry' as source"
49+
echo "3. Use this image URI: $ECR_URI"
50+
echo "4. Configure service settings"

0 commit comments

Comments
 (0)