<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Muhammad Abdullah</title>
    <description>The latest articles on DEV Community by Muhammad Abdullah (@muhammadabdullah).</description>
    <link>https://dev.to/muhammadabdullah</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1110967%2Fa3f5f663-e3f1-4bdd-80bc-c367da877a6d.jpg</url>
      <title>DEV Community: Muhammad Abdullah</title>
      <link>https://dev.to/muhammadabdullah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/muhammadabdullah"/>
    <language>en</language>
    <item>
      <title>How to deploy a Django App with Spheron?</title>
      <dc:creator>Muhammad Abdullah</dc:creator>
      <pubDate>Fri, 30 Jun 2023 12:41:57 +0000</pubDate>
      <link>https://dev.to/muhammadabdullah/how-to-deploy-a-django-app-with-spheron-5bki</link>
      <guid>https://dev.to/muhammadabdullah/how-to-deploy-a-django-app-with-spheron-5bki</guid>
      <description>&lt;p&gt;Today i am going to guide you through every step to publish your Django app with spheron.&lt;br&gt;
So make your &lt;a href="https://code.visualstudio.com/"&gt;code editors&lt;/a&gt; ready and your hands to get dirty with coding!&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                          Lets Start!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Step 1: &lt;strong&gt;Creating a Django app&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Assuming that you have &lt;a href="https://www.python.org/downloads/"&gt;python3&lt;/a&gt; installed and you have an account on &lt;a href="https://hub.docker.com/"&gt;Dockerhub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Open your favorite code editor's terminal or CMD&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install Django if you don't have it by running&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;pip install Django&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;2.Create a new project by running&lt;/p&gt;

&lt;p&gt;&lt;code&gt;django-admin startproject (myproject)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;3.Change the directory to your project&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
cd (myproject)&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
4.Create a new Django app in your projects directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python manage.py startapp (myapp)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5.Open the settings file of your Django app &lt;strong&gt;&lt;em&gt;myproject/settings.py&lt;/em&gt;&lt;/strong&gt;. In &lt;strong&gt;INSTALLED_APPS&lt;/strong&gt; add &lt;em&gt;&lt;strong&gt;myapp&lt;/strong&gt;&lt;/em&gt; &amp;amp; &lt;strong&gt;'*'&lt;/strong&gt; to &lt;strong&gt;ALLOWED_HOSTS&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;6.Create a view in &lt;em&gt;myapp/views.py&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.http import HttpResponse

def hello_world(request):
   return HttpResponse("Hello, World!")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;7.Now open the &lt;em&gt;myapp/urls.py&lt;/em&gt; and replace the lines with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.urls import path
from myapp.views import hello_world

urlpatterns = [
 path('helloworld/', hello_world),
]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;8.Run your Django server by &lt;/p&gt;

&lt;p&gt;&lt;code&gt;python manage.py runserver&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;9.Open your browser and paste this link&lt;a href="//127.0.0.1/8000/helloworld/"&gt;127.0.0.1/8000/helloworld/&lt;/a&gt; or just click on the link from your terminal window.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Step 2:&lt;/strong&gt; &lt;strong&gt;Creating a Docker File&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Here's how it will look like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# 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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: &lt;strong&gt;Build Docker Image&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To build Docker image:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Save the above docker file in the root directory of Django app.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;2.Open a terminal and navigate to the root directory of your project where the Dockerfile is located.&lt;/p&gt;

&lt;p&gt;3.Run this command to create a docker image&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker build -t myproject .&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker run -p 8000:8000 myproject&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: &lt;strong&gt;Pushing the app to Docker Hub&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create a repository on Dockerhub by clicking &lt;strong&gt;Create repository&lt;/strong&gt; button.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;2.In the name section write the name to your repository.&lt;/p&gt;

&lt;p&gt;3.Click &lt;strong&gt;Create&lt;/strong&gt; button to create a repository.&lt;/p&gt;

&lt;p&gt;4.Login to the Docker Hub using the command docker login -u &lt;em&gt;YOUR-USER-NAME&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;5.Use the docker tag command to give the myproject image a new name. &lt;/p&gt;

&lt;p&gt;Be sure to swap out YOUR-USER-NAME with your Docker ID.&lt;br&gt;
&lt;code&gt;docker tag myproject YOUR-USER-NAME/myproject&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;6.Now again run the push command&lt;br&gt;
&lt;code&gt;docker push YOUR-USER-NAME/myproject&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: &lt;strong&gt;Run on Spheron Compute&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;1.Click &lt;strong&gt;New Cluster&lt;/strong&gt; on the top right corner.&lt;/p&gt;

&lt;p&gt;2.Select &lt;strong&gt;Import&lt;/strong&gt; from Docker Hub.&lt;/p&gt;

&lt;p&gt;3.Enter the ** names** for your cluster and docker image.&lt;/p&gt;

&lt;p&gt;4.Then, Add the &lt;strong&gt;tag **and Click&lt;/strong&gt; Next**.&lt;/p&gt;

&lt;p&gt;5.Select the &lt;strong&gt;instance plan&lt;/strong&gt; that suits your needs and Click &lt;strong&gt;Select Plan&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;6.Create new &lt;strong&gt;Port Mapping&lt;/strong&gt;. Add the container port, and Select the exposed port you want to map it to. &lt;a href="https://docs.spheron.network/compute/cluster/#port-mapping"&gt;Click here to know more&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;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. &lt;a href="https://docs.spheron.network/compute/cluster/#environment-variables"&gt;Click here to know more&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;8.Select your &lt;strong&gt;preferred Region&lt;/strong&gt; if any. If you do not add a region, the container will be deployed in any region. &lt;a href="https://docs.spheron.network/compute/cluster/#region"&gt;Click here to know more&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;9.You can add advanced configuration if required. &lt;a href="https://docs.spheron.network/compute/cluster/#advanced-configuration"&gt;Click here to know more&lt;/a&gt;.&lt;br&gt;
    ** Click '&lt;strong&gt;Deploy&lt;/strong&gt;' to initiate deployment.**&lt;/p&gt;

</description>
      <category>sephron</category>
      <category>deployment</category>
      <category>django</category>
      <category>python</category>
    </item>
  </channel>
</rss>
