While Google’s cloud ecosystem offers a variety of solutions, unfortunately they’re not always easy to figure out, particularly since many features remain in “beta” for a long time. Below is an easy/quick way to spin up Google Compute Engine instances with Docker containers and tear them down when you’re done. If you’re looking for permanent, scalable apps this is not the solution for you (rather you should probably look into App Engine or Kubernetes.
- Create a Docker image
- Locally
- Using Google Container Builder
- Push local Docker image to Google Container Repository12docker tag <current name>:<current tag> gcr.io/<project name>/<new name>gcloud docker -- push gcr.io/<project name>/<new name>
- Create a compute instance. This process actually obfuscates a number of steps. It creates a virtual machine (VM) instance using Google Compute Engine, which uses a Google-provided, container-optimized OS image. The image includes Docker and additional software responsible for starting our docker container. Our container image is then pulled from the Container Repository, and run using docker run when the VM starts. Note: you still need to use docker attach even though the container is running. It’s worth pointing out only one container can be run per VM instance. Use Kubernetes to deploy multiple containers per VM (the steps are similar). Find more details on all the options in the links at the bottom of this post.1234567gcloud beta compute instances create-with-container <desired instance name> \--zone <google zone> \--container-stdin \--container-tty \--container-image <google repository path>:<tag> \--container-command <command (in quotes)> \--service-account <e-mail>
Tip You can view available gcloud projects with gcloud projects list
- SSH into the compute instance.1gcloud beta compute ssh <instance name> \ --zone <zone>
- Stop or Delete the instance. If an instance is stopped, you will still be billed for resources such as static IPs and persistent disks. To avoid being billed at all, use delete the instance.a. Stop1gcloud compute instances stop <instance name>
b. Delete
1gcloud compute instances delete <instance name>
Related Links:
- More on deploying containers on VMs
- More on zones
- More create-with-container options