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
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.
- Add GPG key from the Docker homepage to your device
1 2 |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - |
- Add Docker repo to apt of ubuntu
1 2 |
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" |
- Updated apt
1 2 |
sudo apt-get update |
- Check the version you want to install must be from the Docker repo and not the existing version of Ubuntu
1 2 |
apt-cache policy docker-ce |
1 2 3 4 5 6 7 |
docker-ce: Installed: (none) Candidate: 18.06.1~ce~3-0~ubuntu Version table: 18.06.1~ce~3-0~ubuntu 500 500 https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages |
Docker-ce has not been installed but has a backup version of ubuntu.
- If OK, install Docker
1 2 |
sudo apt-get install -y docker-ce |
- After installing, check the status of docker
1 2 |
sudo systemctl status docker |
1 2 3 4 5 6 7 8 9 |
● docker.service - Docker Application Container Engine Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2018-10-18 20:28:23 UTC; 35s ago Docs: https://docs.docker.com Main PID: 13412 (dockerd) CGroup: /system.slice/docker.service ├─13412 /usr/bin/dockerd -H fd:// └─13421 docker-containerd --config /var/run/docker/containerd/containerd.toml |
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
- Docker is running, now install docker-compose only. Download the stable version of docker-compose on github.
1 2 |
sudo curl -L "https://github.com/docker/compose/releases/download/1.25.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose |
- Authorization
1 2 |
sudo chmod +x /usr/local/bin/docker-compose |
- Check the version
1 2 |
docker-compose --version |
1 2 |
docker-compose version 1.25.3, build 1110ad01 |
Dockerfile
You create an empty folder, go there
1 2 3 |
mkdir test_django_app cd test_django_app |
Create a file named Dockerfile to create image in docker. In general, the project environment is formatted as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 |
FROM ubuntu:16.04 RUN apt-get update && apt-get install -y python3-pip python3-dev && cd /usr/local/bin && ln -s /usr/bin/python3 python && pip3 install --upgrade pip RUN mkdir /code WORKDIR /code COPY requirements.txt /code/ RUN pip install -r requirements.txt |
Dockerfile begins with an built-in image environment “ubuntu: 16.04”.
1 2 |
FROM ubuntu:16.04 |
Then install python3
1 2 3 4 5 6 |
RUN apt-get update && apt-get install -y python3-pip python3-dev && cd /usr/local/bin && ln -s /usr/bin/python3 python && pip3 install --upgrade pip |
Create folder in image container
1 2 3 |
RUN mkdir /code WORKDIR /code |
If you have a file requirements (which stores the libraries needed by the project) then copy it to the code folder in the container
1 2 3 |
COPY requirements.txt /code/ RUN pip install -r requirements.txt |
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:
1 2 |
cat test_django_app/requirement.txt |
1 2 3 |
Django>=2.0,<3.0 psycopg2>=2.7,<3.0 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
version: '3' services: db: image: postgres web: build: . command: python manage.py runserver 0.0.0.0:8000 volumes: - .:/code ports: - "8000:8000" depends_on: - db |
The version used here is 3
1 2 |
version: '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”
1 2 3 4 5 |
db: image: postgres:latest container_name: test_django_app_db restart: always |
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).
1 2 3 4 5 6 7 8 9 10 11 12 13 |
web: container_name: test_django_app_web build: . volumes: - .:/code command: bash -c "python ./test_django_app/manage.py runserver 0.0.0.0:8000" ports: - "8000:8000" tty: true depends_on: - db restart: always |
Django app
In the test_django_app folder, create a django project
1 2 |
sudo docker-compose run web django-admin startproject test_django_app |
Check, if you see the project name then you are successful (actually I just copied the instructions only
1 2 3 4 5 6 7 |
$ ls -l drwxr-xr-x 2 root root test_django_app -rw-rw-r-- 1 user user docker-compose.yml -rw-rw-r-- 1 user user Dockerfile -rwxr-xr-x 1 root root manage.py -rw-rw-r-- 1 user user requirements.txt |
Connect App to Database
In the file test_django_app / settings.py, reconfigure the variable DATABASE
1 2 3 4 5 6 7 8 9 10 |
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'postgres', 'USER': 'postgres', 'HOST': 'db', 'PORT': 5432, } } |
Finally, run the command “docker-compose up” in the test_django_app folder to create the project.
1 2 |
docker-compose up |
Check if the container has been created
1 2 3 4 5 |
$sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES def85eff5f51 testdjangoapp_web "python3 manage.py..." 10 minutes ago Up 9 minutes 0.0.0.0:8000->8000/tcp test_django_app_web 678ce61c79cc postgres:latest "docker-entrypoint..." 20 minutes ago Up 9 minutes 5432/tcp test_django_app_db |
Last word
Write tired all hands
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