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
- Setting Up a Micro Server
- Installing Docker
- Deploying Portainer
- Configuring DuckDNS
- Hosting Your Website or Web App
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:
- Visit the DuckDNS website (https://www.duckdns.org) and sign up for an account.
- Create a domain name (e.g.,
yourwebsite.duckdns.org) and take note of it.
- On your GCP VM, create a directory for DuckDNS and navigate to it:
mkdir ~/duckdns cd ~/duckdns
- Use the following command to create a script for updating your dynamic IP address:
Insert the following code into
<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 -
- Make the script executable:
chmod +x duck.sh
- Test the script by running it:
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:
- Pull the DuckDNS Docker container image:
docker pull linuxserver/duckdns
- Create a Docker volume to store your DuckDNS configuration:
docker volume create duckdns-config
- Create and run a Docker container with the following command, replacing
<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:
PGIDshould be set to your user and group IDs. You can find these with the
TZshould be set to your timezone, e.g.,
SUBDOMAINSshould be set to your DuckDNS subdomain (e.g.,
yourwebsiteif your domain is
TOKENshould be set to your DuckDNS token.
- 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:
- Create a Docker container for your website or web app.
- Expose the necessary ports for your application, such as port 80 for HTTP.
- Use Portainer or Docker Compose to manage your containers easily.
- 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.
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!