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
fromflaskimportFlaskapi=Flask(__name__)SERVER_HOST='0.0.0.0'SERVER_PORT=8081@api.route('/')defhello():return"Hello World!"# Execute server only for development
if__name__=='__main__':api.run(host=SERVER_HOST,port=SERVER_PORT,debug=True)
Get your installed packages in requirements format (requirements.txt) using pip freeze.
Dockerfile - dockerfile
# syntax=docker/dockerfile:1FROM python:3.9.5-slimWORKDIR /api# Copy and install Python requirements and web serverCOPY requirements.txt requirements.txtRUN pip install-r requirements.txt
RUN pip install gunicorn
# Copy filesCOPY api.py .# Web server portENV PORT 8081EXPOSE $PORT# Execute web serverCMD exec gunicorn --bind 0.0.0.0:$PORT \
api:api
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-stoppedlabels:-"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
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
Get your installed packages in requirements format (
requirements.txt
) using pip freeze.Dockerfile -
dockerfile
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
.👏 🎉 👏 that is awesome!
I'm super happy you were able to get it working and that my post was helpful!