---
# System prepended metadata

title: Dockerized DRF App

---



# 0 get python on Windows
1. Download the installer from here: https://www.python.org/downloads/

# 1. getting pip on Windows
1. save this file: https://bootstrap.pypa.io/get-pip.py
2. run `python get-pip.py` on a powershell window where you saved the downloaded file

# 2. Install Django and Django Rest Framework
1. `pip install Django`
2. `pip install djangorestframework`

# 3. Create a `requirements.txt` file
```
Django==4.2.3
djangorestframework==3.14.0
pyscopg2
```

# 4. Create a `Dockerfile`
```
FROM python:3.11.4-slim
ENV PYTHONUNBUFFERED 1
WORKDIR /tutorial
COPY ./requirements.txt .
RUN apt-get update \
    && apt-get -y install libpq-dev gcc
RUN pip install -r requirements.txt
COPY . .
```

# 5. within VS Code Terminal
```
PS C:\Users\...\webdevt\tutorial> python -m django startproject tutorial .
PS C:\Users\...\webdevt\tutorial> python -m django startapp quickstart
```

# 6. Create `docker-compose.yml`
```
version: "3"
services:
  api:
    build: .
    ports:
      - "8000:8000"
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/tutorial
    depends_on:
      - db
  db:
    image: postgres:13-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - "POSTGRES_HOST_AUTH_METHOD=trust"

volumes:
  postgres_data:
```

# 7. within VS Code Terminal
```
PS C:\Users\...\webdevt\tutorial> docker-compose up -d 
```

# 8. Code changes
`quickstart/serializers.py` (add new file)

```
from django.contrib.auth.models import User, Group
from rest_framework import serializers


class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ['url', 'username', 'email', 'groups']


class GroupSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Group
        fields = ['url', 'name']
```

`quickstart/views.py` (modify file)

```
from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from rest_framework import permissions
from quickstart.serializers import UserSerializer, GroupSerializer


class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer
    permission_classes = [permissions.IsAuthenticated]


class GroupViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows groups to be viewed or edited.
    """
    queryset = Group.objects.all()
    serializer_class = GroupSerializer
    permission_classes = [permissions.IsAuthenticated]
```
    
`tutorial/urls.py` (modify file)

```
from django.urls import include, path
from rest_framework import routers
from quickstart import views

router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
    path('', include(router.urls)),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
```

`tutorial/settings.py` (add at the bottom)

```
REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 10
}
```

`tutorial/settings.py` (modify INSTALLED_APPS)

```
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'quickstart',
]
```

# 9. Create superuser (inside VS Code Terminal)
`docker-compose exec -ti api bash`
`python manage.py migrate`
`python manage.py createsuperuser --email admin@example.com --username admin`

**set the password to "password123"**