Streamlines: Google Cloud Run

I have a Docker container that can run on a single machine, and I want to deploy it to the cloud. Please note that when you use a database, the database is also a special cloud of information. Cloud vendors will have corresponding mechanisms and generally charge a fee. However, the amount of data in our database will not be charged this time. I expect that using MongoDB Atlas will not incur charged if the data is less than 512M.

services:
  application:
    container_name: my-wood-co2-application
    image: node:lts-bullseye
    ports:
      - "3001:3001"
    volumes:
      - ./code:/home/node/code
    working_dir: /home/node/code/my-wood-co2-app-source
    environment:
      - HOST=0.0.0.0
      - CHOKIDAR_USEPOLLING=true
      - CHOKIDAR_INTERVAL=100
    tty: true
    # command: "npm run start"
    # command: "/bin/sh"
    depends_on:
      - mongodb

  mongodb:
    container_name: my-mongodb
    #image: mongo:latest
    image: mongo:3.6.23
    restart: always
    ports:
      - "27017:27017"
    volumes:
      - mongodb_data:/data/db
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=secretpassword
      - MONGO_INITDB_DATABASE=wood_co2_db
    healthcheck:
      test: ["CMD", "mongo", "--username", "admin", "--password", "secretpassword", "--eval", "db.adminCommand('ping')"]
      interval: 30s
      retries: 3
      start_period: 10s
      timeout: 5s

volumes:
  mongodb_data:

In the configuration of my local docker-compose.yml above, you can see that I use the image node:its-bullseye in the application service, and the image mongo:latest in the mongodb service. These are the initial settings commonly used when developing node.js and using mongo db. In practice, the application service code will be placed in the host’s storage space and then mapped to the application container to develop the project.

Next, we are going to prepare to deploy to Google Cloud Run. I plan to deploy the Application Service and connect the database to the remote MongoDB Atlas. Let’s first deploy the image we’re using to your Google Cloud project. docker tag node:lts-bullseye gcr.io/wood-10958/wood-app docker push gcr.io/wood-10958/wood-app. In addition, please set the host of my Google Cloud Run to North America. gcloud auth configure-docker us-central1-docker.pkg.dev docker push us-central1-docker.pkg.dev/wood-10958/wood-repo/wood-app:latest.

Finally, we will deploy our Application Service to Google Cloud Run. There are three important parameters:

  • –image=us-central1-docker.pkg.dev/wood-10958/wood-repo/wood-app:latest #The Docker image used
  • -region=us-central1 #North American Host
  • –set-env-vars=HOST=0.0.0.0,CHOKIDAR_USEPOLLING=true,CHOKIDAR_INTERVAL=100,MONGODB_URI=mongodb+srv://fluber:<db_password>@cluster0.0smc2wa.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0 #Connecting to an external MongoDB Atlas host
gcloud auth login

gcloud config set project wood-10958

gcloud auth configure-docker

# Tag the image for GCR
docker tag node:lts-bullseye gcr.io/wood-10958/wood-app

# Push it
docker push gcr.io/wood-10958/wood-app

#Try Using Artifact Registry Instead (Recommended)
gcloud services enable artifactregistry.googleapis.com

#Create a Docker Repository
gcloud artifacts repositories create wood-repo \
  --repository-format=docker \
  --location=us-central1 \
  --description="Docker repo for Wood app"

#Tag & Push to Artifact Registry
docker tag fluber/wood:latest us-central1-docker.pkg.dev/wood-10958/wood-repo/wood-app:latest  

docker push us-central1-docker.pkg.dev/wood-10958/wood-repo/wood-app:latest  

#Re-authenticate and Configure Docker
gcloud auth configure-docker us-central1-docker.pkg.dev

#Try Pushing Again
docker push us-central1-docker.pkg.dev/wood-10958/wood-repo/wood-app:latest

gcloud run deploy wood-app \
  --image=us-central1-docker.pkg.dev/wood-10958/wood-repo/wood-app:latest \
  --platform=managed \
  --region=us-central1 \
  --allow-unauthenticated \
  --port=3001 \
  --set-env-vars=HOST=0.0.0.0,CHOKIDAR_USEPOLLING=true,CHOKIDAR_INTERVAL=100,MONGODB_URI=mongodb+srv://fluber:<db_password>@cluster0.0smc2wa.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0

Google Cloud Account

To use Google Cloud Run, you must have a Google Cloud account. Please refer to this site. https://cloud.google.com/docs/get-started. You must set up payment information. This step requires your credit card.

MongoDB Altas Account

In my case, the database is changed to mongodb. You also need to apply for an account. Please refer to this site. https://www.mongodb.com/docs/atlas/getting-started/

There are clearer instructions for Google Cloud Run. Please refer to my other article.

If you think it is well written, please forward this article.

Spread the love