Docker Project Django and PostgreSQL - Zacham17/my-tech-journal GitHub Wiki
Summary
For this project, I created docker containers that run PostgreSQL and Django. This page is to serve as a build document, following the exacts steps that I took to build this projects and get it running properly.
What is Django
Django is a python-based web framework that allows for the creation of websites in a secure fashion. Django allows for administrative management and permission setting capabilities.
What is PostgreSQL
PostgreSQL is a database. It is a back-end database and in this lab, it is used as the database for Django
Prerequisites
- Have python installed
- Have docker installed
- Have docker-compose installed
Project Steps
Creating the Project Directory and Beginning Files
-
Create a directory for the project:
mkdir ./Django-PostgreSQL/
-
Enter the newly created project directory:
cd Django-PostgreSQL
-
Create and edit a file called Dockerfile:
vi Dockerfile
Use the configuration provided HERE for Dockerfile
- Create and edit a file called requrements.txt:
vi requirements.txt
Use the configuration provided HERE for requirements.txt
- Create and edit a file called docker-compose.yml:
vi docker.compose-yml
Use the configuration provided HERE for docker-compose.yml
Creating the Django Project Container
- Create a Django project using the command
docker-compose run web django-admin startproject Django-PostgreSQL .
After running this command there should be more files and a new directory in your root project directory
- There should be a folder inside the main project directory called Django-PostgreSQL. Enter that directory using
cd Django-PostgreSQL
. - Edit the settings.py file, using
vi settings.py
- Find the databases section and fill it with the following information:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
The full settings.py file is located HERE
-
Check for file changes using the command
docker-compose run web python manage.py makemigrations
. You should see "no changes detected" as an output. -
Type the command
docker-compose run web python manage.py makemigrations
to apply changes. -
Navigate to the settings.py file once more and find the "ALLOWED_HOSTS" variable. Inside the brackets, enter the IP address of the host system. It should look similar to the example below
ALLOWED_HOSTS = ['10.0.5.12']
Running the Docker Project
- Navigate back to the root directory:
cd ..
- From the root project directory run the command,
docker-compose up
to start the docker image. To stop the running container, input CTRL-C
Note: An alternative command to use here is docker-compose start
. Using start lets you freely use the host system while the container is running. To stop the running docker containers, use the command docker-compose stop
- While the docker containers for Django and Postgres are running, go to a web browser on a remote system and enter the ip address of the host system which the docker containers are on, along with the port the Django container is listening on to see the default Django web page. You can double check the port using the command
docker ps
-
Finally, back on the host system, add an admin user for Django using the command
docker exec -it CONTAINER_ID python manage.py createsuperuser
, while the docker containers are running, and replace CONTAINER_ID with the container ID that is displayed bydocker ps
. You will be prompted to enter credentials. -
Go back to the browser on the remote system and add /admin to the end of the URL.
Ex. https://10.0.5.12:8000/admin
You should now be on the admin login.
- Login using the credentials you entered in step 4 of this section. You should see a users and groups manager if done successfully.
CONGRATULATIONS! You have successfully containerized Django and PostgreSQL
Adding a Webpage for Django
- Make sure your docker containers are not running:
docker-compose stop
- From the project root directory, run the command
python manage.py startapp hello_world
to create a Django app called hello_world This creates a directory called "hello_world", which contains several files - Edit the settings.py file once and navigate to the INSTALLED_APPS list. Add 'hello_world' to the list. It should look similar to this:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'hello_world',
]
- Navigate to the hello_world directory
- Edit the veiws.py file so the file looks like this:
from django.shortcuts import render
# Create your views here.
def hello_world(request):
return render(request, 'hello_world.html', {})
HERE is a link to the views.py file
-
Create a templates directory within the hello_world directory
-
In the templates directory, create a file called hello_world.html and edit it to contain HTML code for a webpage. An example HTML file can be found HERE
-
Navigate back to the Django project directory within the root project directory. It should be Django_PostgreSQL/Django_PostgreSQL/
-
Edit the urls.py file to include URL configuration for the hello_world app. The file should look like this:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('hello_world.urls')),
]
HERE is a link to the urls.py file
- Navigate back to the root project directory
- Start the docker containers:
docker-compose start
- On the remote system browse to the IP address of the host system, using the port that Django is listening on. You should now see your newly created web page instead of the default Django page.
If you've made it here, then CONGRATULATIONS!!!! You have successfully completed the project!!