How to deploy a Django App with Spheron?
Tags: sephron, deployment, django, python
Published: 2023-06-30 12:41:57

Today i am going to guide you through every step to publish your Django app with spheron.
So make your [code editors](https://code.visualstudio.com/) ready and your hands to get dirty with coding!

                              Lets Start!


## Step 1: **Creating a Django app**

Assuming that you have [python3](https://www.python.org/downloads/) installed and you have an account on [Dockerhub](https://hub.docker.com/)

**Open your favorite code editor's terminal or CMD**

1. Install Django if you don't have it by running

`pip install Django`

2.Create a new project by running

`django-admin startproject (myproject)`

3.Change the directory to your project

`
cd (myproject)
`
4.Create a new Django app in your projects directory

```
python manage.py startapp (myapp)
```
5.Open the settings file of your Django app **_myproject/settings.py_**. In **INSTALLED_APPS** add _**myapp**_ & **'*'** to **ALLOWED_HOSTS**.

6.Create a view in _myapp/views.py_

```
from django.http import HttpResponse
 
def hello_world(request):
   return HttpResponse("Hello, World!")
```

7.Now open the _myapp/urls.py_ and replace the lines with 

```
from django.urls import path
from myapp.views import hello_world
 
urlpatterns = [
 path('helloworld/', hello_world),
]

```
8.Run your Django server by 

`python manage.py runserver`

9.Open your browser and paste this link[127.0.0.1/8000/helloworld/](127.0.0.1/8000/helloworld/) or just click on the link from your terminal window.

##**Step 2:** **Creating a Docker File**

Here's how it will look like 

```
# Use an official Python runtime as the base image
FROM python:3.9
 
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
 
# Set the working directory in the container
WORKDIR /code
 
# Install dependencies
COPY requirements.txt /code/
RUN pip install --no-cache-dir -r requirements.txt
 
# Copy the Django project code to the container
COPY . /code/
 
# Expose the port that Django runs on
EXPOSE 8000
 
# Run the Django development server
CMD python manage.py runserver 0.0.0.0:8000
```

## Step 3: **Build Docker Image**

To build Docker image:

1. Save the above docker file in the root directory of Django app.

2.Open a terminal and navigate to the root directory of your project where the Dockerfile is located.

3.Run this command to create a docker image

`docker build -t myproject .`

`docker run -p 8000:8000 myproject`

##Step 4: **Pushing the app to Docker Hub**

1. Create a repository on Dockerhub by clicking **Create repository** button.

2.In the name section write the name to your repository.

3.Click **Create** button to create a repository.

4.Login to the Docker Hub using the command docker login -u _YOUR-USER-NAME_

5.Use the docker tag command to give the myproject image a new name. 

Be sure to swap out YOUR-USER-NAME with your Docker ID.
`docker tag myproject YOUR-USER-NAME/myproject`

6.Now again run the push command
`docker push YOUR-USER-NAME/myproject`

##Step 5: **Run on Spheron Compute**

1.Click **New Cluster** on the top right corner.

2.Select **Import** from Docker Hub.

3.Enter the ** names** for your cluster and docker image.

4.Then, Add the **tag **and Click** Next**.

5.Select the **instance plan** that suits your needs and Click **Select Plan**.

6.Create new **Port Mapping**. Add the container port, and Select the exposed port you want to map it to. [Click here to know more](https://docs.spheron.network/compute/cluster/#port-mapping).

7.Add ** Environment Variables** if any. Use the Secret Key toggle if the value is a secret key. When you enable the secret key toggle, it will not be saved in the database. [Click here to know more](https://docs.spheron.network/compute/cluster/#environment-variables).

8.Select your **preferred Region** if any. If you do not add a region, the container will be deployed in any region. [Click here to know more](https://docs.spheron.network/compute/cluster/#region).

9.You can add advanced configuration if required. [Click here to know more](https://docs.spheron.network/compute/cluster/#advanced-configuration).
    ** Click '**Deploy**' to initiate deployment.**