gpt4 book ai didi

django - 如何防止我的脚本在每次启动 Docker 容器时运行?

转载 作者:行者123 更新时间:2023-12-02 18:03:44 27 4
gpt4 key购买 nike

我只会在构建 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 模型 - 替换为您在灯具中实际拥有的模型。)

关于django - 如何防止我的脚本在每次启动 Docker 容器时运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60305591/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com