# Setting Up an LXC Container for Docker in Proxmox (Redis Service) This guide will walk you through creating and configuring an LXC container in Proxmox that's optimized for running Docker and our Redis service. ## Step 1: Download the Ubuntu 22.04 LTS Template If you don't already have the Ubuntu 22.04 LTS template in your Proxmox server, you'll need to download it first: 1. Log in to your Proxmox web interface 2. Select your node in the server view 3. Go to the "Local" storage (or any storage configured for CT templates) 4. Click on the "Templates" button 5. In the template list, find "ubuntu-22.04-standard" in the list - If you don't see it, click on "Templates" and then search for "ubuntu-22.04" - If the template list is empty or doesn't show Ubuntu 22.04, you may need to refresh the list by clicking "Refresh" 6. Click on the template and then click "Download" 7. Wait for the download to complete ## Step 2: Create a new LXC Container in Proxmox 1. Log in to your Proxmox web interface 2. Select your node in the server view 3. Click "Create CT" to create a new container 4. Configure the basic settings: - **General**: - Node: (your Proxmox node) - CT ID: (choose an available ID, e.g., 102) - Hostname: redis-docker - Unprivileged container: Yes (checked) - Password: (set a secure password) - SSH Public Key: (optionally add your SSH key) - **Template**: - **Best choice**: Ubuntu 22.04 LTS (ubuntu-22.04-standard) - Reasons: - Excellent Docker compatibility - Long-term support until 2027 - Best documentation and community support for Docker - Most stable kernel features needed for containerization - Regular security updates - **Disks**: - Storage: (select your storage) - Disk size: At least 10GB (recommended 20GB+ for production) - **CPU**: - Cores: At least 2 (recommended 2-4 for production) - **Memory**: - Memory: At least 2GB (recommended 4GB+ for production) - Swap: 1GB - **Network**: - Name: eth0 - Bridge: vmbr0 (or your preferred bridge) - IP address: DHCP or static IP - IP version: IPv4 5. Click "Finish" to create the container ## Step 3: Configure the LXC Container for Docker After creating the container, you need to modify its configuration to support Docker: 1. Stop the container if it's running 2. From the Proxmox shell, run these commands to modify the container configuration: ```bash # Enable nesting and other required features pct set -features nesting=1,keyctl=1 ``` 3. Edit the container configuration file directly: ```bash nano /etc/pve/lxc/.conf ``` 4. Add these lines to the configuration file: ``` lxc.apparmor.profile: unconfined lxc.cgroup.devices.allow: a lxc.cap.drop: lxc.mount.auto: proc:rw sys:rw ``` 5. Start the container ## Step 4: Configure Network and Install Docker 1. Start the container and access its shell: ```bash pct start pct enter ``` 2. **IMPORTANT: Check if your network interface has an IP address:** ```bash ip a ``` If your eth0 interface doesn't show an IPv4 address (like 192.168.x.x), you need to configure it first: ```bash # For Proxmox LXC containers, configure networking from the Proxmox web interface: 1. Exit the container first with 'exit' command 2. In the Proxmox web interface, select your container from the left sidebar 3. Click 'Stop' to stop the container if it's running 4. Go to the 'Network' tab 5. If there's no network interface, click 'Create' to add one: - Name: eth0 - Bridge: vmbr0 (or your preferred bridge) - IPv4: DHCP (or Static with your preferred IP configuration) - IPv4/CIDR: (if using static IP, enter something like 192.168.1.100/24) - Gateway: (if using static IP, enter your gateway, e.g., 192.168.1.1) 6. If there's already a network interface, click 'Edit' and update the configuration 7. Click 'OK' to save the changes 8. Go back to the 'Summary' tab and click 'Start' to start the container 9. Click 'Console' to access the container # Alternatively, use the command line on the Proxmox host: # Stop the container pct stop # Configure networking (DHCP) pct set -net0 name=eth0,bridge=vmbr0,ip=dhcp # Or configure with static IP (replace with your network details) pct set -net0 name=eth0,bridge=vmbr0,ip=192.168.1.100/24,gw=192.168.1.1 # Start the container again pct start pct enter # Verify you now have an IP address ip a ``` 3. Fix network connectivity issues: ```bash # First, check if you can ping IP addresses ping -c 4 8.8.8.8 # If you can't ping IPs, check your network interface ip a # Check your container's network configuration cat /etc/network/interfaces # Check DNS configuration cat /etc/resolv.conf # Fix DNS by adding these entries to resolv.conf echo "nameserver 8.8.8.8 nameserver 8.8.4.4" > /etc/resolv.conf # Make the DNS changes persistent by editing the systemd-resolved configuration mkdir -p /etc/systemd/resolved.conf.d/ cat > /etc/systemd/resolved.conf.d/dns_servers.conf << EOF [Resolve] DNS=8.8.8.8 8.8.4.4 FallbackDNS=1.1.1.1 EOF # Restart networking and DNS services systemctl restart systemd-networkd systemctl restart systemd-resolved # Test DNS resolution host archive.ubuntu.com ``` 4. If DNS is still not working, try adding entries to /etc/hosts: ```bash # Add essential Ubuntu repositories to /etc/hosts cat >> /etc/hosts << EOF 185.125.190.36 archive.ubuntu.com 185.125.190.36 security.ubuntu.com EOF # Test if it works ping -c 2 archive.ubuntu.com ``` 5. Install basic tools: ```bash apt update && apt install -y curl wget apt-transport-https ca-certificates gnupg lsb-release ``` 6. Update the system: ```bash apt update && apt upgrade -y ``` 7. Install Docker using the official installation script: ```bash # Download the Docker installation script curl -fsSL https://get.docker.com -o get-docker.sh # Review the script (optional but recommended) less get-docker.sh # Run the installation script sh get-docker.sh ``` This script automatically detects your OS, adds the appropriate repositories, and installs Docker and its dependencies. 8. Install Docker Compose: ```bash curl -L "https://github.com/docker/compose/releases/download/v2.20.3/docker-compose-linux-$(uname -m)" -o /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose ``` 9. Verify the installations: ```bash docker --version docker-compose --version ``` 10. Enable Docker to start on boot: ```bash systemctl enable docker ``` ## Step 5: Deploy Redis using Docker Compose 1. Create a directory for your Redis service: ```bash mkdir -p /opt/redis-service cd /opt/redis-service ``` 2. Clone your Git repository: ```bash git clone . # Or manually create the files as described in the README.md ``` 3. Create a proper .env file with secure credentials: ```bash cp .env.example .env nano .env ``` 4. Start the Redis service: ```bash docker compose up --build -d ``` 5. Verify that the container is running: ```bash docker-compose ps docker logs redis-server ``` ## Step 6: Configure Firewall (Optional but Recommended) If you're using a firewall on your Proxmox host, make sure to allow traffic to port 6379: ```bash # For UFW ufw allow 6379/tcp # For iptables iptables -A INPUT -p tcp --dport 6379 -j ACCEPT ``` ## Step 7: Test the Connection From your external machine, test the connection to Redis: 1. Using redis-cli: ```bash redis-cli -h -p 6379 -a ``` 2. Once connected, test with a simple command: ``` PING ``` You should receive "PONG" as a response. ## Troubleshooting If you encounter issues: 1. Check container logs: ```bash docker-compose logs ``` 2. Verify network connectivity: ```bash telnet your-server-ip 6379 ``` 3. Check Docker service status: ```bash systemctl status docker ``` 4. Ensure the container has proper resources: ```bash docker stats ``` 5. Check Redis configuration: ```bash docker exec -it redis-server redis-cli -a CONFIG GET * ``` ## Maintenance 1. Backup your data regularly: ```bash # Create a backup directory mkdir -p /opt/redis-backups # Run the backup command docker exec redis-server redis-cli -a SAVE docker cp redis-server:/data/dump.rdb /opt/redis-backups/redis-backup-$(date +%Y%m%d%H%M%S).rdb ``` 2. Update your container: ```bash docker-compose pull docker-compose down docker-compose up -d ``` 3. Monitor your system resources: ```bash htop ``` 4. Check Redis metrics: ```bash docker exec -it redis-server redis-cli -a INFO ```