[英]Init django app with docker-compose
我是docker-compose的新手,我有一个带有我的django实例和一个mysql数据库的docker。 我想创建一个自动配置的容器,它只在第一个docker run上运行命令。 在此命令中,我想执行以下任务:
在此之后,系统应继续启动django测试网络服务器。
是否有任何方法可以告诉docker-compose在首次运行时运行命令,还是在django中有任何替代方法来控制系统是否已经配置和更新?
为了澄清这里我的dockfile和docker-compose.yml:
FROM python:3.4
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
####################
version: '2'
services:
db:
image: "mysql:5.6"
ports:
- 3306:3306
environment:
MYSQL_ROOT_PASSWORD: xxxxxx
MYSQL_DATABASE: xxxxxx
MYSQL_USER: xxxxx
MYSQL_PASSWORD: xxxxxxx
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
谢谢。
根据@ cricket_007的评论,最后我找到了解决问题的棘手解决方案。 我已经为数据库服务和我的Web服务创建了一个sh脚本。 另外,我在我的文件夹web_local.version和web_server.version中创建了两个版本文件。
web_local.version已添加到我的.gitignore中,因为此文件用于存储当前的应用程序版本。
start_web.sh脚本是一个简单的脚本,用于比较文件夹是否包含web_local.version文件。 在这种情况下,项目已经过去配置,脚本会检查当前应用程序版本是否与服务器版本相比更新。 在所有情况都是最新的情况下,只需运行一个Web服务器,否则运行迁移以更新模型,然后运行Web服务器。
以下是引用的web_start.sh脚本:
#!/bin/bash
FILE="web_local.version"
if [ -f "$FILE" ];
then
echo "File $FILE exist."
if diff ./web_server.version ./web_local.version > /dev/null;
then
echo "model version up to date :)"
else
echo "model updated!!"
python manage.py migrate
cp ./web_server.version ./$FILE
fi
else
echo "File $FILE does not exist"
sleep 10 #added because the first time db take a long time to init and the script doesn't wait until db is finished
cp ./web_server.version ./$FILE
python manage.py migrate
fi
python manage.py runserver 0.0.0.0:8000
我想有更多正式的解决方案,但这个解决方案对我的情况有用,因为它允许我们的团队维护相同的模拟数据库和通过git同步的相同模型,我们只用一个命令运行零时间配置环境。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.