如何阻止每次启动Docker容器时运行我的脚本?

问题描述 投票:0回答:2

我仅在构建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:

我有这个网站/ 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容器时才运行,但它们似乎总是在还原容器时才运行。有什么方法可以调整我必须达到的目标吗?

django docker docker-compose seeding docker-entrypoint
2个回答
0
投票

而不是使用entrypoint.sh文件,为什么不只在Web / Dockerfile中运行命令?

RUN python manage.py migrate maps
RUN python manage.py loaddata maps/fixtures/country_data.yaml
RUN python manage.py loaddata maps/fixtures/seed_data.yaml

这样,这些更改将被烘焙到图像中,并且当您启动图像时,这些更改将已经被执行。


0
投票

我以前使用的一种方法是将您的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模型-替换为您实际在灯具中具有的模型。)

© www.soinside.com 2019 - 2024. All rights reserved.