{"id":20455269,"url":"https://github.com/alexmhack/django-k8s","last_synced_at":"2026-05-07T17:32:04.050Z","repository":{"id":229298281,"uuid":"775958919","full_name":"Alexmhack/django-k8s","owner":"Alexmhack","description":"Deploying Django v5 using Kubernetes on Digital Ocean","archived":false,"fork":false,"pushed_at":"2024-03-23T12:12:39.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-04-08T02:14:13.185Z","etag":null,"topics":["django5","fastapi","k8s","k8s-cluster","learning","python3","sample-code","tutorial"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Alexmhack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-03-22T11:47:26.000Z","updated_at":"2024-03-23T12:13:50.000Z","dependencies_parsed_at":null,"dependency_job_id":"78d33ed5-debb-4975-8658-9196263caa5e","html_url":"https://github.com/Alexmhack/django-k8s","commit_stats":null,"previous_names":["alexmhack/django-k8s"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Alexmhack/django-k8s","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexmhack%2Fdjango-k8s","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexmhack%2Fdjango-k8s/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexmhack%2Fdjango-k8s/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexmhack%2Fdjango-k8s/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Alexmhack","download_url":"https://codeload.github.com/Alexmhack/django-k8s/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Alexmhack%2Fdjango-k8s/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32748502,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-07T02:14:30.463Z","status":"ssl_error","status_checked_at":"2026-05-07T02:14:29.405Z","response_time":62,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django5","fastapi","k8s","k8s-cluster","learning","python3","sample-code","tutorial"],"created_at":"2024-11-15T11:18:24.336Z","updated_at":"2026-05-07T17:32:04.045Z","avatar_url":"https://github.com/Alexmhack.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# django-k8s\nDeploying Django v5 using Kubernetes on Digital Ocean\n\n### Docker Compose\n\nUsing `docker compose` we can define and run multi container applications. For example we want to run our Django application as well as \nour PostgreSQL database then we can define multiple services in *docker-compose.yml* file and run them simultaneously in multiple \ncontainers.\n\nA *docker-compose.yml* file consists of services, in our case there would be two services as of now,\n\n1. Django project itself running through gunicorn \u0026 asgi\n2. PostgreSQL DB connecting with Django project\n\nWe can later on add more services and do `docker compose up --build` and that build and make our defined services up and running.\n\nFor running our Django project we have defined some bash scripts like,\n\n1. *migrate.sh* -\u003e For running our Django migrations everytime we spawn a new container.\n2. *entrypoint.sh* -\u003e For actually running our Django application using gunicorn\n\nWe can either define all these files in the *Dockerfile* in `RUN` commands as `chmod +x /app/migrate.sh \u0026\u0026 chmod +x /app/entrypoint.sh \n\u0026 sh /app/migrate.sh \u0026\u0026 sh /app/entrypoint.sh` but these seems too much in a place and if anything breaks then it will be hard to debug, so we will be running the migrate(setup commands) and the actual entrypoint of our app(gunicorn server) separately inside \n*docker-compose.yml*\n\nNow to be able to run these commands from *docker-compose.yml* we need to use some lines of *yml* syntax that will allow us to run \nmultiple bash commands,\n\n```\nversion: \"3.9\"\n\nservices:\n  web:\n    depends_on: postgres_db\n    build:\n      dockerfile: Dockerfile\n      context: ./web\n    image: django-k8s:1.10  # custom name of our image\n    env_file:\n      - web/.env\n    environment:\n      - PORT=8080\n    ports:\n      - 8000:8080\n    command:\n      - /bin/sh\n      - -c\n      - |\n        chmod +x /app/migrate.sh\n        sh /app/migrate.sh\n        sh /app/entrypoint.sh\n\n  postgres_db:\n    image: postgres\n    ...\n```\n\nThe main attraction in the above *docker-compose.yml* contents are,\n\n```\n    command:\n      - /bin/sh\n      - -c\n      - |\n        chmod +x /app/migrate.sh\n        sh /app/migrate.sh\n        sh /app/entrypoint.sh\n\n```\n\nThis was something that I struggled with earlier on how to write multiple bash commands in a single `command` clause in the compose \nfile, then I took reference from [https://www.baeldung.com/ops/docker-compose-multiple-commands](https://www.baeldung.com/ops/docker-compose-multiple-commands), there are few other ways listed on this link which can help with writing multi-line command\nin compose file.\n\n### PostgreSQL Docker Service\n\nSome of the major points to notice in the postgres image of the docker compose services are,\n\n1. environment variables\n2. `healthcheck` command\n\nWe will go through both the points.\n\nFirst of all the environment variables, so PostgreSQL image on [Docker Hub](https://hub.docker.com/_/postgres) allows us to provision a \nwhole database just using some environment variables and few key value pairs in yml file.\n\nEnvironment variables are,\n\n1. `POSTGRES_DB`\n2. `POSTGRES_USER`\n3. `POSTGRES_PASSWORD`\n4. `POSTGRES_HOST`\n5. `POSTGRES_PORT`\n\nWithout these environment variables, *postgres* image on Docker won't create the database and won't even start the PostgreSQL server\nto connect to in our other apps. Note that these variables name are case sensitive and any mistake in spelling can make you wonder what\nwent wrong when things are not working.\n\nSecond is the `healthcheck` command, this is very useful when you have other services depending upon your `postgres` DB service,\nsuppose you have a web application that consumes the Postgres Database, in that case you want your DB to up and running first and then\nall the other services which uses Postgres to get up after. Now you might be wondering then this is what the docker compose \n`depends_on` provides us with but here is a catch, PostgreSQL DB service would be marked as up as soon as the container is spawned but \nstarting the PostgreSQL DB server and healtchecks takes a little time and there might be cases where the other services would start \nearlier and would try connecting with the DB and fail at that time, so to avoid such situation, we need to make sure that we wait until\nall health checks in postgres are done and then service is up.\n\nFor this we will use the `pg_ready` healthcheck command,\n\n```\nservices:\n  postgres_db:\n    image: postgres\n    restart: always\n    ports:\n      - 5432:5432\n    env_file:\n      - web/.env\n    volumes:\n      - postgres_data:/var/lib/postgresql/data\n    healthcheck:\n      test: pg_isready -h postgres_db\n      interval: 1s\n      timeout: 5s\n      retries: 10s\n\nvolumes:\n  postgres_data:\n    driver: local\n```\n\nDocker compose provides us with `healthcheck` which can be used to run commands for this particular service to know the health status\nand until this passes the service won't be marked as up.\n\nTo break down the `healthcheck` key,\n\n1. `test` -\u003e the actual command to run which postgres already provides - `pg_isready` and the `-h postgres_db` is the hostname arg\ntelling to use the config of the same postgres db which is `postgres_db`\n\n2. `interval` -\u003e intervals at which this test command will run\n\n3. `timeout` -\u003e time in seconds after which if a command does not respond then it will be marked as failed\n\n4. `retries` -\u003e number of times to retry this test command for this service's healthcheck\n\nSome other important points are,\n\n1. `ports:` this actually is a mapping of target and published port which tells which port to open in the container and which one to target in the host machine(our local computer or server on which docker container is running)\n\n2. `volumes:` this makes sure that the database data is intact even if the service goes down because the volume is not affected if any\nof the service in the docker goes down which is very obvious, we won't want our data to be effected if the DB is down or crashes.\n\n3. `restart:` this tells when and at what triggers to restart the service, in our case we have defined `always` which basically tells\nwhenever the service goes down or crashes or is shutdown, keep restarting the *postgres_db* service. There are other options as well \nlike `unless-stopped`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexmhack%2Fdjango-k8s","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexmhack%2Fdjango-k8s","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexmhack%2Fdjango-k8s/lists"}