In this tutorial, I will show you how to set up a Docker environment for your Django project when still in the development phase. Although I am using Ubuntu 18.04, the steps remain the same for whatever Linux distro you use with exception of Docker and docker-compose installation.
In any typical software development project, we usually have a process of development usually starting from the developer premise to VCS to production. We will try to stick to the rules so that you understand where each DevOps tools come in.
DevOps is all about eliminating the yesteryears phrase; “But it works on my laptop!”. Docker containerization creates the production environment for your application so that it can be deployed anywhere, Develop once, deploy anywhere!.
Install Docker Engine
You need a Docker runtime engine installed on your server/Desktop. Our Docker installation guides should be of great help.
I have set up a GitHub repo for this project called django-docker-dev-app. Feel free to fork it or clone/download it.
First, create the folder to hold your project. My folder workspace is called django-docker-dev-app. CD into the folder then opens it in your IDE.
In a containerized environment, all applications live in a container. Containers themselves are made up of several images. You can create your own image or use other images from Docker Hub. A Dockerfile is Docker text document Docker reads to automatically create/build an image. Our Dockerfile will list all the dependencies required by our project.
Create a file named Dockerfile.
$ nano Dockerfile
In the file type the following:
#base image FROM python:3.7-alpine #maintainer LABEL Author="CodeGenes" # The enviroment variable ensures that the python output is set straight # to the terminal with out buffering it first ENV PYTHONBUFFERED 1 #copy requirements file to image COPY ./requirements.txt /requirements.txt #let pip install required packages RUN pip install -r requirements.txt #directory to store app source code RUN mkdir /app #switch to /app directory so that everything runs from here WORKDIR /app #copy the app code to image working directory COPY ./app /app #create user to run the app(it is not recommended to use root) #we create user called user with -D -> meaning no need for home directory RUN adduser -D user #switch from root to user to run our app USER user
Create the requirements.txt file and add your projects requirements.
Create the app folder that will hold the source code. Now we are ready to build our image!
$ mkdir app
Open a terminal in your project and type the following command to build our image:
$ docker build .
At the end tells Docker to check for Dockerfile in the same directory, if you had the file in a different location, we would have written the path to that file. Docker will then call the necessarily required images from the Docker Hub to create your image.
Using Docker Compose
From Docker docs; Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.
Because our project will require a lot of services in the future, it is better to create a multi-container environment for it. For example, we might require a container to host our front end, another to host a database and another to host a backend when we switch to a microservice architecture. We will create a docker-compose file that will define the various services and containers our project will require.
Create a file named docker-compose.yml
version: "3" services: ddda: build: context: . ports: - "8000:8000" volumes: - ./app:/app command: > sh -c "python manage.py runserver 0.0.0.0:8000"
Note the YAML file format!!! We have one service called ddda (Django Docker Dev App).
We are now ready to build our service:
$ docker-compose build
After a successful build you see: Successfully tagged djangodockerdevapp_ddda:latest
It means that our image is called djangodockerdevapp_ddda:latest and our service is called ddda
Now note that we have not yet created our Django project! If you followed the above procedure successfully then Congrats, you have set Docker environment for your project!. Next, start your development by doing the following:
Create the Django project:
$ docker-compose run ddda sh -c “django-admin.py startproject app .”
To run your container:
$ docker-compose up
Then visit http://localhost:8000/
$ docker-compose exec ddda python manage.py migrate
From now onwards you will run Django commands like the above command, adding your command after the service name ddda.
That’s it for now. I know it I simple and there are a lot of features we have not covered like a database but this is a start!
The project is available on GitHub here.
More on Docker: