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:
1.2 Install Express:
1.3 Create an app.js
file:
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
:
Step 3: Build and Run Your Docker Container Locally
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
Follow the on-screen instructions to log in and set up your GCP project.
4.3 Configure Docker to push to Google Container Registry
4.4 Tag and push your Docker image to Google Container Registry
Replace [PROJECT-ID]
with your GCP project ID.
4.5 Deploy your container to Google Cloud Run
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: - 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.