Reasons why I love Containers. With Cloudflared and AI Apps.

Reasons why I love Containers. With Cloudflared and AI Apps.

January 4, 2025

Container Tech is Cool

Containers are an essential tool for simplifying self-hosting software. They give us:

  • Portability: Containers ensure that your software runs consistently across different environments (development, testing, production).
  • Scalability: Easily replicated and orchestrated, containers allow for dynamic scaling, particularly in cloud environments.
  • Isolation: Containers isolate applications, preventing conflicts between dependencies.
  • Efficiency: Containers share the host OS kernel, making them more resource-efficient and faster than virtual machines.

What Are Containers?

Containers are lightweight, portable, and self-contained units of software that package an application along with its dependencies, libraries, and configurations.

This makes it possible to run the software seamlessly across different environments.

Unlike virtual machines, containers share the host system’s OS kernel, making them faster to deploy and more efficient.

Prerequisites for Containers

Before you can use containers, you need to set up the required tools. Below are the steps for setting up Docker and Podman, two of the most popular containerization tools.

ℹ️
You can use the SelfHosting script I prepared here

Docker Setup

  1. Install Docker:
    • On Linux:
sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
  1. Verify Installation:
    • Check Docker version:
docker --version
  • Run a simple container to test:
docker run hello-world
  1. Post-Installation:
    • To avoid using sudo with Docker commands on Linux:
sudo usermod -aG docker $USER
  1. Start Using Docker:
    • Common commands: docker build, docker run, docker ps. Try pulling images from Docker Hub (e.g., docker pull ubuntu).

Podman Setup

  1. Install Podman:
    • On Linux:
sudo apt update
sudo apt install podman
  1. Verify Installation:
    • Check the Podman version:
podman --version
  1. Test Podman:
    • Run a test container:
podman run hello-world
  1. Post-Installation:
    • Add your user to the Podman group:
sudo usermod -aG podman $USER
  1. Start Using Podman:
    • Podman commands are similar to Docker’s. Start using commands like podman build, podman run, and podman ps.
UI Tools for Managing Containers 📌

While Docker and Podman are command-line tools, there are several UI tools that make managing containers easier and more intuitive. Here are some popular options:

  1. Portainer
  • Platform: Linux, Windows, macOS
  • Portainer is a popular open-source web-based UI for managing Docker and Podman containers.
  • Installation Command:
    sudo docker run -d -p 8000:8000 -p 9000:9000 --name=portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce
    Access Portainer at: http://localhost:9000.

Portainer Docker UI

  1. Dockge
  • Platform: Docker-based UI for container management.
  • Installation Command:
    docker run -d \
      --name dockge \
      --restart unless-stopped \
      -p 5001:5001 \
      -v /var/run/docker.sock:/var/run/docker.sock \
      -v /home/your_user/Desktop/Dockge/data:/app/data \
      louislam/dockge:1
    Access Dockge at: http://localhost:5001.
  1. Cockpit
  • Platform: Linux (CentOS, Fedora, Ubuntu)
  • Cockpit is a server management tool with a web-based UI for container management.
  • Installation Command:
    sudo apt install cockpit
    sudo systemctl enable --now cockpit.socket
  1. Kitematic (for Docker)
  • Platform: Windows/macOS
  • Kitematic is a simple GUI for Docker that makes it easier to create, configure, and run containers.
  1. Yacht
  • Platform: Web-based UI for Docker containers.
  • Learn more about Yacht from this video or check its GitHub page.

These tools simplify the process of managing containers, especially if you prefer not to work exclusively with the command line.

Setting up Docker or Podman is straightforward, and with UI tools like Portainer, Dockge, and others, managing your containers has never been easier.

Easy SelfHosting with Containers

Exposing Apps Safely with Cloudflare Tunnels

Learn how to securely expose your self-hosted services (Including AI Apps!) using Cloudflare’s Zero Trust Tunnel with Docker

What is Cloudflare Zero Trust Tunnel?

Cloudflare Zero Tunnel creates a secure connection between your local machine and Cloudflare’s global edge network.

It allows private resources to be accessed without exposing them to the public internet, providing enhanced security and privacy.

Key Benefits:

  • Improved Security: End-to-end encryption (TLS 1.3) for safe data transmission.
  • Increased Privacy: Avoid exposing private resources to the public internet.
  • Global Accessibility: Access your services securely from anywhere.
  • Faster Connections: QUIC protocol for more reliable and faster connections.

Docker allows you to containerize and deploy applications easily.

By combining Docker with Cloudflare Zero Tunnel, we can securely expose services on the internet.

  1. Create a Docker container for your service
    Use Docker to package your application, such as a Python-based web service, in a container.

  2. Install Cloudflare Zero Tunnel Client

    • Use the Cloudflare cloudflared Docker image.
    • Configure the Cloudflare Zero Tunnel by setting up a token in the Cloudflare Dashboard.
  3. Set Up Cloudflare Tunnel with Docker

    • Run cloudflared as a container linked to your own services:
docker network create tunnel
version: '3.8'

services:
  cloudflared:
    image: cloudflare/cloudflared:latest
    container_name: cloudflared
    command: tunnel --no-autoupdate run --token token_from_cloudflare_webUI #see video below
    networks:
      - tunnel #a name for the Cloudflare Network
    restart: always #unless-stopped

networks:
  tunnel: #a name for the Cloudflare Network
  1. Connect Your Service to the Tunnel Network
    Add your service container to the Cloudflare tunnel network:
docker network connect tunnel your_service_container
  1. Configure a Public Hostname on Cloudflare
    In the Cloudflare Dashboard, create a public hostname for your service, linking it to the service container using the format your_service_name:docker_port.
Is my home IP safe with Cloudflare? 📌

How to check my local IP address?

ifconfig

How to check the exposed service IP? Cloudflare Tunnel ensures your service is securely exposed without revealing your local IP:

curl your_selected_domain.com

By following this guide, you can expose your services securely without the need for port forwarding or exposing your home IP.

Cloudflare’s Zero Trust Tunnel with Docker is a powerful solution to enhance both security and accessibility of your self-hosted applications.

ℹ️
If you are using a Cloud/VPS, you dont need to PortForward and can get HTTPs with NGINX

Creating Containers

ℹ️
You can create Containers Automatically thanks to CI/CD Tools, like Github Actions

Just recently, we can use not only x86 VM with github to build our containers, but also native ARM (we can skip emulating it with QEMU).

if you want to create containers on your own hardware (but automatically), you can use:

  1. Github/Gitlab Runners
  2. Gitea Act Runner
  3. Jenkins CI/CD
  4. Argo CI/CD
Is my home IP safe with Cloudflare? 📌

Argo CD is a declarative, GitOps continuous delivery tool specifically designed for Kubernetes.

It operates based on the principle that your Git repository serves as the single source of truth for your desired application state.

How Argo CD works?

  1. Define Desired State: You define your desired application state (e.g., deployments, services, configurations) in your Git repository using Kubernetes manifests.
  2. Argo CD Monitors: Argo CD continuously monitors your Git repository for changes.
  3. Automatic Synchronization: When changes are detected, Argo CD automatically synchronizes your Kubernetes cluster to match the desired state defined in Git.
  4. Continuous Reconciliation: Argo CD constantly compares the actual state of your cluster with the desired state in Git. If discrepancies are found, it takes corrective actions to bring the cluster back in sync.

Comparison with Jenkins and GitHub Actions:

FeatureArgo CDJenkinsGitHub Actions
FocusKubernetes-native GitOps CDGeneral-purpose CI/CDCI/CD specifically for GitHub
Deployment ModelPull-based (Git as source of truth)Push-based (CI/CD system triggers deployments)Push-based
StrengthsExcellent for Kubernetes deployments, strong GitOps implementation, declarative approachHighly flexible and customizable, vast plugin ecosystemTight integration with GitHub, user-friendly interface
WeaknessesPrimarily focused on Kubernetes, might have a steeper learning curve for non-Kubernetes usersCan become complex for intricate pipelines, potential for configuration driftLimited to GitHub repositories

In essence:

  • Argo CD excels in Kubernetes environments, emphasizing GitOps principles for streamlined and reliable deployments.The bestThe best choice depends on your specific requirements, team expertise, and the complexity of your CI/CD pipelines. choice depends on your specific requirements, team expertise, and the complexity of your CI/CD pipelines.

  • Jenkins is a versatile platform suitable for various CI/CD needs, offering extensive customization options.

  • GitHub Actions provides a user-friendly, integrated solution for CI/CD workflows within the GitHub ecosystem.

Using Containers

ℹ️
Make sure to understand docker volumes vs bind mounts!

AI Apps with Containers and Cloudflare Tunnels


FAQ

Cloudflare alternatives?

https://github.com/rapiz1/rathole

A lightweight and high-performance reverse proxy for NAT traversal, written in Rust. An alternative to frp and ngrok.

SelfHosting Best Practices

What about NIX?

Recently I heard about Flox and NIX…