In this video I'm going to show you how to create a Docker container with a
Django rest framework application. So, before we get started let's just talk
through the dependencies that you're going to need for this project. As you
can see I'm using linux mint here, however you can use Windows, Mac or Linux
whatever one you choose, the steps will be practically the same except when
you're using Windows then you'll be using the command prompt instead of the
terminal window. Docker is available for all of these platforms so you can head
over to docker.com and download the latest version of docker for either Mac
or Windows. Once you have docker installed you're
going to want to install a tool called docker compose. I've provided links to all
of these in the notes of the video so you can just follow the link and then
follow the instructions on the docker website to install
docker compose. You're also going to need git because we're going to be
cloning an existing project from github. So once you have those things installed
we're good to go. Alright so I've added a link to a
project in the notes of this video called the BYOB profiles REST API
project. This project is an application that we create in my course
How to Build a REST API. Now you don't need to follow the course for this video
you can just go ahead and clone the code this is the finished code
that you would create in the course if you were to take the course and
basically in the course I teach how to build a REST API but I teach
using Vagrant so while we're building our API and testing it we're using
Vagrant. And this video basically explains how to convert that to a docker
file or how to create a docker container for the project so you can use
that instead of Vagrant. The reason why I chose Vagrant for the course is because
Vagrant has a bit of an easier learning curve. It's easier just to get installed
and up and running on a machine, and docker is a little bit more
complicated, however there are a lot of benefits from using
Docker. I like how you can just test using the actual docker container on your
local machine that you will be using in the live environment. So let's start by
cloning this code, I'm going to copy the link under clone or download and I'm
just going to head over to my terminal or if you're on a Windows machine you'll
be using your command prompt and then I have a location here called workspace
where I just clone all of my files to or my projects. I'm just going to
type git clone and then the name of the or the URL for the project and then once
it has downloaded or once it's cloned I'm going to switch to it using CD
command byob and I'm going to open it in my text editor Atom. You can
use Atom or you can use whichever text editor you prefer to use
So once the project loads the first thing that we want to do is we want to
create what's called a docker file and the docker file is a file that contains
a list of instructions that docker should follow when creating our image.
So docker has kind of two stages it has the build stage and the run stage. So on the
build stage you will build your container and it will actually create a
container image which you can then run to run your application. So during
the build stage what it does is it looks at the docker file and then it creates an
image and runs all of the commands in the docker file on the image so that the
image is prepared so that's what it does to build your image. Let's go ahead and
create a docker file by right-clicking the project name and click new file and
you want to type docker file with a capital D and there's no file extension
it's just simply a file called docker file.
Alright so once that is created let's go ahead and create our docker file, so the
docker file consists of a few different commands that
we're going to run to prepare our image. The first one is we're going to tell it
what image to base our docker image on. So one of the cool
things about docker is that you can layer the images on top of each other so
there's a whole marketplace of images available for free that you can use when
building your project and these images all contain, they're like base
images that are set up for specific types of application. So for example
we're going to be using a python base image because our application is a Python
image and it basically just saves us a whole bunch of time because we don't
need to go and create and install all the python dependencies we just simply
type from - all caps - and then the name of the image which is Python -
3 and this will base our image off of Python image so at this point in the
build phase we will already have all the dependencies we need for a standard
Python project set up. That's Python, pip, the basic Linux OS all stuff like that
Alright so the next thing we want to do is create an environment variable called
Python unbuffered. So let's type that, env and then in all caps Python unbuffered
and then we'll set that to 1. All this does is it sets the Python output so the
application output when you run it to unbuffered so it doesn't
buffer it in the STD input outputs it just sends it straight
out to the terminal and this just helps makes it a little bit easier when we're
interacting with the docker container. Alright next we want to create a
directory in our container for our source code so this is going to hold all
of the code for our application. So type run all caps and then mkdir /code. The run
command, all that does is it allows us to run a command as if we were running it
on the server while it's being set up. So we're just running the mkdir command to
create a new directory called /code. Next we're going to set our
workdir, so this is the working directory that we're going to use when
we're interacting with our docker container. So type workdir and then the director we just
created called code, so just like workdir /code. Next we're gonna copy all
of the files for our projects to our container so the container at this point
is literally just a Python 3 based container we've set an environment
variable and we've created a directory but the container doesn't actually
contain our source code so we're gonna want to copy our source code from our
local machine to the image that we're building. The way you do that is you use
the copy command. You type copy . /code
now the dot just says copy from our local project
files so all of these files here are going to be copied to our
/code directory on our container. Finally we're going to want to run pip install
to install the requirements for our project so our project comes with the
requirements.txt file it's a list of python requirements that we need for our
project. Alright so the way we do that is again we use the run command and we
just type run pip install -r says install for main requirements file and
then requirements.txt. The reason we can just type this requirements.txt
here's because we're already working in our /code directory so
the focus is on that and we've copied all of our source code here including
our requirements file to this location so since we've copied it there and we're
working on that directory and we can just type pip install requirements.txt
Alright so that's all we need for our docker file it's a very simple
docker file if you just save that. Next we're going to be creating our docker
compose config now docker compose is an optional tool that you can use with
docker just to make it easier to interact with docker when you're working
with it so basically it's really useful for development environments it
lets you describe multiple different docker containers and how they should
run and it also lets you do cool stuff like forward the ports and set
dependencies between different containers if your application requires
multiple containers. Let's go ahead and create a docker compose config by
right-clicking our project and typing docker - compose.yml hit enter on
that. Alright so in the docker compose the
top line we need to specify which version of the docker compose syntax
we're gonna be using. We're going to be using the docker compose 3 syntax so you
just type version: and then in quotes the version we want to use all
this is is a backward compatibility feature so if docker released docker
compose version 4 in the future it knows that it should run this based on the version 3
version of the syntax. Alright so now we can define our services in
our docker compose file. Type services: and the services we want to define are
web and under web we're going to tell it how to build our container or where the
location of our docker file is so you just type build: . that just says build
from our current directory, next we want to specify the command that we want to
run when we run our container. Then type command:
python src/profiles_project/manage.py runserver 0.0.0.0
: 8000 it's a bit long that command but it is quite simple all it says is use
Python to run the manage.py script under src/profiles_projects/
manage.py which is here and that's just the Django management script that
allows us to interact with our Python project and we're going to use
So we're going to call that and we're going to pass in the arguments
run server and then this location this IP address and what this is going to do is
it's just going to start the development server in our image. Okay so next we want
to map a volume to our container so type volumes: - .:/code
what this does is it maps a volume from our local location so the
location we're running docker compose from to /code on our project the
reason we do this is this allows us to have real-time access to the code within
our docker file. So it's basically like the synchronize folder in a Vagrant project
whenever we make a change to our source code here it will automatically
be mapped to our docker container that will be running so we can make changes
in real time without having to rebuild and run our container each time we make
a change. Finally we're going to map a port from our container to our local
machine now the way that we do that we just type ports and you can map as many
ports as you like but we just need to map port 8000 to port 8000 since we're
going to be running our development server on port
8000 we just need to map that to our host machine so when we access port
8000 on our host machine it will forward the connection to our
docker container so that we can actually access our development server okay so
now that we've done that we can save our file and the next step is to set up our
database for our project. Our project uses a simple sqlite
database and it creates that in the local container that we've run it on
so we need to run the migration command in order to migrate our project. The way
that we do that is
we use docker compose to
override this command here. So what we do is we type...let me just move
this so that you can see it away from my head type docker-compose I'll
just expand this a bit so you can see it clearly
there you go. Docker compose run and then the name of the service we
want to run our command on if you remember we created a service called
web here. I'm going to type docker compose run web and then the command that we want to
run so we want to run python src/profiles_project/manage.py
migrate hit enter and what this actually does is it goes ahead and it creates our
docker container for us and then runs the command that we just specified on it
so it automatically builds the container for us when we type that if
you want to manually build the container you can just type docker - compose build
and this will go and it will build that container for us. You can see that it's
following all the steps that we specified it actually lists all those
steps 1 of 6 so first one is start from the Python 3 container the next one is
set the environment variable and then it runs the command to create our directory
and then it sets our workdir to /code and then it copies our source
code and then it installs all the requirements so that's what it's doing now
it's installing all the other requirements into our docker container so we'll wait
for that to finish it looks like it's on the last stage now
there you go so it's built our image and now we can start our image by typing
docker - compose up. Like I said you don't need to manually build it if
your type docker compose up it will automatically build it for you before it
runs it. Ok so here you can see that our development server is running if we head
over to the browser and we typed localhost
- 8000 it will connect to our container if I show you this side-by-side move
this here you can see just behind my head here that every time you access the
server so if we type /api it outputs here that it's loading all the
files so this is the log output from our django server. Ok so that's how
you create a docker file for a Django rest framework project. Thank you so much
for watching this video I hope you found it useful and if you have any questions
or you run into any issues please feel free to leave questions in the
comments below or if you have any opinions or feedback or whatever about
docker versus vagrant or using docker for these types of projects then please
let us know I'd love to hear about it so yeah thank you so much for watching and
I'll see you next time.