django - 如何防止每次启动 Docker 容器时运行我的脚本?
问题描述
只有在构建我的 docker 容器时,我才会运行一个脚本(填充我的 MySql Docker 容器)。我正在运行以下 docker-compose.yml 文件,其中包含一个 Django 容器。
version: '3'
services:
mysql:
restart: always
image: mysql:5.7
environment:
MYSQL_DATABASE: 'maps_data'
# So you don't have to use root, but you can if you like
MYSQL_USER: 'chicommons'
# You can use whatever password you like
MYSQL_PASSWORD: 'password'
# Password for root access
MYSQL_ROOT_PASSWORD: 'password'
ports:
- "3406:3406"
volumes:
- my-db:/var/lib/mysql
web:
restart: always
build: ./web
ports: # to access the container from outside
- "8000:8000"
env_file: .env
environment:
DEBUG: 'true'
command: /usr/local/bin/gunicorn maps.wsgi:application -w 2 -b :8000
depends_on:
- mysql
apache:
restart: always
build: ./apache/
ports:
- "80:80"
#volumes:
# - web-static:/www/static
links:
- web:web
volumes:
my-db:
我有这个 web/Dockerfile
FROM python:3.7-slim
RUN apt-get update && apt-get install
RUN apt-get install -y libmariadb-dev-compat libmariadb-dev
RUN apt-get update \
&& apt-get install -y --no-install-recommends gcc \
&& rm -rf /var/lib/apt/lists/*
RUN python -m pip install --upgrade pip
RUN mkdir -p /app/
WORKDIR /app/
COPY requirements.txt requirements.txt
RUN python -m pip install -r requirements.txt
COPY entrypoint.sh /app/
COPY . /app/
RUN ["chmod", "+x", "/app/entrypoint.sh"]
ENTRYPOINT ["/app/entrypoint.sh"]
这些是我的 entrypoint.sh 文件的内容
#!/bin/bash
set -e
python manage.py migrate maps
python manage.py loaddata maps/fixtures/country_data.yaml
python manage.py loaddata maps/fixtures/seed_data.yaml
exec "$@"
问题是,当我反复运行“docker-compose up”时,entrypoint.sh 脚本正在使用它的命令运行。我希望这些命令仅在首次构建 docker 容器时运行,但它们似乎总是在容器恢复时运行。有什么方法可以调整我必须实现的目标吗?
解决方案
我之前使用的一种方法是将loaddata
调用包装在自己的管理命令中,该命令首先检查数据库中是否有任何数据,如果有,则不执行任何操作。像这样的东西:
# your_app/management/commands/maybe_init_data.py
from django.core.management import call_command
from django.core.management.base import BaseCommand
from address.models import Country
class Command(BaseCommand):
def handle(self, *args, **options):
if not Country.objects.exists():
self.stdout.write('Seeding initial data')
call_command('loaddata', 'maps/fixtures/country_data.yaml')
call_command('loaddata', 'maps/fixtures/seed_data.yaml')
然后将您的入口点脚本更改为:
python manage.py migrate
python manage.py maybe_init_data
(这里假设您有一个Country
模型 - 替换为您在固定装置中实际拥有的模型。)
推荐阅读
- javascript - 如何隔离 javascript 并将其绑定到特定表单
- c++ - libxml2 的 cmake find_package 不返回有效的头目录
- r - 调度 R 脚本以在 Mac 上使用 crontab 运行
- javascript - 使用pm2启动一个node app,那个node app fork一个子进程,当子进程down掉时,pm2会重启node app吗?
- cplex - CPLEX 无法解决 Gurobi 可以解决的 .lp 文件
- typescript - 如何比较 getMonth() 和完整的日期格式?
- python - Python For循环在中间停止工作
- python - 在 Github for windows 中打开 Tensorboard 的问题
- c# - 我需要帮助尝试在 Unity C# 中的 IF 语句中反转逻辑
- c++ - 在固定的、无序的、拥有的数组中安全、惯用的销毁和压缩