Instructions to create Django app through Docker Compose

Tram Ho

Foreword

In the past, when I first learned the code, I was immediately assigned to work in a real project, my legs and feet didn’t know anything so at first I just sat around with Docker, Django, … But the English part was stuck. flat, so I don’t understand how to use it, after that, I plowed and asked the seniors to get through. So today I would like to write a tutorial on how to create a django app via docker-compose in Vietnamese 😄 .

Define the components of the project

The completion of a project takes a lot of time and effort, which is done giving the customer still FAIL 😢 . The reason is mostly because the code environment is different from the customer environment. So Docker was born as a deploying tool.

Because I use the Linux operating system, specifically Ubuntu 16.04, you should use Windows, Mac sympathy (bow).

Install Docker and Docker Compose

The first thing you need to do is install Docker and Compose.

  1. Add GPG key from the Docker homepage to your device

  1. Add Docker repo to apt of ubuntu

  1. Updated apt

  1. Check the version you want to install must be from the Docker repo and not the existing version of Ubuntu

Docker-ce has not been installed but has a backup version of ubuntu.

  1. If OK, install Docker

  1. After installing, check the status of docker

If you see active (running) then it is already running, do not pay attention because I copied the code from the manual page. Chung attributed to lazy 😕 .

  1. Docker is running, now install docker-compose only. Download the stable version of docker-compose on github.

  1. Authorization

  1. Check the version

Dockerfile

You create an empty folder, go there

Create a file named Dockerfile to create image in docker. In general, the project environment is formatted as follows

Dockerfile begins with an built-in image environment “ubuntu: 16.04”.

Then install python3

Create folder in image container

If you have a file requirements (which stores the libraries needed by the project) then copy it to the code folder in the container

When creating the image, we will install the libraries in this requirement file so if your project needs something, put it in this requirement file. For example:

Docker-compose.yml

You create another file docker-compose.yml in parallel with Dockerfile. This file has the role of describing the services to be used in the project, namely the web and database.

The version used here is 3

Service 1 is the db aka database. I use postgres here so I will pull the postgres image (latest version), name the container “test_django_app_db”

Service 2 is the web. Name the container, build context, map the folder inside the container to the outside of the local (here is test_django_app) so that when you code locally it will be saved to the container immediately, the command runs right when the docker-compose build (python ./test_django_app) /manage.py runserver 0.0.0.0:8000), expose port (default is 8000), depends_on db (associated with db service).

Django app

In the test_django_app folder, create a django project

Check, if you see the project name then you are successful (actually I just copied the instructions only 😄 )

Connect App to Database

In the file test_django_app / settings.py, reconfigure the variable DATABASE

Finally, run the command “docker-compose up” in the test_django_app folder to create the project.

Check if the container has been created

Last word

Write tired all hands 😕 , above is my simple guide to creating a django app with docker. The article does not go into detail on the theory, uhm if the enthusiasm rises, maybe I’ll write an explanation of the terms in the docker. 😄 .

Reference link from:

https://docs.docker.com/compose/django/

https://www.digitalocean.com/community/tutorials/how-to-install-docker-compose-on-ubuntu-16-04

https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-16-04

Share the news now

Source : Viblo