Let’s deploy a container on Google Cloud Platform. We’ll do it in a way that requires no preexisting infrastructure (e.g., a Kubernetes cluster on GKE, a VM running Docker, etc.). All we will need is an existing account with an associated project as well as an installation of Docker.

Running a single container is a great use case for things like deploying an HTTP server or a Twitter bot, doing some video processing, or just trying out the platform without much overhead.

We are going to deploy a small HTTP server that responds to JSON requests with random Go proverbs. All the code including the Dockerfile can be found here.

Add Google Container Registry to Docker

First, we will configure Docker to support pushing to the Google Container Registry, otherwise known as GCR. If you don’t have gcloud installed, see here.

gcloud auth configure-docker

Push the Image

Now, assuming we have already built the container image (named gcr.io/enocom-dev/proverb ), we need to push that image to GCR. For those following along on the command line, you will need to swap out enocom-dev with your own project ID.

docker push gcr.io/enocom-dev/proverb:latest

Allow Ingress Traffic on Port 80

Since we will be running an HTTP server within our container, we have one more thing to do before deploying. We need to configure a firewall rule to allow incoming HTTP traffic.

gcloud compute firewall-rules create allow-http \

-target-tags http-server \

-allow tcp:80

Deploy the Container

Now that we have our image stored in GCR and we have configured a firewall rule, we are ready to deploy the container. Again, note you will need to swap out the project ID identifying the container image.

gcloud beta compute instances \

create-with-container proverb-vm \

-tags http-server \

-container-image gcr.io/enocom-dev/proverb:latest

Users of Docker might noticed the absence of any port mappings in the deploy command. By default, the container is started using the host network. In the case here, that means port 80 in the container will map to port 80 on the host VM.

Send a Request to the Deployed Container

Now with the container deployed, we are ready to send a request to our HTTP server:

# Note: your IP address will be different

curl -i 35.184.67.112/proverbs/random

Provided all goes well, we will get a response that looks something like:

HTTP/1.1 200 OK

Content-Type: application/json; charset=utf-8

Date: Wed, 25 Apr 2018 21:25:18 GMT

Content-Length: 100 {"link":"https://github.com/golang/go/wiki/CodeReviewComments#dont-panic","content":"Don't panic."}

And with that we have a container running on Google Cloud Platform.

Teardown

Teardown is also easy. Here’s what you need to remove all created resources: