Quickly setup a new django env with docker
Dockerized-django-app-with-vscode
- add .gitignore from Github
- Create the app directory for the django project
mkdir app
- Run the singular Docker command to install django and create the project
docker run -v ${PWD}/app:/app -w /app python:3.9-alpine sh -c "pip install Django==3.2 && django-admin startproject app ."
After this all project files under app should appear
In next section we will create the docker config to easily run the Django webserver.
Before you continue, note that this process will create the following files in your project, replacing them if they already exist:
- requirements.txt
- Dockerfile
- docker-compose.yml
- docker-compose.debug.yaml
- .vscode/launch.json
- .vscode/tasks.json
Use the docker extension to add the settings files
- Open the VScode command pallet
macOS: CMD + SHIFT + P
Windows: CTRL + SHIFT + P
-
Choose 'Docker: Add Docker Files to Workspace.'
-
On the Select Application Platform prompt, locate Python: Django and select it:
-
On the Choose the app’s entry point prompt, select app/manage.py and select it:
-
Leave the port as 8000 and answer Yes to create the 'docker-compose.yml' file
-
Change the docker files that were created in MAC to LF at the bottom in VS code.
-
Update the requirments.txt to be more specific
django>=3.2,<3.3
gunicorn>=20.0.4,<20.1
- Update the Dockerfile like so
- Changed base image to python:3.9-alpine3.13 because it is more lightweight.
- Changed the COPY . /app command to COPY /app /app so only our Django app is copied into the Docker image.
- Modified the gunicorn command to use the app.wsgi file for running our app.
- Build the Django webserver and run
docker-compose build
docker-compose up
Next open the compose file for django debugging.
- Locate the app/manage.py line and remove the app/ to change it to manage.py.
We make this change because our container is already working from the app/ directory, so we can call the manage.py file directly.
- Next we need to make some changes on the .vscode folder that was created earlier.
Next open up .vscode/launch.json, locate the line "localRoot": "${workspaceFolder}", and replace it with "localRoot": "${workspaceFolder}/app",.
Again, this change is because our Django project is going to be stored within /app instead of the root project.
- Open the tasks.json file and make small alteration
Open up .vscode/tasks.json and change "file": "app/manage.py" to "file": "manage.py".
- Create views.py file alongside urls.py
from django.http import HttpResponse
def index(request):
return HttpResponse('Hello World!')
- Update the urls.py
from django.contrib import admin
from django.urls import path
from app.views import index
urlpatterns = [
path('admin/', admin.site.urls),
path('', index),
]
While the backend is running via docker.
Select Run start debugging in VScode.
You can then access the debugging varialbes in the debug console in VScode.
to check this working run the server and navigate to
http://localhost:8000/api/hello-world/items/
alternatively you can use CURl
command
The Django commands (migrate
, create_admin
and create_groups
) can be run in the container directly:
docker-compose exec webapp python manage.py <COMMAND>
This way you can create an admin user to help manage the celery tasks for example
To get the API to work you need to migrate
docker-compose -f docker-compose.yml exec webapp python manage.py migrate