How to write file docker-compose and dockerfie for Django connect Kong api
I'm doing a graduation project, and I'm about to put my project up to the server, but I'm having trouble with docker-compose and dockerfile files, could you please advise me where I should fix it?
I experimented with writing, but there are still problems in the kong image docker. It crashes, doesn't always work. How should I fix this? Can you suggest writing these files for me?
Thank you.
dockerfile files
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
EXPOSE 8000
CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]```
docker-compose files
```version: '3.9'
services:
kong-database:
image: postgres:latest
container_name: kong-database
restart: always
ports:
- 15432:5432
networks:
- default
volumes:
- db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=kong
- POSTGRES_USER=kong
- POSTGRES_PASSWORD=kong
kong:
image: kong:latest
container_name: kong
restart: always
ports:
- 18000:8000
- 18443:8443
- 127.0.0.1:8001:8001
- 18444:8444
links:
- kong-database:kong-database
networks:
- default
environment:
- LC_CTYPE=en_US.UTF-8
- LC_ALL=en_US.UTF-8
- KONG_DATABASE=postgres
- KONG_PG_HOST=kong-database
- KONG_PG_USER=kong
- KONG_PG_PASSWORD=kong
- KONG_CASSANDRA_CONTACT_POINTS=kong-database
- KONG_PROXY_ACCESS_LOG=/dev/stdout
- KONG_ADMIN_ACCESS_LOG=/dev/stdout
- KONG_PROXY_ERROR_LOG=/dev/stderr
- KONG_ADMIN_ERROR_LOG=/dev/stderr
- KONG_ADMIN_LISTEN=0.0.0.0:18001, 0.0.0.0:18444 ssl
konga:
image: pantsel/konga
container_name: kong-konga
restart: always
ports:
- 1337:1337
networks:
- default
volumes:
- data:/app/kongadata
links:
- kong:kong
environment:
- NODE_ENV=production
networks:
default:
driver: bridge
volumes:
db:
driver: local
data:
driver: local
setting.py
```DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'kong',
'USER': 'kong',
'PASSWORD': 'kong',
'HOST': '127.0.0.1',
'PORT': '15432',
}
}```
Consider running required migration process before launching pythong? E.g.
CMD ["kong", "migrations bootstrap && python3 manage.py runserver 0.0.0.0:8000"]
Database needs bootstrapping or is older than Kong 1.0. To start a new installation from scratch, run 'kong migrations bootstrap'. To migrate from a version older than 1.0, migrated to Kong 1.5.0 first. If you still have 'apis' entities, you can convert them to Routes and Services using the 'kong migrations migrate-apis' command in Kong 1.5.0.
The error comes from Kong not being able to use the database provided. when you bringing up kong there need to be an additional step of migrations/bootstrap
to prepare the database to be used by Kong.
Since you are using Docker Compose to bring the whole environment up, adding an additional container to do the migration first should be sufficient:
kong-migrations:
container_name: kong-migrations
image: kong:latest
environment:
- KONG_DATABASE=postgres
- KONG_PG_HOST=kong-database
- KONG_PG_USER=kong
- KONG_PG_PASSWORD=kong
- KONG_PASSWORD=kong
networks:
- default
command: kong migrations bootstrap
depends_on:
- kong-database
Since you are mounting data volume from/to Kong database, the first run might not work since Kong might start before the migrations finish. If that's the case, simply wait for Kong to restart automatically and it should work.