Deploy a Dockerized Web Application or Service to Google Compute Engine

From Luis Gallego Hurtado - Not Another IT guy
Jump to: navigation, search


This tutorial shows the step I followed the first time I deployed a web app into Google Cloud Platform (GCP), using Google Compute Engine (GCE), in 2019.

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 more expert developer, with more experience with Google Cloud Platform, would blame me 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.

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.

Configure Docker to use gcloud to as credential helper. This will allow Docker to push the image to your private Google Cloud Container Registry.

gcloud auth configure-docker

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.
CCP-GCE-Create-Instance-1 443x654.png
  • 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).
GCP-GCE-Create-Instance-2 459x509.png
  • 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.
GCP-GCE-Create-Instance-3 442x520.png
  • 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 step, 🙁 . 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:


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, if you don’t really need them.