I am learning to use docker in one of my project which has the following tech stack
django
redis
mysql - production
sqlite - development
mongod
I could configure a simple docker which covers django related and mysql but have no idea how to glue redis and mongod as well. I see various ways in internet but based on the configuration i already made how should i fit them well.
Here is what I have done
docker-compose.yml
version: '3'
services:
db:
image: mysql
web:
build:
context: .
dockerfile: docker/python/Dockerfile
command: bash -c "sleep 3; python /code/manage.py migrate --noinput && python manage.py runserver 0.0.0.0:8000"
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
Dockerfile
# python image (https://hub.docker.com/_/python/)
FROM python:3.6
# Environment Variables
ENV PYTHONBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
COPY ./requirements/requirements.txt /code/requirements.txt
RUN pip install -r /code/requirements.txt
RUN chmod ug+x /code/initialize.sh
COPY . /code/
WORKDIR /code
EXPOSE 8000
initialize.sh
#!/bin/bash
# This script initializes the Django project. It will be executed (from
# supervisord) every time the Docker image is run.
# If we're not in production, create a temporary dev database
if [ "$DJANGO_PRODUCTION" != "true" ]; then
echo "DJANGO_PRODUCTION=false; creating local database..."
# Wait until the MySQL daemon is running
while [ "$(pgrep mysql | wc -l)" -eq 0 ] ; do
echo "MySQL daemon not running; waiting one second..."
sleep 1
done
# Wait until we can successfully connect to the MySQL daemon
until mysql -uroot -pdevrootpass -e ";" ; do
echo "Can't connect to MySQL; waiting one second..."
sleep 1
done
echo "MySQL daemon is running; creating database..."
mysql -uroot -e "CREATE DATABASE appDB; CREATE USER milan@localhost; SET PASSWORD FOR milan@localhost=PASSWORD('milan'); GRANT ALL PRIVILEGES ON appDB.* TO milan@localhost IDENTIFIED BY 'milan'; FLUSH PRIVILEGES;" -pdevrootpass;
else
echo "DJANGO_PRODUCTION=true; no local database created"
fi
# Initialize Django project
python /code/manage.py collectstatic --noinput
python /code/manage.py makemigrations
python /code/manage.py migrate --noinput
# Create a Django superuser named `root` if it doesn't yet exist
echo "Creating Django superuser named 'root'..."
if [ "$DJANGO_PRODUCTION" != "true" ]; then
# We're in the dev environment
if [ "$ROOT_PASSWORD" == "" ]; then
# Root password environment variable is not set; so, load it from config.ini
echo "from ConfigParser import SafeConfigParser; parser = SafeConfigParser(); parser.read('/code/config.ini'); from django.contrib.auth.models import User; print 'Root user already exists' if User.objects.filter(username='root') else User.objects.create_superuser('root', 'admin@example.com', parser.get('general', 'ROOT_PASSWORD'))" | python /code/manage.py shell
else
# Root password environment variable is set; so, use it
echo "import os; from django.contrib.auth.models import User; print 'Root user already exists' if User.objects.filter(username='root') else User.objects.create_superuser('root', 'admin@example.com', os.environ['ROOT_PASSWORD'])" | python /code/manage.py shell
fi
else
# We're in production; use root password environment variable
echo "import os; from django.contrib.auth.models import User; print 'Root user already exists' if User.objects.filter(username='root') else User.objects.create_superuser('root', 'admin@example.com', os.environ['ROOT_PASSWORD'])" | python /code/manage.py shell
fi
so my question is how do i glue redis and mongo here?
P.S please let me know if I am out of best practice way.