DjangoをCloud Runにデプロイ


Dockerfile
FROM python:3.7
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip3 install -r requirements.txt
ADD . /code/
EXPOSE 8000
CMD exec gunicorn --bind 0.0.0.0:$PORT --workers 1 --threads 8 --timeout 0 config.wsgi:application
requirements.txt
asgiref==3.3.1
Django==3.1.7
django-cors-headers==3.7.0
django-mysql==3.11.1
django-rest-framework==0.1.0
django-storages[google]==1.11.1
django-webpack-loader==0.7.0
djangorestframework==3.12.2
gunicorn==20.0.4
mysqlclient==2.0.3
PyMySQL==1.0.2
pytz==2021.1
sqlparse==0.4.1
whitenoise==5.2.0
django-environ==0.4.5
psycopg2-binary==2.8.6
google-cloud-secret-manager==2.3.0
django-registration-redux==2.9
django-widget-tweaks==1.4.8

GCRにイメージを登録し、CloudRunにデプロイするファイル

cloudbuild.yaml
steps:
# Build the container image
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/PROJECT_ID/IMAGE', '.'] #build
# Push the container image to Container Registry
- name: 'gcr.io/cloud-builders/docker'
  args: ['push', 'gcr.io/PROJECT_ID/IMAGE'] #push
# Deploy container image to Cloud Run
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
  entrypoint: gcloud
  args: ['run', 'deploy', 'SERVICE-NAME', '--image', 'gcr.io/PROJECT_ID/IMAGE', '--region', 'REGION', '--platform', 'managed'] #run deploy
- name: "gcr.io/google-appengine/exec-wrapper"
  args:
    [
      "-i",
      "gcr.io/project_id/register-route",
      "-s",
      "project_id:asia-northeast1:register-route",
      "-e",
      "SETTINGS_NAME=django_settings",
      "--",
      "python",
      "manage.py",
      "collectstatic",
      "--verbosity",
      "2",
      "--no-input"
    ] #collectstatic

images:
- gcr.io/PROJECT_ID/IMAGE

terminal
 gcloud builds submit

前提にDockerの知識がないとかなり辛い。
当時何も知らなかったため心が折れそうになった。