Tutorial: How to Redirect Traffic from One Domain to Another Using Google Cloud Run

Welcome to this comprehensive tutorial where we will explore how to redirect traffic from one domain to another using Google Cloud Run. In this tutorial, we will use “morbz/docker-web-redirect” as the Docker image, which is an excellent tool for redirecting HTTP/S traffic.


Before proceeding with the tutorial, ensure you have:

  1. A Google Cloud account with the necessary permissions to create and manage Google Cloud Run instances.

What is Google Cloud Run?

Google Cloud Run is a fully-managed serverless platform developed by Google. With Cloud Run, you can run your applications in Docker containers without worrying about infrastructure management. It is a great option as it has a very generous free tier which includes “2 million free requests per month“.

It abstracts away all infrastructure management, so you can focus on what matters most — building great applications. Or in the case of this tutorial, making domain redirection as simple as possible.

What is morbz/docker-web-redirect?

morbz/docker-web-redirect is a Docker image built specifically for the purpose of redirecting HTTP/S traffic from one domain to another. This Docker image uses Nginx to perform the redirection.

Steps to Redirect the Traffic

(OPTIONAL) Test the Docker Image Locally

Before you can test image, you need to have the following installed and configured on your computer:

  • Docker Desktop
  • A terminal of your choice

If that’s done, proceed with pulling the Docker image “morbz/docker-web-redirect” from Docker Hub:

docker pull morbz/docker-web-redirect

Before we deploy to Google Cloud Run, let’s test our Docker image locally. Run the following command:

docker run -p 8080:80 -e REDIRECT_TARGET="http://example.com" morbz/docker-web-redirect

Now, if you visit http://localhost:8080 in your web browser, you should be redirected to http://example.com. To stop the service, just press Ctrl + C on your keyboard or close the terminal.

Deploy to Google Cloud Run

Step 1: Creating the service

First, we want to create a new Google Cloud Run service in the Cloud Console. So now you need to go to the Cloud Console and navigate to Cloud Run using the top search.

Cloud run dashboard

Now, let’s get to deploying the Docker image to Google Cloud Run. Click on ‘Create Service’ and you will be shown a form to fill in the info about the service. We want to have the following variables configured:

  • Container image URL = morbz/docker-web-redirect
  • Region = where most of your users are located at, not very important to get right
  • Autoscaling -> Maximum number of instances = 10
Creating the cloud run service - top configuration

When you scroll down, you also need to check the “Allow unauthenticated invocations”. If this is not chosen, anonymous users from the internet will not be able to access the web service.

Step 2: Creating the service – configure environment variables

For environment variables, it is fairly straightforward – you just need to provide the REDIRECT_TARGET variable. If you would like to switch the redirect from permanent to temporary, then you will need to look at the explanation of environment variables below.

Understanding Environment Variables

The Docker image “morbz/docker-web-redirect” uses several environment variables to customize the behavior of the redirection:

  • REDIRECT_TARGET: This is the URL to which the traffic will be redirected.
  • REDIRECT_TYPE: If this is not set, the redirect will use HTTP status code 301 (permanent redirect). If it’s set to redirect, then it will use HTTP status code 302 (temporary redirect). The default redirect is with status code 301.

For example, if you want to redirect traffic to “https://mynewsite.com” permanently, you can set the environment variables as:


Step 3: Mapping a custom domain

Once the service has been created, you can already access it using the auto-generated domain that was assigned to it.

However, as we would like to redirect a specific domain, we still have some quick steps to do. First, let’s navigate back to the Cloud Run service list. And next, let’s click on ‘Manage custom domains’ at the top of the page.

On the next page, you need to click on ‘Add mapping’ and in the opened popup, select your service and the verified domain from the second dropdown.

You might need to verify the domain via Google Search Console. Here is a good tutorial on how you can do that.

Once the ownership has successfully been verified, then you can go back to the Google Cloud Platform and continue with the custom domain mapping.

Once the mapping is created, you will be shown DNS entries that you need to provide so that the traffic can be correctly forwarded and the SSL certificate generated.

After you configure the DNS entries at your registrar, you will see at least one pending domain mapping in the list. We also created the www entry as well so that also the traffic for that subdomain is also correctly forwarded.

Step 4: Test the Setup

Once the domains have been configured, then you can try to access them in the browser and see if the traffic is correctly forwarded. Until the domains are verified, you can use the auto-generated domain visible under the service to see if the user is redirected as expected.

In the case of our auto-generated domain, it is working exactly as expected.

Cloud run domain redirect test with auto-generated domain

The same goes for the custom domain that we mapped to the service – the traffic is forwarded and an HTTPS certificate is automatically assigned.

Cloud run domain redirect test with custom mapped domain


And that’s it! You have successfully set up a domain redirect using Google Cloud Run. The traffic visiting your Cloud Run service URL will now be redirected to the target URL specified in the REDIRECT_TARGET environment variable.

When your domain mapping has been correctly configured by Cloud Run (might even take some hours to detect DNS entry changes) then your custom domain will also be correctly redirected.

How can I do it without writing code?

If you are looking for an easy way to redirect a domain and not have to configure all of the technical infrastructure for it, then you should consider using the Domain Forward service. You can sign up using the link below and create your first redirect using the Getting Started guide.