DEV Community

Muhammad Abdullah
Muhammad Abdullah

Posted on

How to deploy a Django App with Spheron?

Today i am going to guide you through every step to publish your Django app with spheron.
So make your code editors ready and your hands to get dirty with coding!

                          Lets Start!
Enter fullscreen mode Exit fullscreen mode

Step 1: Creating a Django app

Assuming that you have python3 installed and you have an account on Dockerhub

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)
Enter fullscreen mode Exit fullscreen mode

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!")
Enter fullscreen mode Exit fullscreen mode

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),
]

Enter fullscreen mode Exit fullscreen mode

8.Run your Django server by

python manage.py runserver

9.Open your browser and paste this link127.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
Enter fullscreen mode Exit fullscreen mode

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.

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.

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.

9.You can add advanced configuration if required. Click here to know more.
** Click 'Deploy' to initiate deployment.**

Top comments (0)