Andy Jarrett // Code. Develop. Create.

How to Run SVR.JS with Docker Compose

Man running towards mountains
Photo by Florian Olivo on Unsplash

TL;DR: Project code here: github.com/andyj/svrjs-with-docker

Why SVR.JS?

SVR.JS is a lightweight and efficient web server designed to serve static files effortlessly. It's great for scenarios where you need a quick and reliable solution to host static content without the overhead of traditional web servers.

By combining SVR.JS with Docker, you can eliminate installation dependencies and simplify deployment, making it an ideal choice for developers, testers, and small-scale projects.

How to Set It Up

Step 1: Docker Compose Configuration

version: '3.9'
services:
  svrjs:
    image: svrjs/svrjs:lts
    container_name: svrjs-server
    platform: linux/amd64
    ports:
      - "8080:80"  # Map port 8080 on host to port 80 in the container
    volumes:
      - ./www:/var/www/svrjs  # Mount the local `www` directory as the web root
      - ./svrjs-config.json:/etc/svrjs-config.json  # Mount custom config
    restart: always

This docker-compose.yml file defines the SVR.JS service. It maps port 8080 on the host to port 80 in the container and sets up volumes for the web root and configuration file.

Step 2: Configuration File

{
  "port": 80,
  "wwwroot": "/var/www/svrjs",
  "logging": {
    "enabled": true,
    "level": "info"
  }
}

Save this as svrjs-config.json. It configures SVR.JS to serve files from the specified directory and enables basic logging.

Step 3: Directory Structure

Create the following directory structure for your project:

svrjs-with-docker
├── README.md
├── docker-compose.yml
├── svrjs-config.json
└── www
    ├── index.html
    └── style.css

The www folder will hold your static files, such as index.html and style.css.

Step 4: Example HTML File

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>SVR.JS Test</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <h1>Welcome to SVR.JS</h1>
  <p>This static file is served by SVR.JS.</p>
</body>
</html>

Save this as index.html inside the www directory.

Step 5: Running the Server

In the root directory of your project, run the following command:

docker-compose up

After the container starts, visit http://localhost:8080 to see your static site in action.

Conclusion

SVR.JS, combined with Docker, provides a powerful and hassle-free way to serve static content. This setup is highly portable and easy to replicate across different environments, making it a valuable tool for developers of all levels.

I’m here, learning and working away. If you liked this content and want to keep me going, consider buying me a coffee. Your support keeps this site running and the coffee brewing! ☕️