How to Deploy a Dockerized web app or service to Google Compute Engine (GCE) in Google Cloud Platform (GCP)

By the time I am writing this, I have just deployed for first time a web app into Google Cloud Platform, using Google Compute Engine. This post intends to help beginners to do the same.

After having worked in IT for many years, I am not proud the way I got it working, since I had to connect to instance with Google Cloud Shell, and create the Docker image from there. I am sure that any DevOps guy who has ever set CD pipeline or with minimal experience with Google Cloud Platform would blame for that, since there must be at least 10 ways of doing that into an automated and pragmatic way for a proof of concept, with zero cost

Any feedback is therefore welcomed through comments section below, but please, keep in mind that this post is aimed for beginners, keep a pragmatic approach.

Find below a simple step-by-step tutorial:

1. Create a Google account

In case you don’t have an account yet, you will need to create a google account.

2. Enable Google Cloud Platform

Once you are logged in, create a free account into Google Cloud Platform and enable it, you may get some free credits to spend.

Once the service is added, you can access to Google Cloud Console. Note that a default project will have been created, but you may create a new one.

If you just created the account, a default project will have been created. Go to the top bar, and click on the name of the project. A new pop up will be displayed. showing the Project Id. Please, take note of Project Id of the project you want to use. You will need it later. You can find it

3. Enable billing for your Google account

Even if you use free initial credits, you will need to enable billing on Billing section and provide card details.

Bonus point: if you do care a lot about your credits, go to Billing section, Budgets and Alerts page and setup some alerts. You will receive emails on completing defined percentages of your budget.

4. Setup your local environment

Install Docker. You will need it for building your Docker Image and testing it locally.

5. Build a Docker image

Dockerize your web app or service, i.e., create a Dockerfile that will describe how the docker containers must be setup to run your web app or service.

Build your Docker Image, in case you didn’t do it before. The tag format is very important, as it tells Docker where to store the image into private Google Cloud Registry.

 docker build -t <IMAGE_NAME>:<TAG_VERSION> 

6. Create a New VM Instance

Launch Google Cloud Console, and, in Compute Engine section, access to VM instances page. From there, click on Create instance button on top.

A new page is displayed. Before seeing the picture below, note the following points:

  • Region and Zone have an impact on user’s network latency and cost. Some regions may not have available any shared CPUs so if you try to create one shared instance in there, you would get an error. Regarding cost, some regions are cheaper than others. For Europe, eureope-west4 is cheap (but it didn’t have any shared CPUs available, so I chose a different one for my proof of concept).
  • Machine Type: the more CPUs and memory, the more you pay. On a budget, you can click on Customize link, and then select 0 cores, which means 1 shared CPU, so your app will be sharing resources with other apps. Minimum RAM is 0.6GB, for shared CPUs, but you may need more RAM (remember you are sharing). Shared CPUs don’t offer GPUs, so that’s another point to consider.
How to Deploy a Dockerized web app or service to Google Compute Engine (GCE) in Google Cloud Platform (GCP) 1
  • We tick Deploy a container image to this VM instance, which change the Boot Disk to an container optimized OS image, which includes Docker command line tool.
  • Container image: if you published your Docker image to either a Docker Registry or Google Cloud Registry, you can just specify the image here and you won’t need any additional configuration. In my case, I was testing my image in my local Docker, so I just setup an image in that field, like jetty:9.4.14-jre11, which is the base of my Docker Image.
  • Tick Allocate a buffer for STDIN and Allocate a pseudo-TTY, just in case you want to connect to the container (but you won’t need it for a proof of concept).
How to Deploy a Dockerized web app or service to Google Compute Engine (GCE) in Google Cloud Platform (GCP) 2
  • Note that instance has a 10GB boot disk with an image optimized for containers. The more space you provide, the more you pay, but 10GB is the minimum disk size, though.
  • If you want to invoke your web app or service from outside the network, check Allow HTTP traffic and/or Allow HTTPS traffic, depending on your web app or service, in order to setup the firewall.
How to Deploy a Dockerized web app or service to Google Compute Engine (GCE) in Google Cloud Platform (GCP) 3

Click Create and your new VM instance should be up and running shortly.

6. Deploy your web app or service to your VM instance

Note that this step is not needed if you had push your Docker image to either a Docker Registry or Google Cloud Registry and specified the Docker Image path on creating the VM instance in previous step.

In Google Cloud Console, in Compute Engine section, access to VM instances page. From there, click on SSH button on the row regarding the created instance. A new SSH terminal will open and you will be able to change your instance.

I am not proud of this, 🙁 . From there, I just cloned with git my project, built the Docker Image again (in the same way I did in previous step), and just run the image:

docker run --rm -p <EXTERNAL_PORT>:<INTERNAL_CONTAINER_PORT><IMAGE_NAME>:<TAG_VERSION> 

7. Allow incoming calls in Google Cloud VPC firewall

App is up and running, but is not accessible yet from outside, in Google Cloud Console, in VPC Network section, access to Firewall Rules page and add a rule for incoming request to <EXTERNAL_PORT> exposed by Docker in previous step. You can specify IP ranges or just 0.0.0.0/0, if you don’t really need them.