2024-09-09 Web Development
Deploy a Dockerized application to Google Cloud
By O Wolfson
Node.js-based web application that takes text input from the user, saves it to a text file, and is deployed using Docker. This tutorial also includes steps to deploy the Dockerized application to Google Cloud.
Prerequisites
- Node.js and npm installed on your machine.
- Docker installed on your machine.
- A Google Cloud Platform (GCP) account.
Step 1: Set Up Your Node.js Application
1.1 Create a new directory and initialize a Node.js project:
bashmkdir text-file-app
cd text-file-app
npm init -y
1.2 Install Express:
bashnpm install express
1.3 Create an app.js
file:
javascriptconst express = require("express");
const fs = require("fs");
const bodyParser = require("body-parser");
const app = express();
const port = 3000;
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", (req, res) => {
res.send(
'<form method="POST" action="/save"><input type="text" name="text"/><button type="submit">Save</button></form>'
);
});
app.post("/save", (req, res) => {
const { text } = req.body;
fs.writeFileSync("output.txt", text);
res.send("Text saved to file!");
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});
Step 2: Dockerize the Application
2.1 Create a .dockerignore
file to exclude node_modules and npm logs:
node_modules
npm-debug.log
2.2 Create a Dockerfile
:
Dockerfile# Use the official Node.js 16 image. # https://hub.docker.com/_/node FROM node:16 # Create and change to the app directory. WORKDIR /usr/src/app # Copy application dependency manifests to the container image. # A wildcard is used to ensure both package.json AND package-lock.json are copied. COPY package*.json ./ # Install production dependencies. RUN npm install --only=production # Copy local code to the container image. COPY . . # Run the web service on container startup. CMD [ "node", "app.js" ]
Step 3: Build and Run Your Docker Container Locally
bashdocker build -t text-file-app . docker run -dp 3000:3000 text-file-app
Visit http://localhost:3000
in your browser to see the application.
Step 4: Deploy to Google Cloud
4.1 Install the Google Cloud SDK
Follow the instructions to install the Google Cloud SDK.
4.2 Initialize gcloud
bashgcloud init
Follow the on-screen instructions to log in and set up your GCP project.
4.3 Configure Docker to push to Google Container Registry
bashgcloud auth configure-docker
4.4 Tag and push your Docker image to Google Container Registry
Replace [PROJECT-ID]
with your GCP project ID.
bashdocker tag text-file-app gcr.io/[PROJECT-ID]/text-file-app docker push gcr.io/[PROJECT-ID]/text-file-app
4.5 Deploy your container to Google Cloud Run
bashgcloud run deploy --image gcr.io/[PROJECT-ID]/text-file-app --platform managed
Follow the prompts to enable the necessary APIs, choose a region, and allow unauthenticated invocations.
When you deploy your application to Google Cloud Run, it will automatically assign a URL to your application. You can use this URL to access your application from any web browser, anywhere.
How to Find Your Application URL
After you successfully deploy your application using the gcloud run deploy
command, Google Cloud Run will output several pieces of information, including the service URL. This URL is the public address you can use to access your application.
The URL typically looks something like this:
https://your-service-name-a1b2c3d4-uc.a.run.app
Here’s the general process to find or verify your application's URL on Google Cloud Run:
-
Using Google Cloud Console:
- Go to the Google Cloud Console.
- Navigate to the "Cloud Run" section under "Compute".
- Select your service.
- On the service details page, you'll see the URL at the top.
-
Using the gcloud Command:
- You can also use the
gcloud
command to list all services and their URLs:bashgcloud run services list
- This command will show a list of all your deployed services, along with their URLs and other details.
- You can also use the
Accessing Your App
Once you have your URL, simply enter it into your browser’s address bar. You should see your application's main page, which in the case of your Node.js app, would be a simple form for inputting text to save to a file.
Troubleshooting
If you encounter any issues accessing your app:
- Check Permissions: Ensure your Cloud Run service is configured to allow unauthenticated invocations if you want to access it without setting up authentication.
- Check Deployment Status: In the Google Cloud Console, check that the deployment was successful and the service is running.
- Firewall and Network: Ensure there are no firewalls or network settings that might block access to the URLs provided by Google Cloud Run.
This URL allows you to interact with your application just as you would locally, but now it's hosted in a scalable cloud environment.
Conclusion
This tutorial showed you how to create a simple Node.js application that saves text input to a file, how to Dockerize it, and how to deploy it to Google Cloud. You can now access your application globally via the URL provided by Google Cloud Run.