[Django]-AWS copilot with Django never finishes deploying

7👍

tl;dr the Dockerfile that’s being used by this tutorial is incomplete for Copilot’s purposes. It needs an extra line containing

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

and the EXPOSE directive should be updated to 8000. Because Copilot doesn’t recognize Docker Compose syntax and there’s no command or entrypoint specified in the Dockerfile, the image will never start with Copilot’s configuration settings.

Details

AWS Copilot is designed around "services" consisting of an image, possible sidecars, and additional storage resources. That means that its basic unit of config is the Docker image and the service manifest. It doesn’t natively read Docker Compose syntax, so all the config that Copilot knows about is that which is specified in the Dockerfile or image and each service’s manifest.yml and addons directory.

In this example, designed for use with Docker Compose, the Dockerfile doesn’t have any kind of CMD or ENTRYPOINT directive, so the built image which gets pushed to Amazon ECR by Copilot won’t ever start. The tutorial specifies the image’s command (python manage.py runserver 0.0.0.0:8000) as an override in docker-compose.yml, so you’ll want to update your Dockerfile to the following:

FROM python:3.7.4
ENV PYTHONUNBUFFERED=1
RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
EXPOSE 8000
COPY . /code/
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

Note here that I’ve changed the EXPOSE directive to 8000 to match the command from docker-compose.yml and added the command specified in the web section to the Dockerfile as a CMD directive.

You’ll also want to run

copilot init --image postgres --name db --port 5432 --type "Backend Service" --deploy

This will create the db service specified in your docker-compose.yml. You may need to run this first so that your web container doesn’t fail to start while searching for credentials.

Some other notes:

  • You can specify your database credentials by adding variables and secrets in the manifest file for db which is created in your workspace at ./copilot/db/manifest.yml. For more on how to add a secret to SSM and make it accessible to your Copilot services, check out our documentation
variables: 
  POSTGRES_DB: postgres
  POSTGRES_USER: postgres

secrets:
  POSTGRES_PASSWORD: POSTGRES_PASSWORD
  • Your database endpoint is accessible over service discovery at db.$COPILOT_SERVICE_DISCOVERY_ENDPOINT–you may need to update your service code which connects to the database to reflect this endpoint instead of localhost or 0.0.0.0.

Leave a comment