본문 바로가기
개발 (ENG)

Spring Boot + Docker: Deploy to Cloud Run with Auto Script

by 새싹 아빠 2025. 7. 14.

🚀 Deploying a Spring Boot API Server to GCP Cloud Run with Docker

In this post, I’ll walk you through how I deployed my Kotlin-based Spring Boot backend (a Dart Entity Generator API) using Docker and Google Cloud Run. The process is fully automated with a shell script and .env configuration.

🛠️ Environment

  • Framework: Spring Boot (Kotlin)
  • Build tool: Gradle
  • Cloud: Google Cloud Platform (Cloud Run)
  • Container: Docker
  • Automation: .env + deploy.sh

📦 Step 1. Build the JAR

./gradlew clean bootJar

You should see a JAR file like build/libs/your-app-name.jar.

🐳 Step 2. Write the Dockerfile

FROM openjdk:17-jdk-slim
WORKDIR /app
COPY build/libs/your-app-name.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]

🚫 Step 3. Configure .dockerignore

Make sure your JAR isn't excluded from upload:

.gradle/
.git/
**/*.log

⚠️ Do NOT exclude build/ or your JAR will be missing!

 

🧱 Prerequisites to Use gcloud Command

Before deploying to Cloud Run, make sure your local machine is ready to use gcloud CLI. Here are the steps:

1. Install Google Cloud CLI

Download and install from the official page:
👉 https://cloud.google.com/sdk/docs/install

2. Initialize gcloud

gcloud init
  • Sign in with your Google account
  • Choose or create a GCP project
  • Set default region and configuration

3. Enable Required APIs

gcloud services enable run.googleapis.com
gcloud services enable cloudbuild.googleapis.com

4. Set Your GCP Project ID

gcloud config set project [YOUR_PROJECT_ID]

5. Set Default Region (Optional but Recommended)

gcloud config set run/region asia-northeast3
Example: asia-northeast3 is the Seoul region
 

☁️ Step 4. Submit Build to Cloud Build

gcloud builds submit --tag gcr.io/<PROJECT_ID>/<IMAGE_NAME>

Most errors at this stage are related to missing JARs or Docker context issues.

🚀 Step 5. Deploy to Cloud Run

gcloud run deploy <IMAGE_NAME> \
  --image gcr.io/<PROJECT_ID>/<IMAGE_NAME> \
  --platform managed \
  --region asia-northeast3 \
  --allow-unauthenticated

You’ll get a public endpoint like:

https://your-app-xxxxx.a.run.app

⚙️ Step 6. Automate with deploy.sh

Create a .env file:

PROJECT_ID=your-gcp-project-id
IMAGE_NAME=your-app-name
REGION=asia-northeast3

Then create the deploy.sh script:

#!/bin/bash
set -e

if [ -f .env ]; then
  export $(grep -v '^#' .env | xargs)
else
  echo "❌ .env file is missing."
  exit 1
fi

IMAGE_PATH="gcr.io/$PROJECT_ID/$IMAGE_NAME"

echo "✅ 1. Building JAR"
./gradlew clean bootJar

echo "✅ 2. Building & pushing Docker image"
gcloud builds submit --tag "$IMAGE_PATH"

echo "✅ 3. Deploying to Cloud Run"
gcloud run deploy "$IMAGE_NAME" \
  --image "$IMAGE_PATH" \
  --platform managed \
  --region "$REGION" \
  --allow-unauthenticated

echo "🎉 Done! Your service URL:"
gcloud run services describe "$IMAGE_NAME" \
  --platform managed \
  --region "$REGION" \
  --format "value(status.url)"

Make it executable and run:

chmod +x deploy.sh
./deploy.sh

✅ Verify the Deployment

  • Access your API via the URL (e.g. https://.../api/endpoint)
  • Check logs in Google Cloud Console → Cloud Run → Logs

💰 Cloud Run Cost & Billing Details

Cloud Run follows a pay-per-use model. This means:

  • No traffic = No charges
  • Great for side projects or small APIs

📦 Free Tier (As of 2025)

  • 2 million requests per month
  • 360,000 vCPU-seconds per month
  • 180,000 GiB-seconds of memory
  • 1 GB internet egress traffic

You can run small backend services or frontend APIs almost for free under these limits.
This is ideal for personal tools, prototypes, or hobby projects.

⚠️ When Might You Get Charged?

  • High traffic (thousands of requests per day)
  • Long-running processes or heavy CPU usage
  • Large file uploads/downloads (high egress bandwidth)

🔗 Official Pricing

Check the most up-to-date pricing here:
https://cloud.google.com/run/pricing

💡 Tip

You can also set up a budget alert in the Google Cloud Console to be notified before costs increase.

🧘‍♂️ Final Thoughts

At first, I ran into issues with .dockerignore, missing JARs, and path errors. But once scripted, the entire deployment became seamless.

Cloud Run makes it incredibly easy to host and scale an API server with HTTPS and custom domain support—without managing any infrastructure manually.


🔍 Keywords

  • Spring Boot Cloud Run deployment
  • GCP Cloud Run tutorial 2025
  • Spring Boot Docker deployment
  • Automated deploy.sh script example
  • Kotlin backend API on Cloud Run