{"id":77,"date":"2022-10-21T19:32:00","date_gmt":"2022-10-21T19:32:00","guid":{"rendered":"https:\/\/zelazkiewicz.com\/?p=77"},"modified":"2023-03-23T09:56:12","modified_gmt":"2023-03-23T09:56:12","slug":"simplifying-django-development-with-docker-and-docker-compose","status":"publish","type":"post","link":"https:\/\/zelazkiewicz.com\/python\/simplifying-django-development-with-docker-and-docker-compose\/","title":{"rendered":"Simplifying Django Development with Docker and Docker Compose"},"content":{"rendered":"\n<p>Docker is an open-source platform that allows developers to automate and streamline the process of building, packaging, and deploying applications in self-contained environments called containers. One of the most popular web frameworks, Django, can greatly benefit from Dockerization. We will discuss the advantages of using Docker for Django development and walk through the process of setting up a Docker Environment for a Django project.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Dockerizing Django<\/h2>\n\n\n\n<ol>\n<li>Consistent environment: Docker ensures a consistent environment across different stages of the development lifecycle, eliminating the &#8220;it works on my machine&#8221; issue. Containers have all the dependencies required for an application, making it easy to share and run the application on any system with Docker installed.<\/li>\n\n\n\n<li>Simplified dependency management: Docker allows you to manage all your project dependencies in a single Dockerfile, reducing the complexity of managing different dependencies across multiple environments.<\/li>\n\n\n\n<li>Scalability: Docker&#8217;s lightweight containers make it easier to scale applications horizontally. With Docker Compose, you can define a multi-container application, allowing for seamless scaling of individual components.<\/li>\n\n\n\n<li>Faster onboarding: New team members can quickly set up their development environments by pulling the repository with Docker configuration, building the image and running the containers, saving time and effort.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Setting up a Dockerized Django project<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Install Docker and Docker Compose<\/h3>\n\n\n\n<p>First, make sure you have Docker and Docker Compose installed on your system. You can download them from the official Docker website (<a href=\"https:\/\/www.docker.com\/\">https:\/\/www.docker.com\/<\/a>) and follow the installation instructions for your specific operating system.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create a Django project directory<\/h3>\n\n\n\n<p>Create a new directory for your Django project and navigate to it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mkdir my_django_project\ncd my_django_project<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Create a Dockerfile<\/h3>\n\n\n\n<p>Create a <code>Dockerfile<\/code> in the project directory with the following content:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Use the official Python image as the base image\nFROM python:3.10\n\n# Set the working directory\nWORKDIR \/app\n\n# Copy the requirements file into the container\nCOPY requirements.txt .\n\n# Install the dependencies\nRUN pip install -r requirements.txt\n\n# Copy the rest of the application code\nCOPY . .\n\n# Expose the port the app runs on\nEXPOSE 8000\n\n# Start the application\nCMD &#91;\"python\", \"manage.py\", \"runserver\", \"0.0.0.0:8000\"]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Create a requirements.txt file<\/h3>\n\n\n\n<p>Create a <code>requirements.txt<\/code> file in the project directory with the following content:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Django<\/code><\/pre>\n\n\n\n<p>Feel free to add any other dependencies your project may require.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create a docker-compose.yml file<\/h3>\n\n\n\n<p>Create a <code>docker-compose.yml<\/code> file in the project directory with the following content:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>version: '3.8'\n\nservices:\n  web:\n    build: .\n    volumes:\n      - .:\/app\n    ports:\n      - \"8000:8000\"<\/code><\/pre>\n\n\n\n<p>This configuration defines a single service named &#8216;web&#8217; that will build and run our Django application.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Initialize the Django project<\/h3>\n\n\n\n<p>Run the following command to create a new Django project inside the container:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker-compose run web django-admin startproject mysite .<\/code><\/pre>\n\n\n\n<p>This command will create a new Django project named &#8216;mysite&#8217; in your current directory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Run the Django development server<\/h3>\n\n\n\n<p>Start the Django development server by running:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker-compose up<\/code><\/pre>\n\n\n\n<p>Access the Django app by navigating to <code>http:\/\/localhost:8000<\/code> in your web browser. You should see the default Django welcome page.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Make database migrations<\/h3>\n\n\n\n<p>If you need to set up a database for your Django project, you can easily configure it in the <code>docker-compose.yml<\/code> file. For instance, let&#8217;s add a PostgreSQL database:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>version: '3.8'\n\nservices:\n  db:\n    image: postgres:13.5-alpine\n    environment:\n      POSTGRES_USER: myuser\n      POSTGRES_PASSWORD: mypassword\n      POSTGRES_DB: mydb\n  web:\n    build: .\n    command: python manage.py runserver 0.0.0.0:8000\n    volumes:\n      - .:\/app\n    ports:\n      - \"8000:8000\"\n    depends_on:\n      - db<\/code><\/pre>\n\n\n\n<p>Update your <code>requirements.txt<\/code> file to include the PostgreSQL adapter for Django:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Django\npsycopg2<\/code><\/pre>\n\n\n\n<p>Then, modify the <code>DATABASES<\/code> setting in your <code>mysite\/settings.py<\/code> file to use the PostgreSQL database:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DATABASES = {\n    'default': {\n        'ENGINE': 'django.db.backends.postgresql',\n        'NAME': 'mydb',\n        'USER': 'myuser',\n        'PASSWORD': 'mypassword',\n        'HOST': 'db',\n        'PORT': 5432,\n    }\n}<\/code><\/pre>\n\n\n\n<p>Run the following commands to apply the database migrations:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>docker-compose down\ndocker-compose up --build -d\ndocker-compose run web python manage.py makemigrations\ndocker-compose run web python manage.py migrate<\/code><\/pre>\n\n\n\n<p>Now, your Django project is connected to a PostgreSQL database, and you can start developing your application.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Docker is an open-source platform that allows developers to automate and streamline the process of building, packaging, and deploying applications in self-contained environments called containers. One of the most popular web frameworks, Django, can greatly benefit from Dockerization. We will discuss the advantages of using Docker for Django development and walk through the process of &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/zelazkiewicz.com\/python\/simplifying-django-development-with-docker-and-docker-compose\/\" class=\"more-link\">Read more<span class=\"screen-reader-text\"> &#8220;Simplifying Django Development with Docker and Docker Compose&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,19,9],"tags":[],"featured_media_urls":[],"_links":{"self":[{"href":"https:\/\/zelazkiewicz.com\/wp-json\/wp\/v2\/posts\/77"}],"collection":[{"href":"https:\/\/zelazkiewicz.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zelazkiewicz.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zelazkiewicz.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zelazkiewicz.com\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":3,"href":"https:\/\/zelazkiewicz.com\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":156,"href":"https:\/\/zelazkiewicz.com\/wp-json\/wp\/v2\/posts\/77\/revisions\/156"}],"wp:attachment":[{"href":"https:\/\/zelazkiewicz.com\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zelazkiewicz.com\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zelazkiewicz.com\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}