Search code examples
dockergoogle-cloud-platformgoogle-container-registry

Automatically deploy new container to Google Cloud Compute Engine from Google Container Registry


I have a docker container which I push to GCR like gcloud builds submit --tag gcr.io/<project-id>/<name>, and when I deploy it on GCE instance, every time I deploy it creates a new instance and I have to remove the old instance manually. The question is, is there a way to deploy containers and force the GCE instances to fetch new containers? I need exactly GCE, not Google Cloud Run or other because it is not an HTTP service.

I deploy the container from Google Console using the Deploy to Cloud Run button Deploy


Solution

  • I'm posting this Community Wiki for better visibility. In the comment section there were already a few good solutions, however at the end OP wants to use Cloud Run.

    At first I'd like to clarify a few things.

    I have a docker container which I push to GCR like gcloud builds submit

    gcloud builds submit is a command to build using Google Cloud Build.

    Cloud Build is a service that executes your builds on Google Cloud Platform infrastructure. Cloud Build can import source code from Cloud Storage, Cloud Source Repositories, GitHub, or Bitbucket, execute a build to your specifications, and produce artifacts such as Docker containers or Java archives.

    In this question, OP is referring to Container Registry, however GCP recommends to use Artifact Registry which soon will replace Container Registry.

    Pushing and pulling images from Artifact Registry is explained in Pushing and pulling images documentation. It can be done by docker push or docker pull command, where earlier you have to tag an image and create Artifact Registry.

    Deploying on different GCP products

    Regarding deploying on GCE, GKE and Cloud Run, those are GCP products which are quite different from each.

    GCE is IaaS where you are specifying the amount of resources and you are maintaining all the installation of all software (you would need to install Docker, Kubernetes, programming libs, etc).

    GKE is like Hybrid as you mention the amount of resources you need but it's customized to run containers on it. After creation you already have docker, kubernetes and other software needed to run containers on it.

    Cloud Run is a serverless GCP product, where you don't need to calculate the amount of needed resources, installing software/libs, it's a fully managed serverless platform.

    When you want to deploy a container app from Artifact Registry / Container Registry, you are creating another VM (GCE and GKE) or new service (Cloud Run).

    If you would like to deploy new app on the same VM:

    • On GCE, you would need to pull an image and deploy it on that VM using Docker or Kubernetes (Kubeadm).
    • On GKE you would need to deploy a new deployment using command like
    kubectl create deployment test --image=<location>-docker.pkg.dev/<projectname>/<artifactRegistryName>/<imageName>
    

    and delete the old one.

    In Cloud Run you can deploy an app without concerns about resources or hardware, which steps are described here. You can create revisions for specific changes in the image. However Cloud Run also allows CI/CD using GitHub, BitBucket or Cloud Source Repositories. This process is also well described in GCP documentation - Continuous deployment

    Possible solutions:

    • Write a Cloudbuild.yaml file that do that for you at each CI/CD pipeline run
    • Write a small application on GCE that subscribes to Pub/Sub notifications created by Cloud Build. You can then either pull the new container or launch a new instance.
    • Use Cloud Run with CI/CD.

    Based on one of the OP's comments, as chosen solution was to use Cloud Run with CI/CD.