如何设置运行Django的AWS Elastic Beanstalk Docker平台?

时间:2021-10-14 23:22:10

I have been running a regular Django site using the regular EB Python platform, but for some reasons, I want to migrate to using a Docker image, but still get everything I get from Elastic Beanstalk. In order to demonstrate the problem, I created a small Django project at https://github.com/dkarchmer/aws-eb-docker-django which works on both Python 2.7 and 3.4, and it is a simplified version of my real site. Anyway, I will be happy getting this to work with either the Preconfigured Docker Platform, or with a generic Docker platform. My Google searches have only found the super simple, Flask based example that AWS shows, and a couple of questions on *, but it almost seems like nobody is trying to put a Django project on EB/Docker. Anyway, lets start with a generic solution, which is my preferred option. If you download the Github project, you can successfully use docker (or better, docker-sompose) to test that the image works when run locally (I am running on a MacOS). It is not clear if I should use a CMD to define specify my "python manage.py runserver" or if I need an ENTRYPOINT, or if I need to use uswgi (as the preconfigured solution does). I believe I need a CMD, so that is what I am doing:

我一直使用常规的EB Python平台运行常规Django站点,但出于某些原因,我想迁移到使用Docker镜像,但仍然可以从Elastic Beanstalk获得所有内容。为了演示这个问题,我在https://github.com/dkarchmer/aws-eb-docker-django创建了一个小型Django项目,该项目适用于Python 2.7和3.4,它是我真实站点的简化版本。无论如何,我很高兴能够使用预配置的Docker平台或通用的Docker平台。我的谷歌搜索只发现了AWS显示的超级简单,基于Flask的示例,以及*上的几个问题,但似乎没有人试图将Django项目放在EB / Docker上。无论如何,让我们从通用解决方案开始,这是我的首选方案。如果您下载Github项目,您可以成功使用docker(或更好的,docker-sompose)来测试图像在本地运行时是否正常工作(我在MacOS上运行)。目前尚不清楚我是否应该使用CMD来定义指定我的“python manage.py runserver”,或者我是否需要ENTRYPOINT,或者我是否需要使用uswgi(如预先配置的解决方案那样)。我相信我需要一个CMD,所以我正在做的事情:

  FROM python:3.4

  RUN adduser --disabled-password --gecos '' myuser

  # Install PostgreSQL dependencies
  # Install Postgres
  RUN apt-get update && apt-get install -y \
        postgresql-9.3 \
        libpq-dev \
        libjpeg-dev; \
        apt-get clean

 # Step 1: Install any Python packages
 # ----------------------------------------

 RUN mkdir /var/app
 WORKDIR  /var/app
 COPY requirements.txt /var/app/
 RUN pip install -r requirements.txt

 # Step 2: Copy Django Code
 # ----------------------------------------

 COPY authentication /var/app/authentication
 COPY myproject /var/app/myproject
 COPY settings /var/app/settings
 COPY manage.py /var/app/

 ENV DJANGO_SETTINGS_MODULE=settings.production

 EXPOSE 8080

 WORKDIR  /var/app
 CMD ["python", "/var/app/manage.py", "runserver", "0.0.0.0:8080"]

You can look (download and try) the code at the Github link above.

您可以在上面的Github链接中查找(下载并尝试)代码。

When I deploy (see fab eb_create_custom in fabfile.py), I get

当我部署时(参见fabfile.py中的fab eb_create_custom),我得到了

[2015-05-02T15:22:27.245Z] INFO  [1732]  - [CMD-Startup/StartupStage0/AppDeployPreHook/04run.sh] : Activity execution failed, because: 17212f02bce509d43c40eeac9f53a281eecf1502387dac69c096c337c9c7b186
 Docker container quit unexpectedly after launch: Docker container quit unexpectedly on Sat May  2 15:22:27 UTC 2015:
 python3: can't open file 'manage.py': [Errno 2] No such file or directory. Check snapshot logs for details. (ElasticBeanstalk::ExternalInvocationError) caused by: 17212f02bce509d43c40eeac9f53a281eecf1502387dac69c096c337c9c7b186

I have confirmed the manage.py file is there. I have downloaded the full logs and cannot see much, other than the error above.

我已经确认manage.py文件在那里。我已经下载了完整的日志,除了上面的错误之外,看不到多少。

Any insides? Anybody knows of an example for running Django on EB Docker?

任何内部?谁知道在EB Docker上运行Django的例子?

Thanks

2 个解决方案

#1


I still do not have a working custom Docker image, but got the Preconfigured Python Docker version working:

我仍然没有可用的自定义Docker镜像,但是预配置的Python Docker版本正常工作:

https://github.com/dkarchmer/aws-eb-preconfigured-docker-django

#2


I finally found the problem.

我终于找到了问题。

The problem was that in the Dockerrun.aws.json file, I was mapping (using 'Volumes') a host directory into the /var/app container directory, which happens to be where I was COPY-ing the project to. This was basically overwriting and replacing all project files. This is why the docker image was working on my local machine (as this file is not needed locally) but failing when releasing to AWS.

问题是在Dockerrun.aws.json文件中,我将(使用'Volumes')主机目录映射到/ var / app容器目录,这恰好是我将项目复制到的位置。这基本上是覆盖和替换所有项目文件。这就是Docker镜像在本地计算机上工作的原因(因为本地不需要此文件)但在发布到AWS时失败。

Because I don't need to cross map these volumes, I was able to fix the problem by just removing the 'Volumes' entry in the Dockerrun.aws.json.

因为我不需要交叉映射这些卷,所以我只需删除Dockerrun.aws.json中的“Volumes”条目即可解决问题。

I have cleaned up and released a working version for others to hopefully find useful:

我已经清理并为其他人发布了一个工作版本,希望有用:

https://github.com/dkarchmer/aws-eb-docker-django

#1


I still do not have a working custom Docker image, but got the Preconfigured Python Docker version working:

我仍然没有可用的自定义Docker镜像,但是预配置的Python Docker版本正常工作:

https://github.com/dkarchmer/aws-eb-preconfigured-docker-django

#2


I finally found the problem.

我终于找到了问题。

The problem was that in the Dockerrun.aws.json file, I was mapping (using 'Volumes') a host directory into the /var/app container directory, which happens to be where I was COPY-ing the project to. This was basically overwriting and replacing all project files. This is why the docker image was working on my local machine (as this file is not needed locally) but failing when releasing to AWS.

问题是在Dockerrun.aws.json文件中,我将(使用'Volumes')主机目录映射到/ var / app容器目录,这恰好是我将项目复制到的位置。这基本上是覆盖和替换所有项目文件。这就是Docker镜像在本地计算机上工作的原因(因为本地不需要此文件)但在发布到AWS时失败。

Because I don't need to cross map these volumes, I was able to fix the problem by just removing the 'Volumes' entry in the Dockerrun.aws.json.

因为我不需要交叉映射这些卷,所以我只需删除Dockerrun.aws.json中的“Volumes”条目即可解决问题。

I have cleaned up and released a working version for others to hopefully find useful:

我已经清理并为其他人发布了一个工作版本,希望有用:

https://github.com/dkarchmer/aws-eb-docker-django