<?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: Anuj Sharma</title>
    <description>The latest articles on DEV Community by Anuj Sharma (@anujdev).</description>
    <link>https://dev.to/anujdev</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%2F163409%2F663f604d-74fc-4180-90e8-4ed5183340ba.jpeg</url>
      <title>DEV Community: Anuj Sharma</title>
      <link>https://dev.to/anujdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anujdev"/>
    <language>en</language>
    <item>
      <title>Django development using Docker as host - Part 5: Django development</title>
      <dc:creator>Anuj Sharma</dc:creator>
      <pubDate>Sun, 17 Jan 2021 07:27:53 +0000</pubDate>
      <link>https://dev.to/anujdev/django-development-using-docker-as-host-part-5-django-development-27bj</link>
      <guid>https://dev.to/anujdev/django-development-using-docker-as-host-part-5-django-development-27bj</guid>
      <description>&lt;p&gt;Till now we covered application setup and docker services running via &lt;code&gt;docker-compose&lt;/code&gt;. In this step, we will use the docker container to develop the Django application.&lt;/p&gt;

&lt;h2&gt;
  
  
  TOC
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Start &lt;code&gt;docker-compose&lt;/code&gt; services&lt;/li&gt;
&lt;li&gt;Django settings&lt;/li&gt;
&lt;li&gt;Run Django application&lt;/li&gt;
&lt;li&gt;Live reload Django changes&lt;/li&gt;
&lt;li&gt;Sync python dependencies&lt;/li&gt;
&lt;li&gt;Create Django app&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Let's dive-in
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Start &lt;code&gt;docker-compose&lt;/code&gt; services &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Before we start with the Django development, we need to run the docker containers with the required services.&lt;br&gt;
Execute the following command to run the services defined in the &lt;code&gt;docker-compose.yml&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose up --build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;After the service has been started, execute the below command in a new terminal to see the running containers&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker ps
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;You will see the list of running containers.&lt;/p&gt;
&lt;h3&gt;
  
  
  2. Django settings &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;When the docker-compose runs for the first time, you will see a Django exception&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This is because we have not yet configured the &lt;code&gt;STATIC_ROOT&lt;/code&gt; settings where the static files are copied on running &lt;code&gt;collectstatic&lt;/code&gt; management command.&lt;br&gt;
Add the following settings to the &lt;code&gt;src/my_app/settings.py&lt;/code&gt; file.&lt;/p&gt;
&lt;h4&gt;
  
  
  Static file settings
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;STATICFILES_DIRS = [
    os.path.join(os.path.dirname(BASE_DIR), 'static_my_project')
]

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn', 'static_root')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn', 'media_root')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;STATICFILES_DIRS&lt;/code&gt; setting defines where the static files will be kept. While development, we will keep the static files inside the &lt;code&gt;static_my_project&lt;/code&gt; directory.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;STATIC_ROOT&lt;/code&gt; is the path where the static files are copied on &lt;code&gt;collectstatic&lt;/code&gt; management command&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Database settings
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'HOST': 'database',
        'PORT': '3306',
        'NAME': 'app_db',
        'USER': 'app_db',
        'PASSWORD': 'app_db'
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Where &lt;code&gt;database&lt;/code&gt; is the service inside the &lt;code&gt;docker-compose&lt;/code&gt;.&lt;/p&gt;
&lt;h4&gt;
  
  
  Template directory
&lt;/h4&gt;

&lt;p&gt;In order to allow the creation of the template files inside the application directory, add the following to the &lt;strong&gt;&lt;code&gt;TEMPLATES.DIRS&lt;/code&gt;&lt;/strong&gt; settings&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
   os.path.join(BASE_DIR, 'templates')
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This allows us to add the template files inside the &lt;code&gt;src/templates&lt;/code&gt; directory and also in the app's &lt;code&gt;templates/&lt;/code&gt; directory.&lt;/p&gt;
&lt;h3&gt;
  
  
  3. Run Django application &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;After adding the settings, run the following to start the docker services&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose up --build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;After the services have been started, let's verify the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Django database has been migrated to the MySQL database.
Visit &lt;code&gt;http://localhost:8080&lt;/code&gt; and use the database credentials to log in to the database management.&lt;/li&gt;
&lt;li&gt;Open &lt;code&gt;http://localhost:8000&lt;/code&gt; to see the Django application running.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  4. Live reload Django changes &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;As we have the &lt;code&gt;.src/&lt;/code&gt; directory mapped to the &lt;code&gt;/app&lt;/code&gt; directory, the host &lt;code&gt;.src/&lt;/code&gt; directory will work as the container's &lt;code&gt;/app&lt;/code&gt; directory. Any changes in the &lt;code&gt;.src/&lt;/code&gt; directory files will reload the Django application.&lt;br&gt;
Check the &lt;code&gt;docker-compose&lt;/code&gt; logs for the logs.&lt;/p&gt;
&lt;h3&gt;
  
  
  4. Sync python dependencies &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;We do not have python installed in our system instead we are using python using &lt;code&gt;Dockerfile&lt;/code&gt; container. So the python dependencies should be installed in the container.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open a bash shell in the running container
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker exec -it app_image bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Install the dependency using &lt;code&gt;pip&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install requests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This dependency can be used until the container is running. Once stopped, re-running the container, it will be removed.&lt;br&gt;
It is required to sync the &lt;code&gt;requirements.txt&lt;/code&gt; file in the host with the latest dependencies in the container to keep both in sync.&lt;/p&gt;

&lt;p&gt;This can be achieved in two ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add the &lt;code&gt;requests&lt;/code&gt; dependency with the version number in the host's &lt;code&gt;requirements.txt&lt;/code&gt; file
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;requests==&amp;lt;version&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Freeze the pip dependencies from container to the host:
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip freeze &amp;gt; /requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Note, since the &lt;code&gt;requirements.txt&lt;/code&gt; in the host is mapped to the same file in the root of the container, the path should start with &lt;code&gt;/&lt;/code&gt;. Without &lt;code&gt;/&lt;/code&gt; it will create a &lt;code&gt;requirements.txt&lt;/code&gt; file inside the &lt;code&gt;src/&lt;/code&gt; directory.&lt;/p&gt;



&lt;p&gt;You can create the Django applications by running the &lt;code&gt;manage.py&lt;/code&gt; management commands inside the container and edit the source code in the &lt;code&gt;./src&lt;/code&gt; directory.&lt;/p&gt;
&lt;h3&gt;
  
  
  6. Create Django app &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;
&lt;h4&gt;
  
  
  Let's create a Django app
&lt;/h4&gt;

&lt;p&gt;Run the following in the container bash&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 home
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;It will create a &lt;code&gt;home&lt;/code&gt; directory in the &lt;code&gt;./src&lt;/code&gt; directory.&lt;/p&gt;
&lt;h4&gt;
  
  
  Add view
&lt;/h4&gt;

&lt;p&gt;Add the following content to the &lt;code&gt;./src/home/views.py&lt;/code&gt; file&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.views.generic import TemplateView


class IndexView(TemplateView):
    template_name = "home/index.html"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Add template
&lt;/h4&gt;

&lt;p&gt;Create a template file &lt;code&gt;index.html&lt;/code&gt; inside the &lt;code&gt;templates/home&lt;/code&gt; directory with the following content&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;Index page&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
    &amp;lt;h2&amp;gt;Welcome to the Index page&amp;lt;/h2&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Add routing
&lt;/h4&gt;

&lt;p&gt;Add the following to the &lt;code&gt;home/urls.py&lt;/code&gt; file&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 home.views import IndexView

app_name = 'home'
urlpatterns = [
    path('', IndexView.as_view()),
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Include the URLs to the application &lt;code&gt;urls.py&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;path('home/', include('home.urls', namespace='home'))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Include the app to the &lt;code&gt;settings.py&lt;/code&gt; file
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSTALLED_APPS = [
   'home'
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Open in browser
&lt;/h4&gt;

&lt;p&gt;In the browser, open the following URL&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://localhost:8000/home/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;You will see the index page content.&lt;/p&gt;



&lt;p&gt;Congratulations, we have completed the setup of Django project using the docker as host.&lt;br&gt;
Now you can develop the Django application without depending on the host system.&lt;/p&gt;

&lt;p&gt;😊 😍 💻&lt;/p&gt;



&lt;p&gt;Check complete source code on Github&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/anuj9196" rel="noopener noreferrer"&gt;
        anuj9196
      &lt;/a&gt; / &lt;a href="https://github.com/anuj9196/django-docker-host" rel="noopener noreferrer"&gt;
        django-docker-host
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Docker host for the Django development. http://bit.ly/django-docker-host
    &lt;/h3&gt;
  &lt;/div&gt;
&lt;/div&gt;



</description>
      <category>django</category>
      <category>docker</category>
      <category>devops</category>
      <category>devjournal</category>
    </item>
    <item>
      <title>Django development using Docker as host - Part 4: docker-compose</title>
      <dc:creator>Anuj Sharma</dc:creator>
      <pubDate>Sun, 17 Jan 2021 07:27:41 +0000</pubDate>
      <link>https://dev.to/anujdev/django-development-using-docker-as-host-part-4-docker-compose-2313</link>
      <guid>https://dev.to/anujdev/django-development-using-docker-as-host-part-4-docker-compose-2313</guid>
      <description>&lt;p&gt;In the previous step, we initialized the Django application using the docker image.&lt;/p&gt;

&lt;p&gt;In this part, we will be creating the &lt;code&gt;docker-compose.yml&lt;/code&gt; file and define the required services to run the application.&lt;/p&gt;

&lt;h2&gt;
  
  
  TOC
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create directories&lt;/li&gt;
&lt;li&gt;Create &lt;code&gt;docker-compose.yml&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Define database (MySQL) service&lt;/li&gt;
&lt;li&gt;Define adminer server (Database management)&lt;/li&gt;
&lt;li&gt;Define app service&lt;/li&gt;
&lt;li&gt;Run docker-compose&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Let's start
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Create directories &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Create two directories at the root of the project, &lt;code&gt;src&lt;/code&gt; and &lt;code&gt;static_my_project&lt;/code&gt; in the host where source code of the Django application will reside in the &lt;code&gt;src&lt;/code&gt; directory and the static files in the &lt;code&gt;static_my_project&lt;/code&gt; directory.&lt;br&gt;
The directory structure 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;- /
  | - scripts
  | - src
  | - static_my_project
  | - docker-compose.yml
  | - Dockerfile
  | - requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Create &lt;code&gt;dokcer-compose.yml&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Create the &lt;code&gt;docker-compose.yml&lt;/code&gt; file in the host with the content&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '3.7'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Define database service &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Let's define a database service that will be consumed by the Django application.&lt;br&gt;
Add the following to the &lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;services:
  database:
    image: mysql:8.0
    container_name: app__database
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    volumes:
    - app_db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=app_db
      - MYSQL_DATABASE=app_db
      - MYSQL_USER=app_db
      - MYSQL_PASSWORD=app_db
    ports:
    - 3306:3306

volumes:
  app_db:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We are using &lt;code&gt;mysql&lt;/code&gt; version &lt;code&gt;8.0&lt;/code&gt; image for the database.&lt;/li&gt;
&lt;li&gt;We have also defined a docker volume to store database files inside &lt;code&gt;app_db&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The database will be running on port &lt;code&gt;3306&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Define adminer server (optional) &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://registry.hub.docker.com/_/adminer"&gt;&lt;strong&gt;Adminer&lt;/strong&gt;&lt;/a&gt; is a web-based database management service. Using this service we can directly access the database in the browser.&lt;br&gt;
Add the following content to the &lt;code&gt;docker-compose.yml&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;services:
  adminer:
    image: adminer:latest
    restart: always
    ports:
    - 8080:8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;adminer&lt;/code&gt; services will be running on port &lt;code&gt;8080&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Define app service &lt;a&gt;&lt;/a&gt;&lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Let's create an application service. We will use the &lt;code&gt;Dockerfile&lt;/code&gt; to build the image.&lt;br&gt;
Add the following content to the &lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: app_image
    command: ["/scripts/docker/wait_for_it.sh", "database:3306", "--", "/scripts/docker/docker_start.sh"]
    depends_on:
      - database
    ports:
    - 8000:8000
    volumes:
    - ./requirements.txt:/app/requirements.txt
    - ./static_my_project:/static_my_project
    - ./src:/app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The application will be build using the &lt;code&gt;Dockerfile&lt;/code&gt; we created in step 1&lt;/li&gt;
&lt;li&gt;When running, the application container will be named &lt;code&gt;app_image&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Django by default runs on port 8000, we have mapped the port 8000 of the host to the container so that the server can be accessed from the host browser.&lt;/li&gt;
&lt;li&gt;We have mapped the following host volumes to the container files/folder.

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;requirements.txt&lt;/code&gt; -- Any modification to the requirements.txt file in the container will be updated to the host requirements.txt file&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;static_my_project&lt;/code&gt; directory of the host will be mapped to the &lt;code&gt;static_my_project&lt;/code&gt; directory in the container.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;/app&lt;/code&gt; directory is mapped to the &lt;code&gt;./src&lt;/code&gt; directory in the container. Any changes to the host &lt;code&gt;./src&lt;/code&gt; or container &lt;code&gt;/app&lt;/code&gt; directory will be synced.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;The contents of the &lt;code&gt;docker-compose.yml&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;version: '3.7'

services:

  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: app_image
    command: ["/scripts/docker/wait_for_it.sh", "database:3306", "--", "/scripts/docker/docker_start.sh"]
    depends_on:
      - database
    ports:
    - 8000:8000
    volumes:
    - ./requirements.txt:/app/requirements.txt
    - ./static_my_project:/static_my_project
    - ./src:/app

  database:
    image: mysql:8.0
    container_name: app__database
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    volumes:
    - app_db:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=app_db
      - MYSQL_DATABASE=app_db
      - MYSQL_USER=app_db
      - MYSQL_PASSWORD=app_db
    ports:
    - 3306:3306

  adminer:
    image: adminer:latest
    restart: always
    ports:
    - 8080:8080

volumes:
  app_db:
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. Run docker-compose &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;To run the defined services inside the &lt;code&gt;docker-compose.yml&lt;/code&gt; file, let's run the services by executing&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker-compose up --build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will build the application image and run the services.&lt;/p&gt;

&lt;p&gt;Let's verify first if &lt;code&gt;database&lt;/code&gt; and &lt;code&gt;adminer&lt;/code&gt; services are running. Open the following link in the browser&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://localhost:8080
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the &lt;code&gt;adminer&lt;/code&gt; login page appears, enter the following details&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Server: database&lt;/li&gt;
&lt;li&gt;Username: app_db&lt;/li&gt;
&lt;li&gt;Password: app_db&lt;/li&gt;
&lt;li&gt;Database: app_db&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the login works, the &lt;code&gt;database&lt;/code&gt; and the &lt;code&gt;adminer&lt;/code&gt; services are successfully running.&lt;/p&gt;

</description>
      <category>django</category>
      <category>docker</category>
      <category>devops</category>
      <category>devjournal</category>
    </item>
    <item>
      <title>Django development using Docker as host - Part 3: Create Django project</title>
      <dc:creator>Anuj Sharma</dc:creator>
      <pubDate>Sun, 17 Jan 2021 07:27:31 +0000</pubDate>
      <link>https://dev.to/anujdev/django-development-using-docker-as-host-part-3-create-django-project-15b1</link>
      <guid>https://dev.to/anujdev/django-development-using-docker-as-host-part-3-create-django-project-15b1</guid>
      <description>&lt;p&gt;Till now we have completed with the &lt;code&gt;Dockerfile&lt;/code&gt; and the required script files. In this step, we will create a Django project inside the container.&lt;/p&gt;

&lt;h2&gt;
  
  
  TOC
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Build docker image&lt;/li&gt;
&lt;li&gt;Run the image&lt;/li&gt;
&lt;li&gt;Create Django project in the container&lt;/li&gt;
&lt;li&gt;Install python dependencies&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Let's start
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Build docker image &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;First, we need to build the image using the &lt;code&gt;Dockerfile&lt;/code&gt;. Execute the following command to build the image and add a tag &lt;code&gt;my_app&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker build -t my_app .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the image building has completed, you can see the image by running&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker images
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Run the image &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Run the &lt;code&gt;bash&lt;/code&gt; terminal using the image&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -it --rm -v "$(pwd)"/src:/app/ app_tut bash 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;-it&lt;/code&gt; will run the image in interactive tty mode&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--rm&lt;/code&gt; will remove the container when the run is exited&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;-v "$(pwd)/src:/app/&lt;/code&gt; will mount the &lt;code&gt;./src&lt;/code&gt; directory in current working directory to the &lt;code&gt;/app&lt;/code&gt; inside the container&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;my_app&lt;/code&gt; is the image name&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;bash&lt;/code&gt; is the command to execute inside the container.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Create Django project &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;First, verify the &lt;code&gt;Django&lt;/code&gt; installation inside the container by running the following command inside the container bash session&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will give a list of installed dependencies inside the container. Verify the &lt;code&gt;Django&lt;/code&gt; installation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Package    Version
---------- -------
Django     3.1.5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As &lt;code&gt;Django&lt;/code&gt; is installed, run the below command to create a Django project with name &lt;code&gt;my_app&lt;/code&gt; in the current directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;django-admin startproject my_app .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To confirm the project creation, list the files in the current directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ls -la
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see a directory &lt;code&gt;my_app&lt;/code&gt; and &lt;code&gt;manage.py&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-rwxr-xr-x 1 root root  662 Jan 16 17:54 manage.py
drwxr-xr-x 7 root root  224 Jan 16 17:54 my_app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Did you noticed something?&lt;/strong&gt;&lt;br&gt;
You can see the &lt;code&gt;my_app&lt;/code&gt; directory and the &lt;code&gt;manage.py&lt;/code&gt; in the host &lt;code&gt;src&lt;/code&gt; directory as well. &lt;br&gt;
This is because we have mounted the &lt;code&gt;src&lt;/code&gt; directory with the &lt;code&gt;/app&lt;/code&gt; directory using &lt;code&gt;-v&lt;/code&gt; parameter.&lt;/p&gt;

&lt;p&gt;This is all for initializing our Django project. In the next step, we will use the &lt;code&gt;docker-compose&lt;/code&gt; to run the Django application and mounted the volumes.&lt;/p&gt;

&lt;p&gt;Type &lt;code&gt;exit&lt;/code&gt; to exit from the docker image shell.&lt;/p&gt;
&lt;h3&gt;
  
  
  4. Install python dependencies &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Since we will be using the MySQL database with the Django project, &lt;code&gt;mysqlclient&lt;/code&gt; python dependency needs to be installed.&lt;br&gt;
Add the following to the &lt;code&gt;requirements.txt&lt;/code&gt; because its installation will require the &lt;code&gt;build-essential&lt;/code&gt; library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mysqlclient==2.0.3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>django</category>
      <category>docker</category>
      <category>devops</category>
      <category>devjournal</category>
    </item>
    <item>
      <title>Django development using Docker as host - Part 2: Create scripts</title>
      <dc:creator>Anuj Sharma</dc:creator>
      <pubDate>Sun, 17 Jan 2021 07:27:18 +0000</pubDate>
      <link>https://dev.to/anujdev/django-development-using-docker-as-host-part-2-create-scripts-3l3k</link>
      <guid>https://dev.to/anujdev/django-development-using-docker-as-host-part-2-create-scripts-3l3k</guid>
      <description>&lt;p&gt;In &lt;strong&gt;Part 1&lt;/strong&gt;, we created a &lt;strong&gt;Dockerfile&lt;/strong&gt; with the required system dependencies.&lt;/p&gt;

&lt;p&gt;In this part, we will be creating the required files and scripts.&lt;/p&gt;

&lt;h2&gt;
  
  
  TOC
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create &lt;code&gt;requirements.txt&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create &lt;code&gt;entrypoint.sh&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create &lt;code&gt;docker_start.sh&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create &lt;code&gt;wait_for_it.sh&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Make scripts executable&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Let's dive in
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Create &lt;code&gt;requirements.txt&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;First, we need to define our basic python dependency for the Django project setup. Although it is not required, to omit any error with the &lt;code&gt;pip install -r requirements.txt&lt;/code&gt; inside the container, let's add our first dependency.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Django==3.1.5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Create &lt;code&gt;entrypoint.sh&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Entrypoint script is executed every time the image is run. I recommend not to run the Django server using this.&lt;br&gt;
Create a file &lt;code&gt;scripts/docker/entrypoint.sh&lt;/code&gt; with the following content&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/sh

exec "$@"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Create &lt;code&gt;docker_start.sh&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;We will use this script to run the Django server.&lt;br&gt;
Create a file &lt;code&gt;scripts/docker/docker_start.sh&lt;/code&gt; with the following content&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/usr/bin/env bash

echo -e "\e[34m &amp;gt;&amp;gt;&amp;gt; Migrating changes \e[97m"
python manage.py migrate
echo -e "\e[32m &amp;gt;&amp;gt;&amp;gt; migration completed \e[97m"

echo -e "\e[34m &amp;gt;&amp;gt;&amp;gt; Collecting Static files \e[97m"
python manage.py collectstatic --noinput
echo -e "\e[32m &amp;gt;&amp;gt;&amp;gt; Static files collect completed \e[97m"

python manage.py runserver 0.0.0.0:8000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will run the migration, copies the static files and run the server over port &lt;code&gt;8000&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Create &lt;code&gt;wait_for_it.sh&lt;/code&gt; &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;This script is needed to wait for the database connection to start before starting the Django server.&lt;br&gt;
Create a file &lt;code&gt;scripts/docker/wait_for_it.sh&lt;/code&gt; with the content from git repository &lt;a href="https://github.com/vishnubob/wait-for-it"&gt;vishnubob/wait-for-it&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  5. Make scripts executable &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;In order to execute the scripts, it is required to make them executable.&lt;br&gt;
Run the following command in the terminal from the project directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chmod +x scripts/docker/entrypoint.sh
chmod +x scripts/docker/docker_start.sh
chmod +x scripts/docker/wait_for_it.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it for this part. Next, we will create the Django project using the docker image.&lt;/p&gt;

</description>
      <category>django</category>
      <category>docker</category>
      <category>devops</category>
      <category>devjournal</category>
    </item>
    <item>
      <title>Django development using Docker as host - Part 1: Dockerfile</title>
      <dc:creator>Anuj Sharma</dc:creator>
      <pubDate>Sun, 17 Jan 2021 07:27:01 +0000</pubDate>
      <link>https://dev.to/anujdev/django-development-using-docker-as-host-part-1-dockerfile-3bnc</link>
      <guid>https://dev.to/anujdev/django-development-using-docker-as-host-part-1-dockerfile-3bnc</guid>
      <description>&lt;blockquote&gt;
&lt;h3&gt;
  
  
  Django
&lt;/h3&gt;

&lt;p&gt;Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel.&lt;/p&gt;
&lt;h3&gt;
  
  
  Docker
&lt;/h3&gt;

&lt;p&gt;Docker is a set of &lt;code&gt;platform as a service&lt;/code&gt; products that use OS-level virtualization to deliver software in packages called containers.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Recently while developing a Django project, I faced multiple issues installing the system dependencies (mysqlclient, etc) to install the required package for the working of my Django application in the local development environment.&lt;/p&gt;

&lt;p&gt;This is always a hassle installing the system dependencies in the development machine, when&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The host is formatted&lt;/li&gt;
&lt;li&gt;You change your development machine&lt;/li&gt;
&lt;li&gt;Change of the operating system (say windows to Linux)&lt;/li&gt;
&lt;li&gt;Upgrading of operating system (&lt;em&gt;removes support for the earlier version of libraries, always comes with the new version of libraries which may not have support for the application&lt;/em&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And again you start the race with the system setup, killing lots of time.&lt;/p&gt;

&lt;p&gt;Even though we use &lt;code&gt;Dockerfile&lt;/code&gt; to create production-ready images to run the production server, why not use the same as host and remove the system dependencies with all phases of the development?&lt;/p&gt;

&lt;p&gt;Using the &lt;code&gt;Docker&lt;/code&gt; as host, the release servers will always be in sync with the development machine and you can always look over the dependencies required for your application.&lt;/p&gt;

&lt;h2&gt;
  
  
  TOC
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create &lt;code&gt;Dockerfile&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add non-root user&lt;/li&gt;
&lt;li&gt;Install runtime dependencies&lt;/li&gt;
&lt;li&gt;Copy files to the container&lt;/li&gt;
&lt;li&gt;Install python dependencies&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;ENTRYPOINT&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Let's jump in
&lt;/h2&gt;

&lt;p&gt;If you have not already installed &lt;code&gt;Docker&lt;/code&gt; and &lt;code&gt;docker-compose&lt;/code&gt; on your machine, first install both by following the official guide.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Docker&lt;/strong&gt;: &lt;a href="https://docs.docker.com/get-docker/"&gt;https://docs.docker.com/get-docker/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;docker-compose&lt;/strong&gt;: &lt;a href="https://docs.docker.com/compose/install/"&gt;https://docs.docker.com/compose/install/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1. Create &lt;code&gt;Dockerfile&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;We need to first create a &lt;code&gt;Dockerfile&lt;/code&gt; in the project directory, where we will be creating the Django project. &lt;br&gt;
To keep the image size lower, we will be using the latest slim python image &lt;code&gt;python:3.9.1-slim&lt;/code&gt;.&lt;br&gt;
Add the following to the first line of the Dockerfile&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM python:3.9.1-slim
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Add non-root user
&lt;/h3&gt;

&lt;p&gt;It is always recommended to create a non-root user instead of using the root user in the Dockerfile. So let's create a user by the name of the project (myapp).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ARG APP_USER=myapp
RUN groupadd -r ${APP_USER} &amp;amp;&amp;amp; useradd --no-log-init -r -m -g ${APP_USER} ${APP_USER}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;ARG APP_USER=myapp&lt;/code&gt; creates an argument variable so that the value &lt;code&gt;myapp&lt;/code&gt; can be referenced using the variable throughout the Dockerfile.&lt;br&gt;
The next command adds a group and creates a user with the &lt;code&gt;myapp&lt;/code&gt;, creates home directory &lt;code&gt;/home/myapp&lt;/code&gt; for the user and assigns the permission to the home directory.&lt;/p&gt;
&lt;h3&gt;
  
  
  3. Install runtime dependencies
&lt;/h3&gt;

&lt;p&gt;With Django, we are going to use the latest version of MySQL, so let's add the required system dependencies to the Dockerfile. Also, we will clean the Linux apt list as it is no longer required.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# default-libmysqlclient-dev -- Required for mysql database support
RUN set -ex \
    # Runtime dependencies
    &amp;amp;&amp;amp; RUN_DEPS=" \
    default-libmysqlclient-dev \
    " \
    &amp;amp;&amp;amp; seq 1 8 | xargs -I{} mkdir -p /usr/share/man/man{} \
    &amp;amp;&amp;amp; apt-get update &amp;amp;&amp;amp; apt-get install -y --no-install-recommends $RUN_DEPS \
    # Remove package list
    &amp;amp;&amp;amp; rm -rf /var/lib/apt/lists/* \
    &amp;amp;&amp;amp; mkdir /static_my_project
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What above command does is &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Installs the &lt;code&gt;mysqlclient&lt;/code&gt; system dependency&lt;/li&gt;
&lt;li&gt;Removes the apt lists&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Copy files to the container
&lt;/h3&gt;

&lt;p&gt;Now, change the working directory to the &lt;code&gt;/app/&lt;/code&gt; directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WORKDIR /app/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's copy the &lt;code&gt;requirements.txt&lt;/code&gt; file for the dependency instalment. (&lt;em&gt;We will create it in further steps&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;ADD requirements.txt /requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's copy the application source code and the scripts to the container&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;COPY ./src /app/
COPY scripts/ /scripts/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Install python dependencies
&lt;/h3&gt;

&lt;p&gt;Now we will install the required dependencies from the &lt;code&gt;requirements.txt&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RUN set -ex \
    # Define build dependencies, they will be removed after build completes and libraries has been installed
    &amp;amp;&amp;amp; BUILD_DEPS=" \
    build-essential \
    " \
    &amp;amp;&amp;amp; apt-get update &amp;amp;&amp;amp; apt-get install -y --no-install-recommends $BUILD_DEPS \
    &amp;amp;&amp;amp; pip install -r /requirements.txt  \
    &amp;amp;&amp;amp; apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false $BUILD_DEPS \
    &amp;amp;&amp;amp; rm -rf /var/lib/apt/lists/*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above command does the following&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Installs the build dependencies&lt;/li&gt;
&lt;li&gt;Installs the python dependencies&lt;/li&gt;
&lt;li&gt;Removes the build dependencies&lt;/li&gt;
&lt;li&gt;Deletes the repository lists&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we need to expose the port &lt;code&gt;8000&lt;/code&gt; to access the running server&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;EXPOSE 8000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. Add &lt;code&gt;ENTRYPOINT&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Let's add an &lt;code&gt;ENTRYPOINT&lt;/code&gt; which allows running manage.py commands without running the server directly. We will run the server by executing a command on the image. (&lt;em&gt;I personally use this pattern because it gives you the flexibility to directly run Django shell without running the server&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;ENTRYPOINT ["/scripts/docker/entrypoint.sh"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;That's it for the &lt;code&gt;Dockerfile&lt;/code&gt;. Here is the final content&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM python:3.9.1-slim

ARG APP_USER=myapp
RUN groupadd -r ${APP_USER} &amp;amp;&amp;amp; useradd --no-log-init -r -m -g ${APP_USER} ${APP_USER}

# default-libmysqlclient-dev -- Required for mysql database support
RUN set -ex \
    # Runtime dependencies
    &amp;amp;&amp;amp; RUN_DEPS=" \
    default-libmysqlclient-dev \
    " \
    &amp;amp;&amp;amp; seq 1 8 | xargs -I{} mkdir -p /usr/share/man/man{} \
    &amp;amp;&amp;amp; apt-get update &amp;amp;&amp;amp; apt-get install -y --no-install-recommends $RUN_DEPS \
    # Remove package list
    &amp;amp;&amp;amp; rm -rf /var/lib/apt/lists/* \
    &amp;amp;&amp;amp; mkdir /static_my_project

WORKDIR /app/

ADD requirements.txt /requirements.txt
COPY ./src /app/
COPY scripts/ /scripts/

# build-essential -- Required to build python mysqlclient library. https://packages.debian.org/sid/build-essential
RUN set -ex \
    # Define build dependencies, they will be removed after build completes and libraries has been installed
    &amp;amp;&amp;amp; BUILD_DEPS=" \
    build-essential \
    " \
    &amp;amp;&amp;amp; apt-get update &amp;amp;&amp;amp; apt-get install -y --no-install-recommends $BUILD_DEPS \
    &amp;amp;&amp;amp; pip install -r /requirements.txt  \
    &amp;amp;&amp;amp; apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false $BUILD_DEPS \
    &amp;amp;&amp;amp; rm -rf /var/lib/apt/lists/*

ENTRYPOINT ["/scripts/docker/entrypoint.sh"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>django</category>
      <category>docker</category>
      <category>devops</category>
      <category>devjournal</category>
    </item>
  </channel>
</rss>
