I've setup my project Django REST API on GCP VM running Ubuntu 22.0.4 LTS in a virtual environment. I'm currently configuring the following open source Rest API project : https://github.com/OkunaOrg/okuna-api
The Okuna CLI uses docker-compose to spawn and coordinate the following docker services. webserver - A server running the Okuna API db - A server with a MariaDB database redis - A server with a Redis database scheduler - A server responsible for running the scheduled Okuna jobs such as curating Explore/Top posts. worker - A server responsible for processing the Okuna jobs such as publishing a post or curating posts
The Django backend opens up successfully at the GCP external IP address as configured by me in the okuna-cli.py. However I face problems when I run
python3.8 manage.py migrate
The Error trace
django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1:3307' (111)")
The .env file(https://github.com/OkunaOrg/okuna-api/blob/master/templates/.env) has the following pertaining to the above error :
# [NAME] ALLOWED_HOSTS
# [DESCRIPTION] Django variable specifying allowed hosts.
# [REQUIRED][PRODUCTION]
# [MORE] https://docs.djangoproject.com/en/2.1/ref/settings/#allowed-hosts
# ALLOWED_HOSTS=openbook.social
# [GROUP] SQL Database Configuration
# [DESCRIPTION] The SQL database configuration
# [REQUIRED][ALWAYS]
RDS_DB_NAME=okuna
RDS_USERNAME=root
RDS_HOSTNAME=127.0.0.1
RDS_PORT=3307
RDS_HOSTNAME_READER=127.0.0.1
RDS_HOSTNAME_WRITER=127.0.0.1
# [NAME] RDS_PASSWORD
# [DESCRIPTION] The password for the SQL Database.
RDS_PASSWORD={{SQL_PASSWORD}}
# [GROUP] Redis Database configuration Configuration
# [DESCRIPTION] The redis database configuration
# [REQUIRED][ALWAYS]
REDIS_HOST=127.0.0.1
REDIS_PORT=6380
# [NAME] REDIS_PASSSWORD
# [DESCRIPTION] The password for the REDIS Database. If using okuna-cli, obtained from .okuna-cli.json
REDIS_PASSWORD={{REDIS_PASSWORD}}
The .docker-compose.env file(https://github.com/OkunaOrg/okuna-api/blob/master/templates/.docker-compose.env) has the following pertaining to the above error :
# [NAME] ALLOWED_HOSTS
# [DESCRIPTION] Django variable specifying allowed hosts.
# [REQUIRED][PRODUCTION]
# [MORE] https://docs.djangoproject.com/en/2.1/ref/settings/#allowed-hosts
# ALLOWED_HOSTS=openbook.social
# [GROUP] SQL Database Configuration
# [DESCRIPTION] The SQL database configuration
# [REQUIRED][ALWAYS]
RDS_DB_NAME=okuna
RDS_USERNAME=root
RDS_HOSTNAME=db.okuna
RDS_PORT=3306
RDS_HOSTNAME_READER=db.okuna
RDS_HOSTNAME_WRITER=db.okuna
# [NAME] RDS_PASSWORD
# [DESCRIPTION] The password for the SQL Database. If using okuna-cli, obtained from .okuna-cli.json
RDS_PASSWORD={{SQL_PASSWORD}}
# [GROUP] Redis Database configuration Configuration
# [DESCRIPTION] The redis database configuration
# [REQUIRED][ALWAYS]
REDIS_HOST=redis.okuna
REDIS_PORT=6379
# [NAME] REDIS_PASSSWORD
# [DESCRIPTION] The password for the REDIS Database.
REDIS_PASSWORD={{REDIS_PASSWORD}}
The settings.py file(https://github.com/OkunaOrg/okuna-api/blob/master/openbook/settings.py) has the following pertaining to the above error :
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS')
if IS_PRODUCTION:
if not ALLOWED_HOSTS:
raise NameError('ALLOWED_HOSTS environment variable is required when running on a production environment')
ALLOWED_HOSTS = [allowed_host.strip() for allowed_host in ALLOWED_HOSTS.split(',')]
else:
if ALLOWED_HOSTS:
logger.info('ALLOWED_HOSTS environment variable ignored.')
ALLOWED_HOSTS = ['*']
REDIS_HOST = os.environ.get('REDIS_HOST', 'localhost')
REDIS_PORT = int(os.environ.get('REDIS_PORT', '6379'))
REDIS_PASSWORD = os.environ.get('REDIS_PASSWORD')
Here is the docker-compose-full.yaml(https://github.com/OkunaOrg/okuna-api/blob/master/docker-compose-full.yml)
version: '3'
services:
webserver:
container_name: okuna-api
build:
dockerfile: Dockerfile
context: ./.docker/api
privileged: true
extra_hosts:
- db.okuna:172.16.16.4
- redis.okuna:172.16.16.5
volumes:
- ./:/opt/okuna-api
- ./.docker-cache/pip:/root/.cache/pip
ports:
- 80:80
working_dir: /opt/okuna-api
networks:
okuna:
ipv4_address: 172.16.16.1
depends_on:
- db
- redis
env_file:
- .docker-compose.env
worker:
container_name: okuna-worker
build:
dockerfile: Dockerfile
context: ./.docker/worker
privileged: true
extra_hosts:
- db.okuna:172.16.16.4
- redis.okuna:172.16.16.5
volumes:
- ./:/opt/okuna-api
- ./.docker-cache/pip:/root/.cache/pip
working_dir: /opt/okuna-api
networks:
okuna:
ipv4_address: 172.16.16.2
depends_on:
- webserver
env_file:
- .docker-compose.env
scheduler:
container_name: okuna-scheduler
build:
dockerfile: Dockerfile
context: ./.docker/scheduler
privileged: true
extra_hosts:
- db.okuna:172.16.16.4
- redis.okuna:172.16.16.5
volumes:
- ./:/opt/okuna-api
- ./.docker-cache/pip:/root/.cache/pip
working_dir: /opt/okuna-api
networks:
okuna:
ipv4_address: 172.16.16.3
depends_on:
- webserver
env_file:
- .docker-compose.env
db:
image: mariadb:10.4.5
hostname: db.okuna
volumes:
- mariadb:/var/lib/mysql
ports:
- 3306
privileged: false
networks:
okuna:
ipv4_address: 172.16.16.4
command: --character-set-server=utf8 --collation-server=utf8_unicode_ci
env_file:
- .docker-compose.env
redis:
image: bitnami/redis:latest
privileged: false
ports:
- 6379
networks:
okuna:
ipv4_address: 172.16.16.5
env_file:
- .docker-compose.env
volumes:
- redisdb:/bitnami/redis/data
volumes:
mariadb:
redisdb:
networks:
okuna:
ipam:
driver: default
config:
- subnet: "172.16.16.0/16"
Need help here. Thanks.
CodePudding user response:
Use docker ps -a
to identify the port that you are binding to 3306 if you're trying to acces it from your localhost, if you're trying to acces it from another container inside your network (okuna) consider using the service name, if you refer to it as 127.0.0.1 from inside your container it will try to connect to the api container itself
CodePudding user response:
The solution is as follows : The ip4 address of the databases mentioned in the containers to be created in the yaml file needs to be added to the .env file inplace of 127.0.0.1