jennischofield02
jennischofield02

Reputation: 13

Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. - Django

I'm new to asking questions on SO, so bear with me. I know there are a lot of answers about this already, but none of them work. I've been working on making a Django web app, and now I have to deploy it so you can access it on mobile. I've been trying to get it running on Google Cloud Run, but no luck. I've made the Docker container, and that runs fine locally, but when I try to deploy the container, it gives me the following error: Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. Logs for this revision might contain more information.

The logs loop back to the same page, with no additional information. I've tried to switch the ports in the docker-compose.yml and Dockerfile to 80, 8000, 8080, and any variation in between, with no luck. I know with Django stuff you're meant to use the App Engine, but I've already loaded too much stuff into the sqlite3 database, and all the sources I can find for the App engine are about PostGres, which I don't have time to convert over to. I also made sure to include the ALLOWED_HOST 0.0.0.0 in the settings.py, so I don't know what's happening. Here are the contents of my Dockerfile:

ENV PYTHONUNBUFFERED 1
EXPOSE 8080
ENV PORT 8080
ENV HOST 0.0.0.0
RUN mkdir /Exeplore
WORKDIR /Exeplore
COPY requirements.txt /Exeplore/
RUN pip install -r requirements.txt
COPY . /Exeplore/

Here are the contents of my docker-compose.yml file:

  web:
    build: .
    environment:
      MYENV: EXAMPLE
    volumes:
      - .:/code
  web_migrate:
    extends:
      service: web
    command: python manage.py migrate
  web_run:
    extends:
      service: web
    command: python manage.py runserver 0.0.0.0:8080
    ports:
      - "8080:8080"

This is the error I get from google cloud run, and the log just links to the same page:

This is the error I get from google cloud run, and the log just links to the same page

I hope I've done this right, but please let me know if anything else is needed.

Upvotes: 0

Views: 2162

Answers (1)

rosera
rosera

Reputation: 51

Add the Docker compose commands to the Dockerfile

ENV PYTHONUNBUFFERED 1
EXPOSE 8080
ENV PORT 8080
ENV HOST 0.0.0.0
RUN mkdir /Exeplore
WORKDIR /Exeplore
COPY requirements.txt /Exeplore/
RUN pip install -r requirements.txt
COPY . /Explore/
ENTRYPOINT [ "python" "manage.py"]
CMD [ "runserver", "0.0.0.0:8080" ]

You should now be able to call the python application with the necessary parameters e.g. to perform the default command.

docker run -p 8080:8080 my_container

In the above Dockerfile:

  • The Entrypoint will issue the call to the python interpreter and call the manage.py application.
  • The CMD (command) will call the additional parameters used in the compose file (i.e. runserver 0.0.0.0:8080). You can override the default behaviour by adding the necessary parameter i.e. migrate.

e.g.

docker run -p 8080:8080 my_container migrate

Upvotes: 1

Related Questions