Deploy Docker images to google cloud run with docker-compose

Viewed 883

I'm trying to deploy an app from a docker-compose file with two images in it: an Angular app (frontend) and a very small nestjs app (backend). I'm using GCB, their triggers and a cloudbuild.json.

This is its current state:

{
  "steps": [
    {
      "name": "gcr.io/$PROJECT_ID/docker-compose",
      "args": ["-f", "./docker-compose.${_ENVIRONMENT}.yml", "up", "-d"]
    },
    {
      "name": "gcr.io/cloud-builders/docker",
      "args": ["tag", "configurator:latest", "gcr.io/$PROJECT_ID/${_IMAGE_ID}"]
    },
    {
      "name": "gcr.io/cloud-builders/gcloud",
      "args": [
        "run", "deploy",
        "--allow-unauthenticated",
        "${_IMAGE_ID}",
        "--image", "gcr.io/$PROJECT_ID/${_IMAGE_ID}",
        "--region", "europe-west4",
        "--platform", "managed"
      ]
    }
  ],
  "images": [
    "gcr.io/$PROJECT_ID/${_IMAGE_ID}"
  ],
  "timeout": "1200s"
}

The build always fails on step 2, when trying to push the docker image to the registry. I'm not really sure what the images name could be or how this should even work, since there is two docker images that need to be pushed and deployed. Is it even possible with GCB or do I need a GKE Cluster for running two docker images?

Should I maybe build the two Docker images separately, push them each to the registry and deploy them to separate GCR Services?

Thanks in advance.

1 Answers

Docker compose build nothing, it only run the images according with the yaml configuration. Your step 2, that tag something, tag what? That's why it fails.

So, forget docker compose. Deploy your backend on Cloud Run, only the container. For your frontend, you have several solution:

  • Deploy it on Cloud Run also (not the best solution)
  • Deploy the static files on App Engine (need to create a app.yaml file)
  • Deploy the static files on Google Cloud Storage.

then, create a load balancer with 2 backends:

  1. Your web site backend
  2. Your nestjs backend.

And then, you have a production ready deployment. (Let me know if you need more help on some parts)


EDIT 1

With a load balancer, you can define the path to serve your resources (URL map). For instances:

  • /nest/* -> redirect the traffic to your nestjs backend
  • /* -> by default, redirect the traffic to your bucket backend.

Both, static and nestjs backend, are accessible through the same IP (and domain name, after the set up of your DNS registrar. Google can also automatically provision the SSL certificate for you).

There is 2 advantages to use a Load balancer:

  • The front and the back have the same base URL, you won't have to manage CORS on your backend.
  • The traffic of both back and front is served in HTTP (not recommended) or HTTPS, that prevent the errors for mixed content in your browser.

Note: without a load balancer, you can serve static source only in HTTP mode, HTTPS isn't supported without additional layer, such as a load balancer.

The "issue" with a load balancer, is a minimal cost of $15 per month.

Related