# Setting Up an LXC Container for Docker in Proxmox This guide will walk you through creating and configuring an LXC container in Proxmox that's optimized for running Docker and our MongoDB service. ## Step 1: 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., 101) - Hostname: mongo-docker - Unprivileged container: Yes (checked) - Password: (set a secure password) - SSH Public Key: (optionally add your SSH key) - **Template**: - Select a recent Ubuntu or Debian template (e.g., ubuntu-22.04-standard) - **Disks**: - Storage: (select your storage) - Disk size: At least 20GB (recommended 40GB+ for production) - **CPU**: - Cores: At least 2 (recommended 4+ for production) - **Memory**: - Memory: At least 4GB (recommended 8GB+ for production) - Swap: 2GB - **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 2: 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 3: Install Docker inside the LXC Container 1. Start the container and access its shell: ```bash pct start pct enter ``` 2. Update the system: ```bash apt update && apt upgrade -y ``` 3. Install required packages: ```bash apt install -y apt-transport-https ca-certificates curl software-properties-common gnupg ``` 4. Add Docker's official GPG key and repository: For Ubuntu: ```bash curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null ``` For Debian: ```bash curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null ``` 5. Update the package database and install Docker: ```bash apt update apt install -y docker-ce docker-ce-cli containerd.io ``` 6. 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 ``` 7. Verify the installations: ```bash docker --version docker-compose --version ``` 8. Enable Docker to start on boot: ```bash systemctl enable docker ``` ## Step 4: Deploy MongoDB using Docker Compose 1. Create a directory for your MongoDB service: ```bash mkdir -p /opt/mongo-service cd /opt/mongo-service ``` 2. Clone your Git repository: ```bash git clone ssh://git@gitea.mehmetkaratay.com.tr:222/evyos-center-server/mongo-service.git . ``` 3. Create a proper .env file with secure credentials: ```bash cp .env .env.example nano .env ``` 4. Start the MongoDB service: ```bash docker-compose up -d ``` 5. Verify that the containers are running: ```bash docker-compose ps ``` ## Step 5: Configure Firewall (Optional but Recommended) If you're using a firewall on your Proxmox host, make sure to allow traffic to ports 27017 and 8081: ```bash # For UFW ufw allow 27017/tcp ufw allow 8081/tcp # For iptables iptables -A INPUT -p tcp --dport 27017 -j ACCEPT iptables -A INPUT -p tcp --dport 8081 -j ACCEPT ``` ## Step 6: Test the Connection From your external machine, test the connection to MongoDB: 1. Using MongoDB Compass or another MongoDB client, connect to: ``` mongodb://admin:yourpassword@your-server-ip:27017/?authSource=admin&replicaSet=rs0 ``` 2. Access Mongo Express in your web browser: ``` http://your-server-ip:8081 ``` ## Troubleshooting If you encounter issues: 1. Check container logs: ```bash docker-compose logs ``` 2. Verify network connectivity: ```bash telnet your-server-ip 27017 ``` 3. Check Docker service status: ```bash systemctl status docker ``` 4. Ensure the container has proper resources: ```bash docker stats ``` ## Maintenance 1. Backup your data regularly: ```bash docker-compose exec mongodb mongodump --out /data/backup ``` 2. Update your containers: ```bash docker-compose pull docker-compose down docker-compose up -d ``` 3. Monitor your system resources: ```bash htop ```