DEV Community

Discussion on: Adding custom Docker containers to Appwrite

Collapse
 
javibonilla profile image
Javier Bonilla

Alex, thanks a lot for this, it is really useful!

Thanks to your help I managed to get this working for an API in Python Flask. This is the code for a simple example.

Python Flask API code - api.py

from flask import Flask
api = Flask(__name__)

SERVER_HOST = '0.0.0.0'
SERVER_PORT = 8081


@api.route('/')
def hello():
    return "Hello World!"


# Execute server only for development
if __name__ == '__main__':
    api.run(host=SERVER_HOST, port=SERVER_PORT, debug=True)
Enter fullscreen mode Exit fullscreen mode

Get your installed packages in requirements format (requirements.txt) using pip freeze.

Dockerfile - dockerfile

# syntax=docker/dockerfile:1

FROM python:3.9.5-slim

WORKDIR /api

# Copy and install Python requirements and web server
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
RUN pip install gunicorn

# Copy files
COPY api.py .

# Web server port
ENV PORT 8081
EXPOSE $PORT

# Execute web server
CMD exec gunicorn --bind 0.0.0.0:$PORT \
    api:api
Enter fullscreen mode Exit fullscreen mode

Docker compose file - docker-compose.yml

The docker compose remains the same, the only changes are for the custom service definition (only the networks configuration is needed) and don't forget to set your Docker image name: YOUR_IMAGE_NAME.

  appwrite-customApi:
    image: "<YOUR_IMAGE_NAME>"
    restart: unless-stopped
    labels:
        - "traefik.http.middlewares.portainerpathstrip.stripprefix.prefixes=/customApi/" # requests to this endpoint will be routed to our container 
        - "traefik.http.middlewares.portainerpathstrip.stripprefix.forceSlash=false"

        - "traefik.enable=true"
        - "traefik.constraint-label-stack=appwrite"
        - "traefik.docker.network=appwrite"
        - "traefik.http.services.appwrite-customApi.loadbalancer.server.port=8081"
        - "traefik.http.routers.appwrite-customApi-http.middlewares=portainerpathstrip"
        - traefik.http.middlewares.sslheader.headers.customrequestheaders.X-Forwarded-Proto = https
        - traefik.http.routers.appwrite-customApi-http.entrypoints=web
        - traefik.http.routers.appwrite-customApi-http.rule=PathPrefix(`/customApi`)
        - traefik.http.routers.appwrite-customApi-http.service=appwrite-customApi
        - "traefik.http.routers.appwrite-customApi-https.middlewares=portainerpathstrip"
        - traefik.http.routers.appwrite-customApi-https.entrypoints=websecure
        - traefik.http.routers.appwrite-customApi-https.rule=PathPrefix(`/customApi`)
        - traefik.http.routers.appwrite-customApi-https.service=appwrite-customApi
        - traefik.http.routers.appwrite-customApi-https.tls=true

    # customize the following properties based on your docker container

    # user: "node"
    # working_dir: /home/node/app
    # environment:
    #   - NODE_ENV=production
    #   - PORT=8081
    # volumes:
    #    - ../customApi/:/home/node/app
    # command: "npm run prod"

    networks:
        - appwrite
Enter fullscreen mode Exit fullscreen mode
Collapse
 
alexweininger profile image
Alex Weininger

👏 🎉 👏 that is awesome!

I'm super happy you were able to get it working and that my post was helpful!