Docker
Docker is a way to isolate and run entire applications. With Docker it doesn’t matter if you are using a Mac, Windows, or Linux computer anymore. The entire development environment is isolated: programming language, software packages, databases, and more.
With Docker we now longer have to mess around with virtual environments. We can faithfully reproduce a production environment locally. And Docker can be shared among team members so everyone is working on the same setup. Wins all around.
What’s the downside? Complexity. Docker is a complex beast under the hood.
Install Docker
- download desktop version here.
- run
$ docker --version
to check version
Hello, World
- run
$ docker run hello-world
to test function - A good command to inspect Docker is
$ docker info
- use
$ docker image ls
to inspect just the current image - use
$ docker container ls -la
to inspecting containers, either running, paused, or stopped
Images and Containers
A baking analogy we can use here is as follows:
- A
Dockerfile
is the recipe for a cake - An image is a snapshot of the recipe at a given time
- A
docker-compose.yml
says how to make the cake - And the container is the actual, baked cake
Django REST Framework
Django REST Framework works alongside the Django web framework to create web APIs. We cannot build a web API with only Django Rest Framework; it always must be added to a project after Django itself has been installed and configured.
how touse it
After use Django to build our website, we can add the REST Framework just like adding any other liberaries.
then, add it to the INSTALLED_APPS
config in our settings.py
file.
# library_project/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# 3rd party
'rest_framework', # new
# Local
'books.apps.BooksConfig',
]
Ultimately our API will expose a single endpoint that lists out all books in JSON. So we will need a new URL route, a new view, and a new serializer file (more on this shortly).
- first create a new api app.
# Local 'books.apps.BooksConfig', 'api.apps.ApiConfig', # new
- Adding an API endpoint is just like configuring a traditional Django app’s routes.
-
First at the project-level we need to include the api app and configure its URL route, which will be api/.
# library_project/urls.py from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('books.urls')), path('api/', include('api.urls')), # new ]
-
Then create a
urls.py
file within the api app, And update it as follows:# api/urls.py from django.urls import path from .views import BookAPIView urlpatterns = [ path('', BookAPIView.as_view()), ]
-
Next up is our
views.py
file which relies on Django REST Framework’s built-in generic class views.# api/views.py from rest_framework import generics from books.models import Book from .serializers import BookSerializer class BookAPIView(generics.ListAPIView): queryset = Book.objects.all() serializer_class = BookSerializer
-
A serializer translates data into a format that is easy to consume over the internet, typically JSON, and is displayed at an API endpoint. Make a
serializers.py
file within our api app then update it as follows:# api/serializers.py from rest_framework import serializers from books.models import Book class BookSerializer(serializers.ModelSerializer): class Meta: model = Book fields = ('title', 'subtitle', 'author', 'isbn')
-