Home

Flask app on mikrus

Complete Guide: Running Docker Flask App on mikr.us VPS

This comprehensive guide will walk you through the process of setting up a simple Flask “Hello, World!” application using Docker on your mikr.us VPS, and configuring it with a subdomain for easy access.

Table of Contents

  1. Understanding mikr.us VPS
  2. SSH Access to Your Server
  3. Installing Docker
  4. Creating the Flask Application
  5. Building and Running the Docker Container
  6. Setting Up the Subdomain
  7. Troubleshooting
  8. Useful Commands
  9. Advanced Configuration

Understanding mikr.us VPS

mikr.us VPS has some specific features to be aware of:

SSH Access to Your Server

Connect to your mikr.us VPS using SSH:

ssh username@server.mikr.us -p 10XXX

Replace username with your mikr.us username and XXX with your server number. For example, if your server is number 123, the port would be 10123.

Installing Docker

Update system packages and install Docker:

# Update system packages
sudo apt update && sudo apt upgrade -y

# Install Docker using the official script
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

# Add your user to the docker group to avoid using sudo
sudo usermod -aG docker $USER

# Apply the group changes without logging out
newgrp docker

# Verify Docker installation
docker --version

Creating the Flask Application

  1. Create a project directory:
mkdir flask-hello-world
cd flask-hello-world
  1. Create the Flask application file (app.py):
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "Hello, world!"

if __name__ == "__main__":
    # Important: Use "::" to listen on IPv6 for mikr.us compatibility
    app.run(host="::", port=5000, debug=False)
  1. Create a requirements.txt file:
Flask==3.0.0
  1. Create a Dockerfile:
FROM python:3.11-slim

# Set working directory
WORKDIR /app

# Copy requirements first for better caching
COPY requirements.txt .

# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY app.py .

# Expose port
EXPOSE 5000

# Run the application
CMD ["python", "app.py"]

Building and Running the Docker Container

Build the Docker image and run the container:

# Build the Docker image
docker build -t flask-hello-world .

# Run the container
docker run -d -p 5000:5000 --name my-flask-app flask-hello-world

# Check if the container is running
docker ps

Setting Up the Subdomain

Use the mikr.us domena command to create a subdomain that points to your Flask app:

Option 1: Random Subdomain

domena 5000

This will create a random subdomain like abc123.byst.re pointing to port 5000.

Option 2: Custom Subdomain

domena myflaskapp.byst.re 5000

This will create myflaskapp.byst.re pointing to port 5000.

After running the command, you’ll receive a URL for your subdomain. You can test your application by accessing it in a web browser or using curl:

curl your-subdomain.byst.re

You should see “Hello, world!” as the response.

Troubleshooting

Subdomain Not Created

If the subdomain isn’t being created, check:

  1. IPv6 Listening: Make sure your Flask app is listening on IPv6 (::), not IPv4 (0.0.0.0)
  2. App Running: Verify the Docker container is running with docker ps
  3. No 50x Errors: Check the logs for any errors with docker logs my-flask-app

Permission Denied for Docker

If you get permission errors when running Docker commands:

sudo usermod -aG docker $USER
newgrp docker

Port Already in Use

If port 5000 is already in use:

  1. Choose a different port
  2. Update both the Flask app and Docker run command
  3. Update the subdomain command to use the new port

Useful Commands

Docker Management

# View logs from the container
docker logs my-flask-app

# Stop the container
docker stop my-flask-app

# Remove the container
docker rm my-flask-app

# Remove the image
docker rmi flask-hello-world

# Access container shell
docker exec -it my-flask-app /bin/bash

Testing Your Application

# Test application from within the server
curl -6 http://[::1]:5000

# Test from outside
curl https://your-subdomain.byst.re

Advanced Configuration

Persisting Data

To persist data between container restarts, you can use Docker volumes:

docker run -d -p 5000:5000 -v $(pwd)/data:/app/data --name my-flask-app flask-hello-world

Automatic Restart

Ensure your container automatically restarts if the server reboots:

docker run -d -p 5000:5000 --restart unless-stopped --name my-flask-app flask-hello-world

Using Environment Variables

To pass configuration to your Flask app using environment variables:

docker run -d -p 5000:5000 -e DEBUG=False -e SECRET_KEY=mysecretkey --name my-flask-app flask-hello-world

Then in your app.py, you can use:

import os
debug_mode = os.environ.get('DEBUG', 'False') == 'True'
secret_key = os.environ.get('SECRET_KEY', 'default-secret-key')

Docker Compose (Optional)

For more complex applications, you might want to use Docker Compose. Create a docker-compose.yml file:

version: '3'
services:
  web:
    build: .
    ports:
      - "5000:5000"
    restart: unless-stopped
    environment:
      - DEBUG=False
      - SECRET_KEY=mysecretkey

Then run:

docker-compose up -d

Conclusion

You now have a fully functional Flask application running in a Docker container on your mikr.us VPS, accessible via a custom subdomain. This setup is easily expandable for more complex applications, and the Docker container ensures your app runs in an isolated environment with all its dependencies.

Tags: VPS, Python, Flask, Mikrus