How to use Google Cloud Run with Docker

Are you looking for a way to deploy your Docker containers to the cloud? Look no further than Google Cloud Run! This fully managed serverless platform allows you to run your containers on demand, without worrying about infrastructure or scaling. In this article, we'll show you how to use Google Cloud Run with Docker, step by step.

Prerequisites

Before we get started, you'll need a few things:

If you don't have a Google Cloud account yet, you can sign up for a free trial at https://cloud.google.com/free. Once you have an account, you can create a new project and enable billing.

To install Docker, follow the instructions for your operating system at https://docs.docker.com/get-docker/. Make sure you can run the docker command from your terminal.

To install the Google Cloud SDK, follow the instructions at https://cloud.google.com/sdk/docs/install. Make sure you can run the gcloud command from your terminal.

Creating a Docker image

The first step in using Google Cloud Run with Docker is to create a Docker image of your application. If you already have a Docker image, you can skip this section.

For this example, we'll create a simple Node.js application that responds with "Hello, world!" when accessed. Create a new directory for your project and create a file named index.js with the following contents:

const http = require('http');

const hostname = '0.0.0.0';
const port = process.env.PORT || 8080;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, world!\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

This code creates a simple HTTP server that listens on the port specified by the PORT environment variable (or 8080 if not set) and responds with "Hello, world!" when accessed.

Next, create a file named Dockerfile in the same directory with the following contents:

FROM node:14

WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .

EXPOSE 8080
CMD [ "npm", "start" ]

This Dockerfile specifies that we want to use the official Node.js 14 image as our base image, set the working directory to /app, copy the package.json and package-lock.json files to the working directory and run npm install to install the dependencies, copy the rest of the files to the working directory, expose port 8080 and run npm start when the container starts.

To build the Docker image, run the following command from the directory containing the Dockerfile:

docker build -t my-app .

This command builds a Docker image with the tag my-app using the Dockerfile in the current directory (.).

Pushing the Docker image to Google Container Registry

Now that we have a Docker image, we need to push it to a container registry that Google Cloud Run can access. For this example, we'll use Google Container Registry, which is a private Docker image registry that's integrated with Google Cloud.

To push the Docker image to Google Container Registry, follow these steps:

  1. Tag the Docker image with the registry name:

    docker tag my-app gcr.io/[PROJECT-ID]/my-app
    

    Replace [PROJECT-ID] with your Google Cloud project ID.

  2. Authenticate Docker with Google Cloud:

    gcloud auth configure-docker
    

    This command configures Docker to use your Google Cloud credentials for authentication.

  3. Push the Docker image to Google Container Registry:

    docker push gcr.io/[PROJECT-ID]/my-app
    

    This command pushes the Docker image to Google Container Registry.

Deploying the Docker image to Google Cloud Run

Now that we have a Docker image in Google Container Registry, we can deploy it to Google Cloud Run. Follow these steps:

  1. Deploy the Docker image to Google Cloud Run:

    gcloud run deploy --image gcr.io/[PROJECT-ID]/my-app --platform managed
    

    This command deploys the Docker image to Google Cloud Run in managed mode. Google Cloud Run will automatically scale the container up and down based on traffic.

  2. Wait for the deployment to complete:

    gcloud run services wait my-app
    

    This command waits for the deployment to complete. It may take a few minutes for the container to start up.

  3. Get the URL of the deployed service:

    gcloud run services describe my-app --format 'value(status.url)'
    

    This command gets the URL of the deployed service. Visit this URL in your web browser to see the "Hello, world!" message.

Congratulations, you've successfully deployed a Docker container to Google Cloud Run!

Conclusion

In this article, we've shown you how to use Google Cloud Run with Docker. We've created a simple Node.js application, built a Docker image, pushed it to Google Container Registry and deployed it to Google Cloud Run. Google Cloud Run makes it easy to run your Docker containers in the cloud, without worrying about infrastructure or scaling. Give it a try and see how it can simplify your container deployments!

Editor Recommended Sites

AI and Tech News
Best Online AI Courses
Classic Writing Analysis
Tears of the Kingdom Roleplay
Crypto Tax - Tax management for Crypto Coinbase / Binance / Kraken: Learn to pay your crypto tax and tax best practice round cryptocurrency gains
Defi Market: Learn about defi tooling for decentralized storefronts
Neo4j App: Neo4j tutorials for graph app deployment
Shacl Rules: Rules for logic database reasoning quality and referential integrity checks
Skforecast: Site dedicated to the skforecast framework