{"id":2570,"date":"2022-07-29T11:09:36","date_gmt":"2022-07-29T11:09:36","guid":{"rendered":"https:\/\/www.checkmateq.com\/blog\/?p=2570"},"modified":"2023-08-04T16:37:23","modified_gmt":"2023-08-04T16:37:23","slug":"docker-compose","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/docker-compose","title":{"rendered":"Deploy django application using docker compose"},"content":{"rendered":"<p><strong>Docker compose is a tool using which we can define and run multi-container applications<\/strong>. With docker-compose, we can define configurations using a<strong> single YAML file<\/strong> and manage <strong>multiple containers using a single command<\/strong>.<\/p>\n<p>In this blog, we will deploy a <a href=\"https:\/\/www.checkmateq.com\/django-development\">Django<\/a> application using docker-compose. In production, a Django application is built with the following components:<\/p>\n<ol>\n<li><strong>WSGI server &#8211; Gunicorn\u00a0<\/strong><\/li>\n<li><strong>Web Server &#8211; Nginx<\/strong><\/li>\n<li><strong>Database &#8211; PostgreSQL<\/strong><\/li>\n<\/ol>\n<p>To package a Django <a href=\"https:\/\/www.checkmateq.com\/application\">application<\/a> with Docker, Gunicorn, Nginx, and PostgreSQL follow the below steps.<\/p>\n<p><strong>Step1: Create Dockerfile for the Django application<\/strong><\/p>\n<p>In this blog the directory structure followed is as below.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2576\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Checkmate-cloud-operation-management--300x135.png\" alt=\"\" width=\"749\" height=\"337\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Checkmate-cloud-operation-management--300x135.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Checkmate-cloud-operation-management--768x347.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Checkmate-cloud-operation-management-.png 780w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Go to the directory where Django code is present. Create a file with the name <strong>Dockerfile<\/strong> and paste the following code there.<\/p>\n<pre>FROM python:3.8-slim-buster\r\nENV PYTHONBUFFERED=1\r\nRUN pip install --upgrade pip\r\nWORKDIR \/app\r\nCOPY requirements.txt requirements.txt\r\nRUN pip install -r requirements.txt\r\nCOPY . .\r\nCMD [\"\/bin\/bash\", \"entrypoint.sh\"]\r\n<\/pre>\n<p>Create a file<strong> entrypoint.sh <\/strong>and paste the following script there. Replace the WSGI module <strong>core.wsgi:application<\/strong> according to your application.<\/p>\n<pre>#!\/bin\/bash\r\n\r\npython manage.py makemigrations --no-input\r\npython manage.py migrate --no-input\r\npython3 manage.py collectstatic --no-input\r\n\r\ngunicorn core.wsgi:application --bind 0.0.0.0:8000\r\n<\/pre>\n<h3>Step2: Create Dockerfile for Nginx<\/h3>\n<p>Go to nginx directory. Create a <strong>Dockerfile\u00a0<\/strong>and paste the following code there.<\/p>\n<pre>FROM nginx:1.19.0-alpine\r\n\r\nCOPY .\/default.conf \/etc\/nginx\/conf.d\/default.conf\r\n<\/pre>\n<p>Now create a file <strong>default.conf<\/strong> and paste the following code there.<\/p>\n<pre>upstream django {\r\nserver django_app:8000;\r\n}\r\n\r\nserver {\r\n     listen 80;\r\n \r\n     location \/ {\r\n          proxy_pass http:\/\/django;\r\n          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\r\n          proxy_set_header Host $host;\r\n          proxy_redirect off;\r\n          }\r\n\r\n    location \/static\/ {\r\n        alias \/app\/staticfiles\/ ;\r\n         }\r\n}\r\n<\/pre>\n<h3>Step3: Create a docker-compose file<\/h3>\n<p>Now create a docker-compose.yaml file and paste the following code there.<\/p>\n<pre>version: \"3.8\"\r\nservices:\r\n   django_app:\r\n     build: .\/djangoapp\r\n     volumes:\r\n       - staticfiles:\/app\/staticfiles\r\n     ports:\r\n       - 8000:8000\r\n     image: app:django\r\n     depends_on:\r\n      - pgdb\r\n     restart: \"on-failure\"\r\n\r\n   nginx:\r\n     build: .\/nginx\r\n     volumes:\r\n       - staticfiles:\/app\/staticfiles\r\n     ports:\r\n       - 80:80\r\n     image: nginx:django\r\n     depends_on:\r\n       - django_app\r\n     restart: \"on-failure\"\r\n\r\n   pgdb:\r\n     image: postgres:13\r\n     volumes:\r\n       - pg_data:\/var\/lib\/postgresql\/data\/\r\n     environment:\r\n       - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}\r\n       - POSTGRES_DB=${DB_NAME}\r\n       - POSTGRES_USER=${POSTGRES_USER}\r\n     restart: \"on-failure\"\r\n\r\nvolumes:\r\n   staticfiles:\r\n   pg_data:\r\n\r\n<\/pre>\n<p>Next, create a .env file and add the Postgres environment variables to it.<\/p>\n<pre>POSTGRES_PASSWORD=postgres_pass\r\nDB_NAME=postgres_db\r\nPOSTGRES_USER=postgres_user\r\n<\/pre>\n<p>Step4: Start the application<\/p>\n<p>Use the following command to build the images and start the containers.<\/p>\n<pre>docker compose up --build\r\n<\/pre>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2583\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Checkmate-cloud-infrastructure-management-1-1-300x145.png\" alt=\"docker compose\" width=\"751\" height=\"363\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Checkmate-cloud-infrastructure-management-1-1-300x145.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Checkmate-cloud-infrastructure-management-1-1-1024x495.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Checkmate-cloud-infrastructure-management-1-1-768x372.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Checkmate-cloud-infrastructure-management-1-1-1536x743.png 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Checkmate-cloud-infrastructure-management-1-1-1200x581.png 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Checkmate-cloud-infrastructure-management-1-1.png 1695w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>To run the containers in the background use <strong>-d<\/strong> flag.<\/p>\n<pre>docker compose up -d\r\n<\/pre>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-2584\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Checkmate-Infrastructure-services-1-1-300x31.png\" alt=\"docker compose\" width=\"755\" height=\"78\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Checkmate-Infrastructure-services-1-1-300x31.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Checkmate-Infrastructure-services-1-1-1024x107.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Checkmate-Infrastructure-services-1-1-768x80.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/07\/Checkmate-Infrastructure-services-1-1.png 1162w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Now you can access your application from the browser.<\/p>\n<p><a href=\"https:\/\/www.checkmateq.com\/cloud\">Please contact<\/a> our technical consultants if you have anything related to cloud infrastructure to be discussed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Docker compose is a tool using which we can define and run multi-container applications. With docker-compose, we can define configurations using a single YAML file and manage multiple containers using a single command. In this blog, we will deploy a Django application using docker-compose. In production, a Django application is built with the following components: &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/docker-compose\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Deploy django application using docker compose&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":2588,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[15,53,2,70,68,7,11,16],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2570"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=2570"}],"version-history":[{"count":20,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2570\/revisions"}],"predecessor-version":[{"id":4261,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2570\/revisions\/4261"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media\/2588"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=2570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=2570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=2570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}