#4 Mendeploy Django di Docker (Part 1)

#4 Mendeploy Django di Docker (Part 1)

Part 1

Environment Django

Pada tutorial sebelumnya kita menginstall django dalam environment dengan menggunakan library venv. Ada yang bertanya bagaimana mendeploy django di docker? Sebelumnya kita akan menginstall django di localhost dengan library pembuat environment python selain venv yaitu pipenv.

  1. Langkah pertama Install python. Pastikan python sudah terinstall cek dengan code berikut:
D:\>python --version
  1. Kemudian cek pip list, apakah sudah terinstall pipenv? Jika belum install pipenv.
D:\>pip list

Package          Version
---------------- ---------
certifi          2023.7.22
distlib          0.3.7
filelock         3.12.2
pip              23.1.2
pipenv           2023.7.11
platformdirs     3.9.1
setuptools       65.5.0
virtualenv       20.24.1
virtualenv-clone 0.5.7

Terlihat package pipenv sudah terinstall.

  1. Buat folder misalnya folder ‘djangoku’ di D:
D:\> mkdir djangoku
D:\> cd djangoku
D:\djangoku>pipenv install django

Creating a virtualenv for this project...
Pipfile: D:\djangoku\Pipfile
Using default python from C:\Program Files\Python311\python.exe (3.11.4) to create virtualenv...
[  ==] Creating virtual environment...created virtual environment CPython3.11.4.final.0-64 in 1779ms
  creator CPython3Windows(dest=C:\Users\BlueInk\.virtualenvs\djangoku-wI74BJPd, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\BlueInk\AppData\Local\pypa\virtualenv)
    added seed packages: pip==23.1.2, setuptools==68.0.0, wheel==0.40.0
  activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
[ ===] Creating virtual environment...
Successfully created virtual environment!
Virtualenv location: C:\Users\BlueInk\.virtualenvs\djangoku-wI74BJPd
Creating a Pipfile for this project...
Installing django...
Resolving django...
Adding django to Pipfile's [packages] ...
Installation Succeeded
Pipfile.lock not found, creating...
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
Success!
Locking [dev-packages] dependencies...
Updated Pipfile.lock (af42abefb766e975f7680f10368735353569fb4fe0114e59496a7202658362fc)!
Installing dependencies from Pipfile.lock (8362fc)...
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

Dalam folder djangoku yang terbentuk adalah file Pipfile dan Pipfile.lock

  1. Aktifkan environment dengan perintah ‘pipenv shell’:
D:\djangoku>pipenv shell
Launching subshell in virtual environment...
Microsoft Windows [Version 10.0.19045.3086]
(c) Microsoft Corporation. All rights reserved.

(djangoku-wI74BJPd) D:\djangoku>

Sekarang environment djangoku sudah aktif. Selanjutnya?

  1. Selanjutnya kita akan cek paket apa saja yang terinstall dalam environment djangoku:
(djangoku-wI74BJPd) D:\djangoku>pip list
Package    Version
---------- -------
asgiref    3.7.2
Django     4.2.3
pip        23.1.2
setuptools 68.0.0
sqlparse   0.4.4
tzdata     2023.3
wheel      0.40.0

Terlihat paket django versi 4.2.3 sudah terinstall. Selanjutnya adalah membuat project django.

  1. Kita akan membuat project django bernama config. Berikut codenya:
(djangoku-wI74BJPd) D:\\djangoku>django-admin startproject config .

Tanda ‘.’ digunakan untuk membuat folder config hanya satu level folder, jika tanpa titik maka folder akan dibuat dua level.

Setelah itu, cek struktur folder, maka struktur folder django menjadi sebagai berikut:

(djangoku-wI74BJPd) D:\djangoku>tree /f
Folder PATH listing
Volume serial number is 8CAE-FCC6
D:.
│   manage.py
│   Pipfile
│   Pipfile.lock
│
└───config
        asgi.py
        settings.py
        urls.py
        wsgi.py
        __init__.py

Mari kita lakukan migrasi dan menjalan server, code:

(djangoku-wI74BJPd) D:\djangoku>py manage.py migrate

output:
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK

(djangoku-wI74BJPd) D:\djangoku>python manage.py runserver

output:
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
July 24, 2023 - 02:13:14
Django version 4.2.3, using settings 'config.settings'
Starting development server at <http://127.0.0.1:8000/>
Quit the server with CTRL-BREAK.

Cek http://127.0.0.1:8000/ di browser, maka akan terlihat tampilan berikut:

Demikian berarti django berhasil diinstall dengan environment menggunakan library pipenv. Selanjutnya kita akan buat aplikasi pages dengan menampilkan Hello World secara sederhana, kemudian dideploy ke docker yang diinstall di localhost… Bersambung Part 2

Leave a Reply