2023-12-04
Goal
Finish Chapter 2 of Django For Professionals and learn about Docker.
Notes
Chapter 2
Last time I left off on how to create the Dockerfile, the file that creates the Docker Image which essentially creates a Virtual Environment for the entire Tech Stack of a project.
Next I learned how to implement a dockerignore, which is similar to a gitignore file. The .dockerignore
file therefore looks like the following:
.venv
.git
.gitignore
Then we can finally build the custom docker image using the
docker build .
command in the same directory as the Dockerfile. This constructs the initial Docker Image. Now we can build the actual Docker Container.
First we need to create the docker-compose.yml file which is placed in the same location as the Dockerfile. The book states that the most recent version is 3.9 but ChatGPT says the most recent version is 2.23.3. I was able to check my version with
docker-compose --version
But ChatGPT said that this wasn't what I wanted for the docker-compose.yml
file and that the version stated in the book is forwards compatible so we'll go with that. Either way the docker-compose.yml
file is here:
version: '3.9'
services:
web:
build: .
ports:
- "8000:8000"
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/django_for_professionals
Then I could run the
docker-compose up
command to get things up and running. This actually resulted in a failed image (the website wouldn't actually render our page) even though it said that it was up and running. I tried activating the Virtual Environment with
.venv/bin/activate
and running the server with
python manage.py runserver
To check if the Django App was actually running and it worked fine. So it was something to do with how I actually set up Docker. I decided to take a little bit of time to troubleshoot why that is. Even though it's a toy example, I think it's worth it to spend at least some time trying to debug it here. After going back and forth with ChatGPT, I found out that it worked fine when I accessed the app via
http://localhost:8000/
instead of the link it provided me
(http://0.0.0.0:8000/
)
According to ChatGippity, this is because
The address
0.0.0.0:8000
is used within Docker to indicate that the service (in your case, the Django app) is listening on all network interfaces inside the container. It is not an address you can use to access the service from your browser.
So anyways it looks like Docker was correctly set up, I just wasn't accessing it correctly. Yay!
Also as a side note the author also mentioned a small section about Docker Root Settings, where the default user is set to have root permissions. This might need to be adjusted before launching the Production version of the HitHub App.
Docker and PostgreSQL
The next chapter teaches how to integrate Docker and PostgreSQL. I'll read through the next chapter (chapter 3) and try to integrate that into this toy example before proceeding.
Results
- Successfully implemented Docker and completed chapter 2 of Django For Professionals
Next Time
- Complete Chapter 3 of Django For Professionals to integrate PostgreSQL into Docker.