Micro-Applications with Docker and Cloudflare

Combining Docker Compose with Cloudflare Tunneld (Argo Tunnel) containers provides an easy mechanism for application deployment, without the need for NAT, or exposing a host directly to the Internet. This is a great option for those that like to host from their home which commonly has a dynamic IP address.

This post assumes you have a working Linux / Docker / Docker Compose installation.

services:
  cf:
    container_name: example_cf
    image: cloudflare/cloudflared
    restart: always
    command: tunnel run
    environment:
      TUNNEL_TOKEN: TOKEN-FROM-CLOUDFLARE

Here is a configured Cloudflare container within a stack.

This can be combined with web applications to create a functional stack:

services:
  cf:
    container_name: example_cf
    image: cloudflare/cloudflared
    restart: always
    command: tunnel run
    environment:
      TUNNEL_TOKEN: TOKEN-FROM-CLOUDFLARE
  web:
    container_name: example_web
    image: nginx:alpine
    restart: always

Cloudflare combined with an Alpine based nginx web server.

The Cloudflare tunnel configuration can be found within the Zero Trust dashboard, under Networks.

Work through the process, giving the tunnel a name of your choosing. You should then see a page that gives you environment choices and commands to install and run a connector.

docker run cloudflare/cloudflared:latest tunnel --no-autoupdate run --token 123456

An example of the output for a docker environment.

We won’t be using any of these commands, however the –token referenced here should be referenced in Docker Compose file.

services:
  cf:
    container_name: example_cf
    image: cloudflare/cloudflared
    restart: always
    command: tunnel run
    environment:
      TUNNEL_TOKEN: 123456

    container_name: example_web
    image: nginx:alpine
    restart: always

An example with the token in place. Please Note: This isn’t a real token.

Next we need to configure a route for Cloudflare. Below is an example, drawing on the stack we have defined.

After the route is configured, we can then proceed with issuing your `docker compose up -d` command to launch the stack and containers. The Cloudflare Tunnel will connect up to Cloudflare and act as a reverse proxy to the backend web server. This connection uses UDP/7844.

Cloudflare-Working_Tunnel.jpg

DavisSystem

Consolidated Notes From the Desk of Sean Davis.


Micro-Applications with Docker and Cloudflare

By Sean, 2024-02-20