arrow_back

Deploy a static site with Caddy V2 on Google Cloud Run

Login Gabung
Dapatkan akses ke 700+ lab dan kursus

Deploy a static site with Caddy V2 on Google Cloud Run

Lab 30 menit universal_currency_alt 1 Kredit show_chart Pengantar
info Lab ini mungkin menggabungkan alat AI untuk mendukung pembelajaran Anda.
Dapatkan akses ke 700+ lab dan kursus

gem-cloud-run-caddy-website

Google Cloud self-paced labs logo

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

  1. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. The output contains a line that declares the PROJECT_ID for this session:

Your Cloud Platform project in this session is set to YOUR_PROJECT_ID

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:
gcloud auth list
  1. Click Authorize.

  2. Your output should now look like this:

Output:

ACTIVE: * ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:
gcloud config list project

Output:

[core] project = <project_ID>

Example output:

[core] project = qwiklabs-gcp-44776a13dea667a6 Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Overview

In this lab, you'll learn how to deploy a static website using Caddy V2 on Google Cloud Run. Caddy is a powerful, enterprise-ready, open source web server with automatic HTTPS. We'll containerize Caddy with your static website and deploy it to Cloud Run using Artifact Registry for container image storage.

Task 1. Set up your environment

Configure your Google Cloud environment for this lab.

  1. Set your Project ID. Replace with your Project ID.
gcloud config set project {{{ project_0.project_id | "PROJECT_ID" }}} Note:
This command sets your active project identity.
  1. Set your default region. Replace with your desired region (e.g., us-central1).
gcloud config set run/region {{{ project_0.default_region | "REGION" }}} Note:
This command sets your active cloud run region.
  1. Enable the Cloud Run, Artifact Registry, and Cloud Build APIs.
gcloud services enable run.googleapis.com artifactregistry.googleapis.com cloudbuild.googleapis.com Note:
This command enables the necessary APIs for this lab.

Task 2. Create an Artifact Registry repository

Create a Docker repository in Artifact Registry to store the Caddy container image.

  1. Create a Docker repository named 'caddy-repo' in Artifact Registry. Replace with your desired region.
gcloud artifacts repositories create caddy-repo --repository-format=docker --location={{{ project_0.default_region | "REGION" }}} --description="Docker repository for Caddy images" Note:
This command creates an Artifact Registry repository.

Task 3. Create the static website and Caddyfile

Create a simple static website and configure Caddy to serve it.

  1. Create an index.html file with the following content:
<html> <head> <title>My Static Website</title> </head> <body> <div>Hello from Caddy on Cloud Run!</div> <p>This website is served by Caddy running in a Docker container on Google Cloud Run.</p> </body> </html> Note:
This is the HTML content for your static website.
  1. Create a Caddyfile with the following content:
:8080 root * /usr/share/caddy file_server Note:
This Caddyfile configures Caddy to serve files from the `/usr/share/caddy` directory on port 8080.

Task 4. Create the Dockerfile

Define the Docker image for Caddy and your static website.

  1. Create a Dockerfile with the following content:
FROM caddy:2-alpine WORKDIR /usr/share/caddy COPY index.html . COPY Caddyfile /etc/caddy/Caddyfile Note:
This Dockerfile uses the official Caddy Alpine image, sets the working directory, and copies your website and Caddyfile.

Task 5. Build and push the Docker image

Build the Docker image and push it to Artifact Registry.

  1. Build the Docker image. Replace and with your region and project ID.
docker build -t {{{ project_0.default_region | "REGION" }}}-docker.pkg.dev/{{{ project_0.project_id | "PROJECT_ID" }}}/caddy-repo/caddy-static:latest . Note:
This command builds the Docker image and tags it with the Artifact Registry repository URL.
  1. Push the Docker image to Artifact Registry.
docker push {{{ project_0.default_region | "REGION" }}}-docker.pkg.dev/{{{ project_0.project_id | "PROJECT_ID" }}}/caddy-repo/caddy-static:latest Note:
This command pushes the Docker image to Artifact Registry.

Task 6. Deploy to Cloud Run

Deploy the container image to Cloud Run.

  1. Deploy the service to Cloud Run. Replace and with your region and project ID.
gcloud run deploy caddy-static --image {{{ project_0.default_region | "REGION" }}}-docker.pkg.dev/{{{ project_0.project_id | "PROJECT_ID" }}}/caddy-repo/caddy-static:latest --platform managed --allow-unauthenticated Note:
This command deploys the Docker image to Cloud Run and allows unauthenticated access.
  1. When prompted, confirm the service name as caddy-static and allow unauthenticated invocations.
Note:
This configures the service name and permissions.
  1. Note the service URL provided by Cloud Run.
Note:
This is the URL where your static website is accessible.

Task 7. Access your website

Access the deployed website through the Cloud Run service URL.

  1. Open the Cloud Run service URL in your web browser.
Note:
Verify that your static website is displayed correctly.

Congratulations!

You have successfully deployed a static website using Caddy V2 on Google Cloud Run! You learned how to containerize a web server with your static content, store the image in Artifact Registry, and deploy it to Cloud Run for automatic scaling and management. Next steps include exploring Caddy's advanced features and integrating Cloud Run with other Google Cloud services.

Additional Resources

Manual Last Updated Jun 23, 2025

Lab Last Tested Jun 23, 2025

Sebelum memulai

  1. Lab membuat project dan resource Google Cloud untuk jangka waktu tertentu
  2. Lab memiliki batas waktu dan tidak memiliki fitur jeda. Jika lab diakhiri, Anda harus memulainya lagi dari awal.
  3. Di kiri atas layar, klik Start lab untuk memulai

Gunakan penjelajahan rahasia

  1. Salin Nama Pengguna dan Sandi yang diberikan untuk lab tersebut
  2. Klik Open console dalam mode pribadi

Login ke Konsol

  1. Login menggunakan kredensial lab Anda. Menggunakan kredensial lain mungkin menyebabkan error atau dikenai biaya.
  2. Setujui persyaratan, dan lewati halaman resource pemulihan
  3. Jangan klik End lab kecuali jika Anda sudah menyelesaikan lab atau ingin mengulanginya, karena tindakan ini akan menghapus pekerjaan Anda dan menghapus project

Konten ini tidak tersedia untuk saat ini

Kami akan memberi tahu Anda melalui email saat konten tersedia

Bagus!

Kami akan menghubungi Anda melalui email saat konten tersedia

Satu lab dalam satu waktu

Konfirmasi untuk mengakhiri semua lab yang ada dan memulai lab ini

Gunakan penjelajahan rahasia untuk menjalankan lab

Gunakan jendela Samaran atau browser pribadi untuk menjalankan lab ini. Langkah ini akan mencegah konflik antara akun pribadi Anda dan akun Siswa yang dapat menyebabkan tagihan ekstra pada akun pribadi Anda.