April 17, 2024

Host Your Website on Google Cloud (GCP) Without a Public IP: A Step-by-Step Guide

Introduction

Google Cloud Platform (GCP) offers a vast array of services for cloud computing, and it’s a popular choice among developers and businesses. However, one common challenge is the cost associated with reserving a public IP address. In this blog post, we’ll show you how to use a micro server with a regular IP address, install Docker, Portainer, and DuckDNS to set up a Dynamic Domain Name System (DDNS) for hosting your website or connecting to a web app on GCP. By following these steps, you can save money on public IP addresses while still enjoying the benefits of a dynamic DNS.

Table of Contents

  1. Prerequisites
  2. Setting Up a Micro Server
  3. Installing Docker
  4. Deploying Portainer
  5. Configuring DuckDNS
  6. Hosting Your Website or Web App
  7. Conclusion

1. Prerequisites

Before diving into the tutorial, ensure you have the following:

  • A Google Cloud Platform account.
  • A virtual machine (VM) instance running on GCP with a regular IP address. Otherwise follow this blog post.
  • Basic knowledge of the Linux command line.

2. Setting Up a MicroServer

To begin, set up a micro server on your GCP instance. You can use a lightweight Linux distribution like Ubuntu Server or Debian for this purpose. Ensure you have SSH access to your VM, as you’ll need it for the next steps.

3. Installing Docker

Docker is a containerization platform that simplifies deploying applications. Install Docker on your VM using the following commands:

# Update package lists
sudo apt update

# Install required packages for Docker
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common

# Add Docker GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

# Add Docker repository
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Update package lists again
sudo apt update

# Install Docker
sudo apt install -y docker-ce docker-ce-cli containerd.io

4. Deploying Portainer

Portainer is a user-friendly management interface for Docker. To deploy Portainer on your VM, run the following Docker command:

docker run -d -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock portainer/portainer-ce

Access Portainer by visiting your VM’s IP address on port 9000 (e.g., http://your_vm_ip:9000) in your web browser. Follow the on-screen instructions to set up an admin account and connect Portainer to your Docker installation.

5. Configuring DuckDNS

DuckDNS is a free dynamic DNS service that allows you to assign a domain name to your dynamic IP address. Follow these steps to set up DuckDNS:

  1. Visit the DuckDNS website (https://www.duckdns.org) and sign up for an account.
  2. Create a domain name (e.g., yourwebsite.duckdns.org) and take note of it.
  3. On your GCP VM, create a directory for DuckDNS and navigate to it:
mkdir ~/duckdns
cd ~/duckdns
  1. Use the following command to create a script for updating your dynamic IP address:
nano duck.sh

Insert the following code into duck.sh, replacing <your_token> and <your_domain> with your DuckDNS token and domain:

#!/bin/bash
echo url="https://www.duckdns.org/update?domains=<your_domain>&token=<your_token>&ip=" | curl -k -o ~/duckdns/duck.log -K -
  1. Make the script executable:
chmod +x duck.sh
  1. Test the script by running it:
./duck.sh

Your DuckDNS domain should now point to your VM’s IP address.

Alternate DuckDNS Container way:

5. Configuring DuckDNS with a Docker Container

Instead of running a script on your VM to update DuckDNS, you can use a Docker container that periodically updates your DuckDNS domain. Here’s how:

  1. Pull the DuckDNS Docker container image:
docker pull linuxserver/duckdns
  1. Create a Docker volume to store your DuckDNS configuration:
docker volume create duckdns-config
  1. Create and run a Docker container with the following command, replacing <your_token> and <your_domain> with your DuckDNS token and domain:
docker run -d \
  --name=duckdns \
  --restart=always \
  -e PUID=1000 \
  -e PGID=1000 \
  -e TZ=Your/Timezone \
  -e SUBDOMAINS=<your_domain> \
  -e TOKEN=<your_token> \
  -v duckdns-config:/config \
  linuxserver/duckdns

Explanation of the environment variables:

  • PUID and PGID should be set to your user and group IDs. You can find these with the id command.
  • TZ should be set to your timezone, e.g., America/New_York.
  • SUBDOMAINS should be set to your DuckDNS subdomain (e.g., yourwebsite if your domain is yourwebsite.duckdns.org).
  • TOKEN should be set to your DuckDNS token.
  1. Check the Docker container logs to ensure it’s working correctly:
docker logs duckdns

The container should start, and you should see log messages indicating successful updates.

With this Docker container running, your DuckDNS domain will be automatically updated with your VM’s IP address. You can now use this domain to access your hosted website or web app on Google Cloud Platform without the need for a reserved public IP address.

6. Hosting Your Website or Web App

Now that you have a DDNS domain, you can use it to host your website or web app on your GCP VM. Here’s how:

  1. Create a Docker container for your website or web app.
  2. Expose the necessary ports for your application, such as port 80 for HTTP.
  3. Use Portainer or Docker Compose to manage your containers easily.
  4. Update your DuckDNS script (duck.sh) to run periodically (e.g., using a cron job) to keep your DDNS domain up to date with your VM’s IP address.

7. Conclusion

By following these steps, you can host your website or web app on Google Cloud Platform without the need for a reserved public IP address. Instead, you’ll use a regular IP address and a Dynamic DNS service like DuckDNS to give your application a user-friendly domain name. This approach can help you save on costs while still enjoying the benefits of hosting your projects on GCP.

Remember to monitor your VM’s IP changes and update your DuckDNS script accordingly to ensure your domain name always points to the correct IP address. Happy hosting!

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
WordPress Appliance - Powered by TurnKey Linux