Instrukcje i wymagania dotyczące konfiguracji modułu
Chroń swoje konto i postępy. Zawsze używaj okna przeglądania prywatnego i danych logowania do modułu, kiedy go uruchamiasz.

Deploy a Static Site Using Traefik and Cloud Run

Laboratorium 30 godz. universal_currency_alt 1 punkt show_chart Wprowadzające
info Ten moduł może zawierać narzędzia AI, które ułatwią Ci naukę.
Te treści nie są jeszcze zoptymalizowane pod kątem urządzeń mobilnych.
Dla maksymalnej wygody odwiedź nas na komputerze, korzystając z linku przesłanego e-mailem.

gem-cloud-run-traefik-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 Traefik as a reverse proxy and Cloud Run for serving the content. You will containerize a simple static site, push the image to Artifact Registry, configure Traefik to route traffic, and deploy it all to Cloud Run.

Task 1. Set Up Your Environment

Configure your Google Cloud environment for this lab.

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

Task 2. Create an Artifact Registry Repository

Create a Docker repository in Artifact Registry to store your container images.

  1. Create a Docker repository named 'traefik-site'.
gcloud artifacts repositories create traefik-repo --repository-format=docker --location={{{ project_0.default_region | "REGION" }}} --description="Docker repository for static site images" Note:
This command creates a Docker repository in Artifact Registry.

Task 3. Build and Push the Static Site Image

Create a simple static site, containerize it using Docker, and push the image to Artifact Registry.

  1. Create a directory for your static site.
mkdir traefik-site && cd traefik-site && mkdir public
  1. Create an public/index.html file with some simple content.
cat > public/index.html <<EOF <html> <head> <title>My Static Website</title> </head> <body> <p>Hello from my static website on Cloud Run!</p> </body> </html> EOF Note:
This is the HTML content for the static site.
  1. Authenticate Docker to Artifact Registry.
gcloud auth configure-docker {{{ project_0.default_region | "REGION" }}}-docker.pkg.dev Note:
This command configures Docker to use your Google Cloud credentials.
  1. Create a traefik.yml with the following content:
entryPoints: web: address: ":8080" providers: file: filename: /etc/traefik/dynamic.yml watch: true log: level: INFO Note:
This command creates the Traefik configuration file.
  1. Create a dynamic.yml with the following content:
http: routers: static-files: rule: "PathPrefix(`/`)" entryPoints: - web service: static-service services: static-service: loadBalancer: servers: - url: "http://localhost:8000" Note:
This command creates the dynamic content configuration file.

Task 4. Create the Dockerfile

Define the Docker image for Traefik and your static website.

  1. Create a Dockerfile with the following content:
FROM alpine:3.20 # Install traefik and caddy RUN apk add --no-cache traefik caddy # Copy configs and static files COPY traefik.yml /etc/traefik/traefik.yml COPY dynamic.yml /etc/traefik/dynamic.yml COPY public/ /public/ # Cloud Run uses port 8080 EXPOSE 8080 # Run static server (on 8000) and Traefik (on 8080) ENTRYPOINT [ "caddy" ] CMD [ "file-server", "--listen", ":8000", "--root", "/public", "&", "traefik" ] Note:
This Dockerfile uses the official Alpine image, sets the working directory, and copies your website and Caddyfile. Traefik will be used to handle routing and Caddy used to serve web content.

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" }}}/traefik-repo/traefik-static-site: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" }}}/traefik-repo/traefik-static-site: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 traefik-static-site --image {{{ project_0.default_region | "REGION" }}}-docker.pkg.dev/{{{ project_0.project_id | "PROJECT_ID" }}}/traefik-repo/traefik-static-site:latest --platform managed --allow-unauthenticated --port 8000 Note:
This command deploys the Docker image to Cloud Run and allows unauthenticated access.
  1. When prompted, confirm the service name as traefik-static-site 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!

Congratulations! You have successfully deployed a static website using Traefik and Cloud Run. You learned how to containerize a static site, store the image in Artifact Registry, configure Traefik as a reverse proxy, and deploy it all to Cloud Run.

Additional Resources

Manual Last Updated Jun 25, 2025

Lab Last Tested Jun 25, 2025

Zanim zaczniesz

  1. Moduły tworzą projekt Google Cloud i zasoby na określony czas.
  2. Moduły mają ograniczenie czasowe i nie mają funkcji wstrzymywania. Jeśli zakończysz moduł, musisz go zacząć od początku.
  3. Aby rozpocząć, w lewym górnym rogu ekranu kliknij Rozpocznij moduł.

Użyj przeglądania prywatnego

  1. Skopiuj podaną nazwę użytkownika i hasło do modułu.
  2. Kliknij Otwórz konsolę w trybie prywatnym.

Zaloguj się w konsoli

  1. Zaloguj się z użyciem danych logowania do modułu. Użycie innych danych logowania może spowodować błędy lub naliczanie opłat.
  2. Zaakceptuj warunki i pomiń stronę zasobów przywracania.
  3. Nie klikaj Zakończ moduł, chyba że właśnie został przez Ciebie zakończony lub chcesz go uruchomić ponownie, ponieważ spowoduje to usunięcie wyników i projektu.

Ta treść jest obecnie niedostępna

Kiedy dostępność się zmieni, wyślemy Ci e-maila z powiadomieniem

Świetnie

Kiedy dostępność się zmieni, skontaktujemy się z Tobą e-mailem

Jeden moduł, a potem drugi

Potwierdź, aby zakończyć wszystkie istniejące moduły i rozpocząć ten

Aby uruchomić moduł, użyj przeglądania prywatnego

Najlepszym sposobem na uruchomienie tego laboratorium jest użycie okna incognito lub przeglądania prywatnego. Dzięki temu unikniesz konfliktu między swoim kontem osobistym a kontem do nauki, co mogłoby spowodować naliczanie dodatkowych opłat na koncie osobistym.