Sign inSign up

rafaelrodriguess/weather-api

By rafaelrodriguess

•Updated about 2 months ago

An API that retrieves current and forecast weather data.

Image
Languages & frameworks
API management
Web servers
1

8.3K

rafaelrodriguess/weather-api repository overview

⁠Weather API - Rafael Rodrigues

Docker Image Size Python Version

This is the official image for the Weather API application, a robust REST API built with Python (Flask). The image is optimized using python:3.13-alpine, ensuring high performance, reduced size, and a secure environment for production workloads.

⁠🚀 About this Image

The image packages all the application code and dependencies, making it ready to run in any containerized environment. It is configured following Docker best practices:

  • Uses an entrypoint.sh script to automatically run database migrations (flask db upgrade) before starting the server.
  • Automatically initializes internal telemetry scripts.
  • Runs the application using gunicorn (WSGI server) optimized with 2 workers, request limiters (max-requests, max-requests-jitter), and timeouts for maximum stability.
  • Exposes port 5000.
  • Executes all processes using a non-root user (fuser), providing an essential layer of security.
  • Graceful degradation: While a PostgreSQL database is required for full operation, the application runs perfectly without Redis, automatically degrading to work without an external cache broker.
  • Documentation available at http://localhost:5000

⁠🌐 Live Demo & Documentation

You can also test the endpoints, read the API documentation, and see this exact API running in production at: https://weatherapi.lat⁠

⁠🛠️ Basic Usage (API + PostgreSQL)

The application requires a PostgreSQL database to function. The easiest way to start is by running the API alongside the database container, without requiring Redis.

Create a docker-compose.yml with the following content:

services:
  db:
    image: postgres:17
    environment:
      POSTGRES_DB: weather_db
      POSTGRES_USER: postgres_user
      POSTGRES_PASSWORD: your_secure_password
    volumes:
      - db_weather_data:/var/lib/postgresql/data
    networks:
      - weather_network

  web:
    image: rafaelrodriguess/weather-api:latest
    ports:
      - "5000:5000"
    depends_on:
      - db
    environment:
      - SECRET_KEY="your-super-secure-secret-key"
      - JWT_SECRET_KEY="your-jwt-secret-key"
      - DATABASE_URL=postgresql://postgres_user:your_secure_password@db:5432/weather_db
    networks:
      - weather_network

volumes:
  db_weather_data:

networks:
  weather_network:

⁠⚙️ Environment Variables (.env)

You can configure the application's behavior using a .env file. Place it in the same directory as your docker-compose.yml:

# Application Settings
SECRET_KEY="your-super-secure-secret-key"
JWT_SECRET_KEY="your-jwt-secret-key"

# External Services (Mandatory Database)
DATABASE_URL="postgresql://postgres_user:postgres_password@db:5432/weather_db"

# (Optional) Cache Broker - If omitted, app runs without Redis
REDIS_URL="redis://redis:6379/0"

# Postgres container creation variables
POSTGRES_USER=postgres_user
POSTGRES_PASSWORD=postgres_password
POSTGRES_DB=weather_db

⁠🏗️ Robust Usage (API + PostgreSQL + Redis)

For a highly available production environment, it's recommended to run the API alongside dedicated PostgreSQL and Redis containers.

Create a docker-compose.yml with this advanced setup:

services:
  db:
    image: postgres:17
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - db_weather_data:/var/lib/postgresql/data
    networks:
      - weather_network

  redis:
    image: redis:7-alpine
    networks:
      - weather_network

  web:
    image: rafaelrodriguess/weather-api:latest
    ports:
      - "5000:5000"
    depends_on:
      - db
      - redis
    env_file:
      - ./.env
    networks:
      - weather_network

volumes:
  db_weather_data:

networks:
  weather_network:

⁠🔒 Security and Performance Focus

This image was carefully designed for production usage:

  • Alpine Linux Base: Drastically reduces the image footprint and attack surface.
  • Non-root user (fuser): Ensures the app doesn't have root privileges, limiting potential internal security vulnerabilities.
  • Production WSGI Server: Uses Gunicorn instead of Flask's built-in development server, configured to prevent memory leaks and handle multiple concurrent connections.
  • Python Optimization: Default environment variables PYTHONDONTWRITEBYTECODE=1 and PYTHONUNBUFFERED=1 are active to prevent caching clutter and ensure immediate logging output.

Developed by Rafael Rodrigues.

Tag summary

Content type

Image

Digest

sha256:5dc3a35e3…

Size

43.6 MB

Last updated

2 months ago

docker pull rafaelrodriguess/weather-api