Back to articles
AI & Cloud

Running Web Services in Isolation Without Docker: Leveraging Native Linux with systemd-nspawn 2

A detailed guide on running web services in complete isolation without Docker, leveraging native Linux with systemd-nspawn 2. Covers installation, configuration, and performance...

DPTCloud Engineering4 min read
Running Web Services in Isolation Without Docker: Leveraging Native Linux with systemd-nspawn 2

Introduction

In an era where containerization solutions like Docker are increasingly common, many users still seek methods to isolate services without relying on Docker. systemd-nspawn – a component of systemd – offers a native Linux approach to creating and managing lightweight containers using Linux namespaces and cgroups. In this guide, we will walk through how to run a web service in complete isolation without Docker, leveraging the power of systemd-nspawn 2 on modern Linux systems.

Why Choose systemd-nspawn Over Docker?

systemd-nspawn is a lightweight containerization tool integrated into systemd, enabling the creation of isolated containers based on Linux namespaces and cgroups. Compared to Docker, systemd-nspawn offers several advantages:

  • No need to install additional software beyond the Linux system.
  • Reduced performance overhead since it doesn’t run a daemon or use overlay layers.
  • Simpler configuration and control over container behavior.
  • Well-suited for environments requiring stability and resource optimization.

Prerequisites and Installation

To follow this guide, you will need:

  • A Linux server (Ubuntu 22.04 or later, or compatible distributions).
  • Root or sudo privileges.
  • systemd and systemd-nspawn already installed.

Run the following command to verify that systemd-nspawn is available:

systemd-nspawn --version

If the command returns a version number, you are ready to proceed. Otherwise, install it with:

sudo apt update
sudo apt install systemd-container

Running a Web Service in Isolation with systemd-nspawn

We will create an isolated container to run a simple web application (e.g., a Python Flask server).

1. Create Directory for the Container

mkdir -p /var/lib/machines/webapp
cd /var/lib/machines/webapp

2. Create Container File System

Run the following command to create a basic file system:

sudo systemd-nspawn -M webapp --boot

During initialization, you will be prompted to create a user and password. Once complete, log into the container using:

sudo systemd-nspawn -M webapp -u root

3. Install Web Server

Inside the container, install Python and Flask:

apt update
apt install python3 python3-pip -y
pip3 install flask

Create a file app.py:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello from systemd-nspawn container!"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)

4. Start the Application

python3 app.py

Automatically Running the Container with systemd

To start the container automatically at boot, create a service file:

sudo nano /etc/systemd/system/webapp.service

Insert the following content:

[Unit]
Description=Web App Container
After=network.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/systemd-nspawn -M webapp --boot
ExecStop=/usr/bin/systemd-nspawn -M webapp --kill
ExecStopPost=/usr/bin/systemd-nspawn -M webapp --halt

[Install]
WantedBy=multi-user.target

Enable and start the service:

sudo systemctl enable webapp.service
sudo systemctl start webapp.service

Verification and Testing

To verify the container is running:

sudo systemd-nspawn -l -M webapp

Access the application from the host:

curl http://localhost:8080

The response should be: Hello from systemd-nspawn container!

Troubleshooting

  • Container fails to start: Ensure systemd-nspawn is installed and systemd is running.
  • Service not accessible: Check firewall rules and ensure port 8080 is not blocked.
  • Container does not start automatically: Check the service file and review logs with journalctl -u webapp.service.

References

systemd-nspawn provides a native Linux method for isolating services, ideal for systems requiring high performance, reliability, and simplicity. This guide demonstrates how to deploy a web service in isolation without Docker, leveraging the power of systemd-nspawn 2.

Related infrastructure & engineering guides: High-RAM Cloud VPS for Self-Hosted LLMs & API Gateways

DPTCloud Tier III Infrastructure

High-RAM Cloud VPS Plans Optimized for AI, Databases & Cache

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02Recommended

VPS-4E

297.000 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe
ECO · vn-02

VPS-5E

614.900 ₫ / mo

10% VAT included

  • CPU: 8 vCPU
  • RAM: 16 GB
  • SSD: 100 GB NVMe
ADVANCED · vn-03Recommended

VPS-4T

548.900 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478