Deploy Swift HTTP Serverless Container to Google Cloud Run in 5 minutes

Alfian Losari
Apr 23 · 5 min read

At Google Cloud Next 2019, Google has just introduced Google Cloud Run. It’s a serverless computing service, which means we don’t have to manage the infrastructure by ourselves. It is based on containers, so we only need to provide the Dockerfile that contains our HTTP server application and deploy. The service will be invocable via the HTTP request from the client and the payment will be using pay per use model.

There are many features that Google Cloud provides for the Cloud Run, such as:

  1. Fast autoscaling, automatically scales our app up and down based on the traffic.
  2. Container based, using Docker container to build and deploy our service.
  3. No DevOps. All we need to do is deploy our container and Google Cloud will manage all the rest for us.
  4. Based on Knative, the portability means we can also deploy to Google Kubernetes Engine (GKE Cluster) across platforms.
  5. Integrated logging and monitoring with StackDriver.
  6. Ability to use our own custom domains.

You can learn more about the Cloud Run directly from Google with the official link below.

What we will build

In this article, we will deploy a simple Swift HTTP Server app to Google Cloud Run using Dockerfile. We will use the Google Cloud SDK with Command Line for this. There are only 4 main tasks that we need to perform:

  1. Prepare our HTTP Swift app.
  2. Build the Dockerfile.
  3. Upload to Container Registry.
  4. Deploy container to Google Cloud Run.

Setting up Google Cloud

Before you begin, here are the things that you require to have:

  1. Register and Sign in to the Google Cloud Platform. (
  2. Download and install Google Cloud SDK to your machine. (
  3. Create a new project from the Google Cloud Console.
  4. Make sure to follow all these steps in here to activate the Google Cloud Run API for your project. (

Prepare our HTTP Swift Application

Open your terminal/shell, create a new directory named hello-swift-cloudrun and navigate to that directory.

Inside the directory, create a new swift package.

Next, open Package.swift and copy the following code into the file. We will add the Swifter tiny HTTP server library as the dependency to run our HTTP Server in Swift.

Next, open the main.swift file from the Sources directory. Copy the following code.

Here are the things that it performs:

  1. Register 2 routes /html and /api. These routes will return the response containing the current date that is formatted using DateFormatter. The html path will return the text in HTML format, while the api path will return the response in JSON format.
  2. Retrieve the PORT from the environment variable.
  3. Start the HTTPServer passing the PORT to listen for the request in the port.

Try to build and run the server by typing these commands the terminal.

To test, open your browser and navigate to the address http://localhost:8080/html. You should see the text printed with the current time in your browser.

Swift HTTP Server

Build the Dockerfile

Next, we will containerize our app by creating the Dockerfile. Create the file and copy the following code below.

This will copy all the file to the container image, then run the swift build using release configuration. It will also run the server after the build has been finished.

Upload to Container Registry

Next, we need to upload our container to Cloud Registry. Make sure to retrieve your project id for your project. Run the command below.

Wait for the container builds process to finish and then uploaded to container registry. It will print the success message to the terminal.

You can check the list of the successfully uploaded container using this command.

Deploy Container to Google Cloud Run

At last, we need the deploy the image to the Google Cloud Run. Type these following command.

Here are several things that it performs:

  1. Set the region of deployment to us-central1.
  2. Deploy the image from the Container Registry path in this case hello-swift-cloudrun.
  3. Configure to use 512M of memory to use.
  4. Allow unauthenticated request to invoke the HTTP.

You can configure other things, from memory, concurrency, and request timeout. Check the link below

After the deployment finished successfully, the terminal will print the URL endpoint of the deployed service that we can use. Open your browse and navigate to:

  1. https://$URL/html
  2. https://$URL/api

Monitoring through Dashboard

You can view all your deployed services to Cloud Run fromt the console dashboard.

Cloud Run Dashboard

In here you can also manage the custom domains, delete and create services, and view the logs of your deployed services.

!!!Make sure to delete all the resources that you have created after you finish this article to avoid billings!!!


You can clone the completed project in the repository below.

That’s it, in just a simple steps we have deployed our serverless backend using Docker to the Google Cloud Run managed autoscaling service. The serverless paradigm provide us speed and reliability to execute rapidly as the size of our application grows over time ⚡️⚡️⚡️.

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Alfian Losari

Written by

Mobile, Web, a bit of backend Software Developer and Lifelong Learner. Currently building super app @ Go-Jek.

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade