简体   繁体   English

在 docker-compose 服务中运行初始命令

[英]run initial commands in a docker-compose service

I followed this tutorial to run my django web-app locally, apart for the web-app the only service is a postgres db.我按照教程在本地运行我的 django web-app,除了 web-app 唯一的服务是 postgres db。

I wrote a simple script entrypoint.sh to automate the initial operations needed by a django app, like migrate, makemigrations, collectstatic, createsuperuser ;我编写了一个简单的脚本entrypoint.sh来自动化 django 应用程序所需的初始操作,例如migrate, makemigrations, collectstatic, createsuperuser

Everything works fine, except that entrypoint.sh runs everytime I use docker-compose up , performing initial operations that should only run once.一切正常,除了entrypoint.sh每次我使用docker-compose up时都会运行,执行应该只运行一次的初始操作。

How can I set up my Dockerfile or docker-compose.yml so that entrypoint.sh is run just the first time and not everytime I docker-compose down and then docker-compose up again?如何设置我的Dockerfiledocker-compose.yml以便第一次运行entrypoint.sh而不是每次我docker-compose down然后docker-compose up再次运行?

Dockerfile Dockerfile

# importing base image
FROM python:3.9

# updating docker host or host machine
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
    && rm -rf /var/lib/apt/lists/*

# changing current working directory to /usr/src/app
WORKDIR /usr/src/app

# copying requirement.txt file to present working directory
COPY requirements.txt ./

# installing dependency in container
RUN pip install -r requirements.txt

# copying all the files to present working directory
COPY . .

# informing Docker that the container listens on the
# specified network ports at runtime i.e 8000.
EXPOSE 8000
ENTRYPOINT ["./entrypoint.sh"]

docker-compose.yml码头工人-compose.yml

version: '3.7'

services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
  app:
    build: ./
    command: gunicorn sial.wsgi:application --workers=2 --bind 0.0.0.0:8000
    volumes:
      - ./data/:/usr/src/app/data/
      - ./media/:/usr/src/app/media/
    ports:
      - 8000:8000
      - 5432:5432
    environment:
      - POSTGRES_NAME=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - DJANGO_SUPERUSER_EMAIL=admin@email.it
      - DJANGO_SUPERUSER_USERNAME=admin@email.it
      - DJANGO_SUPERUSER_PASSWORD=passadmin
    depends_on:
      - db

entrypoint.sh入口点.sh

#!/bin/bash

python3 manage.py migrate;

python3 manage.py makemigrations;

python3 manage.py migrate;

python3 manage.py collectstatic --clear;

python3 manage.py createsuperuser --no-input;

gunicorn sial.wsgi:application --workers=2 --bind 0.0.0.0:8000;

RECAP回顾

In the directory where my Dockerfile and docker-compose.yml file are:在我的 Dockerfile 和 docker-compose.yml 文件所在的目录中:

  1. sudo docker-compose build app
  2. sudo docker-compose up -> initial migrations are applied, static files are collected, superuser created sudo docker-compose up -> 应用初始迁移,收集静态文件,创建超级用户
  3. sudo docker-compose down
  4. sudo docker-compose up -> initial migrations are applied, static files are collected, superuser created AGAIN. sudo docker-compose up -> 应用初始迁移,收集静态文件,再次创建超级用户。 I'm trying to avoid this.我试图避免这种情况。

I'm new to docker-compose and any help is really appreciated thanks.我是 docker-compose 的新手,非常感谢任何帮助。

A dirty but simple way would be to ignore the error of the createsuperuser command by changing it to python3 manage.py createsuperuser --no-input || true;一个肮脏但简单的方法是忽略createsuperuser命令的错误,将其更改为python3 manage.py createsuperuser --no-input || true; python3 manage.py createsuperuser --no-input || true; . .

This might even be the solution you prefer, because if the variables for docker-compose change, a new superuser would be created with the changed values.这甚至可能是您更喜欢的解决方案,因为如果 docker-compose 的变量发生变化,将使用更改后的值创建一个新的超级用户。

#!/bin/bash

python3 manage.py migrate;

python3 manage.py makemigrations;

python3 manage.py migrate;

python3 manage.py collectstatic --clear;

python3 manage.py createsuperuser --no-input || true;

gunicorn sial.wsgi:application --workers=2 --bind 0.0.0.0:8000;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM