DEPLOY DJANGO PROJECT ON GCP WITH THE GDAL Dockerfile
DEPLOY DJANGO PROJECT ON GCP WITH THE GDAL Dockerfile
First, you need to follow the instruction
- CREATE PROJECT (OR YOU HAVE PROJECT )
- IT IS RUNNING IN LOCAL SERVER
- CREATE BILLING ACCOUNT ON GCP
- CONNECT DATABASE WITH GCP USING POSTGRESQL
- ADD REQUIRED FILE IN YOUR PROJECT FOR DEPLOY
- DEPLOY THE PROJECT
CREATE PROJECT (OR YOU HAVE PROJECT )
If you have a project which you have created or simply you can also download from Github
after that,
IT IS RUNNING IN LOCAL SERVER
before deploying on google cloud. you can run it into the local server so that no problem is facing on the live server
CREATE BILLING ACCOUNT ON GCP
go on the google console cloud click on this you see this
Click on activate on top of the right after you can add your credit or debit card datils after that you receive $300 for the 12 months so that you can use it.
CONNECT DATABASE WITH GCP USING POSTGRESQL
you can use to create a server of PostgreSQL create PostgreSQL server after that right-click on the server and add the database after that you can go into the setting.py of your project add the following command
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'DATABASE_NAME',
'USER': 'postgres',
'PASSWORD': 'YOUR_PASSWORD',
'HOST': 'YOUR_HOST',
'PORT': '5432',
}
}
after right-click on your PostgreSQL server click on the property to get the host and user
after that, migrate your project
py manage.py migrate
ADD REQUIRED FILE IN YOUR PROJECT FOR DEPLOY
let my project name project1
now add the required fill app.yaml, appengine_config.py, Dockerfile, main.py, noxfile_config.py, requirements.txt the structure of the project1
add in the file
app.yaml
runtime: custom
#python
env: flex
entrypoint: gunicorn -t 300 -b :$PORT project1.wsgi
runtime_config:
python_version: 3.7
# [END runtime]
automatic_scaling:
max_num_instances: 3
appengine_config.py
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START vendor]
import os
from google.appengine.ext import vendor
vendor.add(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib'))
# [END vendor]
Dockerfill
from python:3.8.0-slim-buster
RUN groupadd dev && useradd -m -g dev -s /bin/bash dev
RUN echo "dev ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
RUN mkdir -p /home/dev/app
RUN chown -R dev:dev /home/dev/app
RUN chmod -R +x+r+w /home/dev/app
WORKDIR /home/dev/app/HomePage
RUN apt-get update && apt-get -y upgrade
RUN apt-get install -y python3-dev python3-pip
RUN apt-get install -y libpq-dev
RUN apt-get install -y binutils libproj-dev gdal-bin
RUN pip install --upgrade pip
ADD requirements.txt /app/requirements.txt
RUN pip install -r /app/requirements.txt
# Add the application source code.
ADD . /home/dev/app/HomePage
ENTRYPOINT gunicorn -t 300 -b :$PORT project1.wsgi
main.py
from project1.wsgi import application
# App Engine by default looks for a main.py file at the root of the app
# directory with a WSGI-compatible object called app.
# This file imports the WSGI-compatible object of your Django app,
# application from mysite/wsgi.py and renames it app so it is discoverable by
# App Engine without additional configuration.
# Alternatively, you can add a custom entrypoint field in your app.yaml:
# entrypoint: gunicorn -b :$PORT mysite.wsgi
app = application
noxfile_config.py
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Default TEST_CONFIG_OVERRIDE for python repos.
# You can copy this file into your directory, then it will be inported from
# the noxfile.py.
# The source of truth:
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/noxfile_config.py
TEST_CONFIG_OVERRIDE = {
# You can opt out from the test for specific Python versions.
'ignored_versions': ["2.7"],
# An envvar key for determining the project id to use. Change it
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
# build specific Cloud project. You can also use your own string
# to use your own Cloud project.
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
# A dictionary you want to inject into your test. Don't put any
# secrets here. These values will override predefined values.
'envs': {
'DJANGO_SETTINGS_MODULE': 'project1.settings'
},
}
requirements.txt
Django
gunicorn
wheel
in requirements.txt add that app name which you have download and add in your installed app in your settings.py
NOW DEPLOY THE PROJECT
Before you begin
In the Cloud Console, on the project selector page, select or create a Cloud project.
Make sure that billing is enabled for your Google Cloud project. Learn how to confirm billing is enabled for your project.
- Enable the Cloud SQL Admin API.
- Install and initialize the Cloud SDK
Log in to gcloud
Acquire new credentials to use the Cloud SQL Admin API:
After the installation has completed, accept the following options:
- Start Cloud SDK Shell
- Run gcloud init
The installer starts a terminal window and runs the gcloud init
command.
after that add the location of project like this
cd project_location in your computer
after add run
- gcloud app deploy
Comments
Post a Comment