[英]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?如何设置我的
Dockerfile
或docker-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 文件所在的目录中:
sudo docker-compose build app
sudo docker-compose up
-> initial migrations are applied, static files are collected, superuser created sudo docker-compose up
-> 应用初始迁移,收集静态文件,创建超级用户sudo docker-compose down
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.