🧠 Headless Raspberry Pi 4B (2GB RAM) Setup for Docker, k3s & API Hosting
The Raspberry Pi 4B with 2GB RAM is powerful enough for light container workloads and web API hosting — if you keep it lean and optimized. With a headless, terminal-only setup, we can turn this tiny board into a micro cloud server for APIs, automations, and more.
🧰 What You’ll Need
- Raspberry Pi 4B (2GB RAM)
- Raspberry Pi OS Lite (64-bit recommended)
- microSD card (16GB+ recommended)
- SSH access (headless)
- Ethernet or Wi-Fi connection
- Internet access
🛰️ Step 1: Headless Raspberry Pi Setup
1. Flash Raspberry Pi OS Lite (64-bit)
Download from: https://www.raspberrypi.com/software/operating-systems/
Use:
- Raspberry Pi Imager
- balenaEtcher
- Or
dd
command (advanced)
2. Enable SSH and Wi-Fi
On the /boot
partition of the SD card:
- Add an empty file named
ssh
- Add
wpa_supplicant.conf
:
country=US
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="Your_SSID"
psk="Your_PASSWORD"
}
Insert SD card and power on.
3. Connect via SSH
ssh pi@raspberrypi.local
# Or use Pi’s IP:
ssh pi@<ip-address>
🔐 Step 2: Initial Configuration
passwd # Change default password
sudo apt update && sudo apt upgrade -y
sudo raspi-config
In raspi-config
, set:
- Hostname
- Locale, timezone
- Enable SSH, I2C, SPI (as needed)
🛠️ Step 3: Essential Tools & Python Setup
Install development tools:
sudo apt install -y \
git curl wget build-essential \
python3 python3-pip python3-venv \
vim nano tmux htop neofetch
Set up Python environment:
python3 -m pip install --upgrade pip
python3 -m pip install virtualenv ipython
🐳 Step 4: Install Docker (Rootless Optional)
Option 1: Classic Docker Install
curl -sSL https://get.docker.com | sh
sudo usermod -aG docker $USER
Reboot or log out/in to apply group changes.
Test Docker:
docker run hello-world
Option 2: Lightweight Kubernetes (k3s)
Install k3s (server mode):
curl -sfL https://get.k3s.io | sh -
Check status:
sudo k3s kubectl get nodes
To use kubectl
without sudo
:
mkdir -p ~/.kube
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
sudo chown $USER:$USER ~/.kube/config
🔌 Step 5: Host Web API with FastAPI or Flask
Create virtual environment
python3 -m venv webenv
source webenv/bin/activate
pip install fastapi uvicorn flask
🔹 Option A: FastAPI Example
Create main.py
:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Hello from FastAPI on Raspberry Pi 4B!"}
Run it:
uvicorn main:app --host 0.0.0.0 --port 8000
🔹 Option B: Flask Example
Create app.py
:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello from Flask on Raspberry Pi 4B!"
Run:
python app.py
🛡️ Optional: Run as a Systemd Service
Create /etc/systemd/system/fastapi.service
:
[Unit]
Description=FastAPI App
After=network.target
[Service]
User=pi
WorkingDirectory=/home/pi/project
ExecStart=/home/pi/webenv/bin/uvicorn main:app --host 0.0.0.0 --port=8000
Restart=always
[Install]
WantedBy=multi-user.target
Enable & start:
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable fastapi
sudo systemctl start fastapi
📦 Step 6: Create Backup (Portable Snapshot)
Backup config and packages:
mkdir -p ~/pi-backup/etc-backup
cp ~/.bashrc ~/pi-backup/
cp -r ~/.config ~/pi-backup/
sudo cp -r /etc ~/pi-backup/etc-backup/
apt-mark showmanual > ~/pi-backup/manual-packages.txt
Compress for portability:
tar -czvf pi-backup.tar.gz pi-backup
♻️ Step 7: Restore on New Pi
- Copy
pi-backup.tar.gz
to new Pi and extract:
tar -xzvf pi-backup.tar.gz
- Run restore script:
#!/bin/bash
xargs sudo apt install -y < ~/pi-backup/manual-packages.txt
cp ~/pi-backup/.bashrc ~/
cp -r ~/pi-backup/.config ~/
✅ Final Notes & Tips
- Monitor containers with
docker ps
,docker stats
- Use
tmux
orscreen
for persistent SSH sessions - Open ports with
ufw
or use a reverse proxy (e.g. Caddy or Nginx) - Add a domain + SSL with
Caddy
for zero-config HTTPS
🚀 What’s Next?
Now your Raspberry Pi 4B is a fully container-ready API server!
You can now:
- 🎯 Deploy microservices with Docker or k3s
- 🌐 Host local dashboards, REST APIs, or even Telegram bots
- ☁️ Sync config/scripts with GitHub for future projects
Top comments (0)