Expose Your Local App to the Internet with Cloudflare Tunnel (Linux Version)

Expose your app to the internet with Cloudflare Tunnel (Linux version)

Cloudflare Tunnel provides a secure way to expose your locally-hosted applications to the internet without opening ports or configuring complex network settings. This guide will walk you through the process of setting up Cloudflare Tunnel for your app on Linux, using either the CLI tool or Docker.

Prerequisites

  • A Cloudflare account
  • Your application running on localhost
  • Linux (any modern distribution)

Option 1: Using cloudflared CLI

Steps to Expose Your App

  1. Install cloudflared

    • Download the latest Linux executable from the official Cloudflare website.
    • Move the file to a location in your PATH, e.g., /usr/local/bin/.
  2. Authenticate cloudflared Open Terminal and run:

    cloudflared tunnel login
  3. Create a Tunnel

    cloudflared tunnel create my-app-tunnel
  4. Configure Your Tunnel Create a configuration file config.yml in your desired directory:

    url: http://localhost:3000
    tunnel: my-app-tunnel
    credentials-file: /path/to/credentials.json
  5. Start the Tunnel

    cloudflared tunnel run my-app-tunnel
  6. Route Traffic to Your Tunnel

    cloudflared tunnel route dns my-app-tunnel myapp.yourdomain.com

Option 2: Using Docker

Steps to Expose Your App

  1. Install Docker Follow the instructions on the official Docker website.

  2. Create a Docker network

    docker network create cloudflared
  3. Run cloudflared in Docker

    docker run -d --name cloudflared --network cloudflared cloudflare/cloudflared:latest tunnel --no-autoupdate run --token YOUR_TUNNEL_TOKEN

    Replace YOUR_TUNNEL_TOKEN with the token obtained from your Cloudflare dashboard.

  4. Connect your app to the network When running your app container, add it to the same network:

    docker run -d --name myapp --network cloudflared myapp:latest
  5. Configure Cloudflare DNS In your Cloudflare dashboard, add a CNAME record pointing to your tunnel's address.

Benefits of Using Cloudflare Tunnel

  • Secure exposure without opening ports
  • End-to-end encryption
  • DDoS protection
  • Easy setup and management
  • Flexibility to use CLI or Docker

By following either of these methods, you can safely and easily share your localhost applications with the world, leveraging Cloudflare's robust infrastructure for security and performance on your Linux machine.

Troubleshooting

  • If you encounter "command not found" errors with the CLI method, ensure cloudflared is in your system PATH.
  • For Docker issues, make sure Docker is running and you have the necessary permissions.
  • If you face connection issues, check your firewall settings to allow cloudflared or Docker to access the network.

Thank you for reading! I hope you found this post informative and helpful.

If you have any questions or feedback, feel free to reach out to me on Twitter. I always appreciate hearing from readers and am happy to help with any queries.

Stay tuned for more posts, and happy coding!

Also read