简体   繁体   English

在部署在 Elastic Beanstalk 上的 Dockerfile 上运行迁移

[英]Run migrations on Dockerfile deploying on Elastic Beanstalk

I have a problems making migrations (Django App 4.0.6) on Elastic beanstalk.我在 Elastic beanstalk 上进行迁移(Django App 4.0.6)时遇到问题。

This is my Dockerfile:这是我的 Dockerfile:

FROM python:3.8

ENV PROJECT_DIR=/usr/src/app/
 
ENV PYTHONIOENCODING=utf-8
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV PIPENV_USE_SYSTEM=1

WORKDIR ${PROJECT_DIR}

COPY .  ./

RUN pip install -r requirements.txt

EXPOSE 8000
    
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

Until here, all works well, but if I try to add RUN python manage.py migrate before to EXPOSE 8000 and make the deploy, I have an 504 error.到这里为止,一切正常,但是如果我尝试在EXPOSE 8000之前添加RUN python manage.py migrate并进行部署,则会出现 504 错误。

I tried to add.ebextensions and config file like this:我尝试像这样添加.ebextensions 和配置文件:

container_commands:
  01_migrate:
    command: "source /var/app/venv/*/bin/activate && python3 manage.py migrate"
    leader_only: true

But I don't sure how to activate my env in a Docker, I have an error when I try to make the deploy但是我不确定如何在 Docker 中激活我的环境,当我尝试进行部署时出现错误

2022-08-01 03:10:23,328 P28507 [INFO] Command 01_migrate
2022-08-01 03:10:23,331 P28507 [INFO] -----------------------Command Output-----------------------
2022-08-01 03:10:23,331 P28507 [INFO]   /bin/sh: /var/app/venv/*/bin/activate: No such file or directory
2022-08-01 03:10:23,331 P28507 [INFO] ------------------------------------------------------------
2022-08-01 03:10:23,331 P28507 [ERROR] Exited with error code 1

¿What is the best solution for my case? ¿ 对于我的情况,最好的解决方案是什么?

Thanks for your help: :)谢谢你的帮助: :)

If you are not using docker compose to deploy, then you can add an entrypoint file and run your migrations and the server command there.如果您没有使用 docker compose 进行部署,那么您可以添加一个入口点文件并在那里运行您的迁移和服务器命令。

# Dockerfile

...

RUN pip install -r requirements.txt

EXPOSE 8000
    
ENTRYPOINT ["bin/production-entrypoint.sh"]

where your bin/production-entrypoint.sh file looks something like:您的bin/production-entrypoint.sh文件如下所示:

#!/bin/bash

source /var/app/venv/*/bin/activate
python3 manage.py migrate
python manage.py runserver 0.0.0.0:8000

Or, if you are using docker compose to deploy, then you can run the commands in the docker-compose.yml file:或者,如果您使用 docker compose 进行部署,则可以运行docker-compose.yml文件中的命令:

# Dockerfile

...

RUN pip install -r requirements.txt

EXPOSE 8000
# docker-compose.yml

version: "3"

services:
  web:
    build: .
    ports:
      - "8000:8000"
    env_file:
      - .env
    volumes:
      - /var/app/current:/usr/src/app
    command: >
      bash -c "source /var/app/venv/*/bin/activate
      && python3 manage.py migrate
      && python manage.py runserver 0.0.0.0:8000

...

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

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