我正在学习在具有以下技术堆栈的项目中使用docker
django
redis
mysql-生产
sqlite-开发
蒙哥
我可以配置一个简单的docker,它涵盖与django相关的内容和mysql,但不知道如何粘贴redis和mongod。我看到了Internet上的各种方式,但是基于配置,我已经确定了如何使其适合自己。
这就是我所做的
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
所以我的问题是我如何在这里粘合redis和mongo?
P.S请让我知道我是否没有采取最佳做法。
答案 0 :(得分:0)
最常见的做法是每个容器只有一个进程,即将redis和mongo添加到docker-file
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
redis:
image: "redis:2.8"
mongodb:
image: mongo:latest
关于sqlite,您可以将与计算机/实例共享的卷用于db文件本身。