Dockerizing an SQLite Django Project

Kento Murata
7 min readMay 16, 2020

Training to become a software engineer, there are all sorts of tools and coding languages that must be learned in order to create a desired project. Tons of time can be put into researching documentation for specific tools or coding languages. I have gained first hand experience in this process as I expanded my horizons in computer science, pushing myself to learn new tools and coding languages that are completely foreign to my coding practices. I have gotten to a stage where I feel comfortable with my computer science knowledge, and I feel that I can solve more problems I come across while coding on my own. I was able to expose myself to the new content, and repeatedly practiced the implementation of the new content I learned into an actual project.

Although I have made satisfying progress in my software engineering skills, I realized that I don’t have any experience dealing with the other factors that go into creating an app or website, such as managing my coding projects in a more efficient manner. That is why I have been learning Docker, which is a tool used to help programmers create, run, and deploy applications by the use of containers. I was given a task to store one of my existing projects into a Docker container, which automates the steps needed to run my project, such as installing project dependencies, making it easier for other developers to run my application. I used a Docker-compose.yml file to build my container, which will hold my Django project I made using an SQLite database. In this article, I would like to share my experiences creating a Docker-compose.yml file to build and run my Django project, which uses SQLite as its database.

Docker-compose.yml File: What is it?

Although I chose to use the Docker-compose.yml file to build my Docker container holding my Django project, I also have the option to create a Dockerfile which is another way to build a Docker container. I could even choose to use both a Docker-compose.yml file and a Dockerfile to build the container for more complex projects. But since my Django project is a relatively basic development website, I don’t want to do more work than required for a smaller project. As I started learning Docker, I started off learning how to implement the Dockerfile to projects first, providing a step-by-step todo list for the Docker container to follow. But I have never tried writing a Docker-compose.yml file which could ideally perform the same task, providing software developers the ability to build and run multiple containers at once. I could continue explaining the documentation of the Docker-compose.yml file to you, but I think providing an example of my Docker-compose.yml file I am using would be much more helpful to understand what exactly goes on inside the file.

My Docker-compose.yml File: Basic Fundamentals & File Structure

Based on my experiences, the best way of learning new coding tools and languages is to see examples of how other developers would go about writing their code. Docker can be overwhelming with the huge variety of docker specific commands and flags in order to properly manage your Docker containers. Since the Django project I decided to use utilizes the SQLite database, I don’t need to write a separate container in my Docker-compose.yml file since my Docker container will be able to access the database data directly from the root directory of my Django project. Ideally, I would later want to change the database I am using to a different database that supports production applications. This is because SQLite is used for development applications, meaning that SQLite won’t be able to retain all of the data stored into the database if you are attempting to run a production level application.

The picture shown below is a snippet of my Docker-compose.yml file which is capable of building and running my Django project.

Before discussing what each line does, it is important to note that Docker-compose.yml is a file that can run multiple Docker containers at once. Each Docker container inside the file represents the service dependencies required to run a given application. Applications that are dockerized will most likely require different service dependencies. An example of a service dependency would be a database you implemented into your application, such as Postgres. The more services your application requires to run properly, the more complex your Docker-compose.yml file will grow.

Each container inside of the file is separated into categories based on the indentation of the code. In my case, I only needed to create one container which would be responsible for building and running my Django project. Additional objects existing inside of a service’s scope are important environment variables specific to that service. The ‘django:’ object on line 3 represents the Docker container that will build and run my django project service. If I were to have any other services that my code depended on, I would create a new object outside of the ‘django:’ object’s scope. This would indicate that Docker Compose will build and run a Docker container for each service listed inside the Docker-compose.yml file when executed. Now that I’ve gone over the general format of the file, let’s take a look at what each line inside my file actually does.

[Line 1] version: “3.7”

Line 1 is where the header information of Docker Compose file belongs. This tells Docker Compose to use version 3.7, since Docker Compose is always being updated in order to fix bugs in the tool. The next line tells Docker Compose that what follows will be the service dependencies needed to build and run my Django project. This code looks like so:

[Line 2] services:

Since I my project only requires one service dependency, which is the Django framework, I only need to declare one service object:

The rest of the following lines are all properties belonging to my ‘django:’ service I declared in line 3.

Line 4 is setting the name of the Docker container that will be built and ran when Docker Compose is executed. Line 5 configures the desired directory that will be used to build and run the Docker application. Line 6 will tell Docker Compose to name the base image of the project. Before wrapping up my Docker-compose.yml file structure, I would like to share my experience dealing with an error I had while creating my Docker Compose file.

The next two lines, lines 11 and 12, are important pieces of code that was able to help me resolve my error I was having with my Docker Compose file.

A tty is a text input output environment, which is commonly referred to as a shell. The -it flag gives you an interactive tty to the docker container. This can be thought of as if you are inside the shell for the docker container. The stdout for the docker container is connected to your current shell and your input is connected to the docker container. Before implementing these two lines of code, every time I attempted to build and run my Docker Compose, my Django service container seemed to be stuck in some sort of loop. The command line would output that it is checking for any changes made to the container, and the only way I can close the container is by killing the terminal and then killing the container because it would still be running in the background.

The final lines that conclude my Docker Compose file includes:

Line 15 tells Compose that the following lines of code will be the host paths or named volumes I want my Docker container to mount with. within the “volumes:” scope, “- .:/var/www/myproject which would be the directory my Docker-compose.yml file can access outside the container.

Lastly, line 17 tells Compose that the following lines of code will include any ports that I want my Docker container to expose. Exposing a port allows me to access my Django project from that specified port.

Running Docker Compose

Now that all of Docker-compose.yml is setup, I can go ahead and execute the Docker compose command in the command line using:

$ docker-compose up

--

--

Kento Murata

Attending Make School Product College located in San Francisco, training to be a Backend Engineer.