mongo-service/proxmox-lxc-setup.md

5.3 KiB

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:
# Enable nesting and other required features
pct set <container-id> -features nesting=1,keyctl=1
  1. Edit the container configuration file directly:
nano /etc/pve/lxc/<container-id>.conf
  1. 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
  1. Start the container

Step 3: Install Docker inside the LXC Container

  1. Start the container and access its shell:
pct start <container-id>
pct enter <container-id>
  1. Update the system:
apt update && apt upgrade -y
  1. Install required packages:
apt install -y apt-transport-https ca-certificates curl software-properties-common gnupg
  1. Add Docker's official GPG key and repository:

For Ubuntu:

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:

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
  1. Update the package database and install Docker:
apt update
apt install -y docker-ce docker-ce-cli containerd.io
  1. Install Docker Compose:
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
  1. Verify the installations:
docker --version
docker-compose --version
  1. Enable Docker to start on boot:
systemctl enable docker

Step 4: Deploy MongoDB using Docker Compose

  1. Create a directory for your MongoDB service:
mkdir -p /opt/mongo-service
cd /opt/mongo-service
  1. Clone your Git repository:
git clone ssh://git@gitea.mehmetkaratay.com.tr:222/evyos-center-server/mongo-service.git .
  1. Create a proper .env file with secure credentials:
cp .env .env.example
nano .env
  1. Start the MongoDB service:
docker-compose up -d
  1. Verify that the containers are running:
docker-compose ps

If you're using a firewall on your Proxmox host, make sure to allow traffic to ports 27017 and 8081:

# 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:

    docker-compose logs
    
  2. Verify network connectivity:

    telnet your-server-ip 27017
    
  3. Check Docker service status:

    systemctl status docker
    
  4. Ensure the container has proper resources:

    docker stats
    

Maintenance

  1. Backup your data regularly:

    docker-compose exec mongodb mongodump --out /data/backup
    
  2. Update your containers:

    docker-compose pull
    docker-compose down
    docker-compose up -d
    
  3. Monitor your system resources:

    htop