This guide provides a complete walkthrough for developing a containerized application locally with Docker and deploying it to Google Cloud Run. It's designed to be a general-purpose guide, using placeholders for project-specific names.
docker-compose is perfect for local development. It reads a docker-compose.yml file to start your app and any other services it needs (like a database). This setup often uses a development-focused Dockerfile with tools like hot-reloading.
# Start the application in the backgrounddocker compose up -d# View logsdocker compose logs -f# Stop the applicationdocker compose down
The local development loop looks like this:
graph TD
A[Write/Modify Code] --> B{Run `docker compose up`};
B --> C[Test Application Locally in Browser];
C -->|Looks good?| D[Stop with `docker compose down`];
C -->|Needs changes?| A;
D --> E[Ready for Production Deployment];
REPO_NAME: The name of the Artifact Registry repository you created.
IMAGE_NAME: The name of your application.
TAG: A version identifier. While latest is common, it's better to use a Git commit hash (git rev-parse --short HEAD) or a semantic version (e.g., v1.2.1) for traceability.
# Best practice: use an environment variable for the full image nameexport IMAGE_NAME="us-central1-docker.pkg.dev/YOUR_PROJECT_ID/YOUR_REPO_NAME/YOUR_APP_NAME:$(git rev-parse --short HEAD)"# Build the imagedocker build -t $IMAGE_NAME .
This command tells Cloud Run to create a new revision for your service using the image you just pushed.
New vs. Existing Deployment: The gcloud run deploy command is smart. If a service with the name YOUR_SERVICE_NAME already exists, it updates it. If it doesn't exist, it creates it. You don't need a different command.
gcloud run deploy YOUR_SERVICE_NAME \ --image=$IMAGE_NAME \ --region=us-central1 \ --platform=managed \ --allow-unauthenticated # ... add other flags for memory, CPU, etc. as needed
graph TD
subgraph "Local Machine"
A[1. Code Changes] --> B[2. `docker compose up` for local testing];
B --> C[3. Commit to Git];
end
subgraph "Build & Push"
C --> D[4. Build Production Docker Image <br> `docker build -t ...`];
D --> E[5. Push Image to Artifact Registry <br> `docker push ...`];
end
subgraph "Google Cloud"
E --> F[6. Deploy New Revision to Cloud Run <br> `gcloud run deploy ...`];
F --> G[Live Application Updated];
end
I hope this guide clarifies the entire process for you. It's a powerful and repeatable workflow once you get the hang of it!