DEV Community

Cover image for Django development using Docker as host - Part 3: Create Django project
Anuj Sharma
Anuj Sharma

Posted on

Django development using Docker as host - Part 3: Create Django project

Till now we have completed with the Dockerfile and the required script files. In this step, we will create a Django project inside the container.

TOC

  1. Build docker image
  2. Run the image
  3. Create Django project in the container
  4. Install python dependencies

Let's start

1. Build docker image

First, we need to build the image using the Dockerfile. Execute the following command to build the image and add a tag my_app

docker build -t my_app .
Enter fullscreen mode Exit fullscreen mode

After the image building has completed, you can see the image by running

docker images
Enter fullscreen mode Exit fullscreen mode

2. Run the image

Run the bash terminal using the image

docker run -it --rm -v "$(pwd)"/src:/app/ app_tut bash 
Enter fullscreen mode Exit fullscreen mode
  • -it will run the image in interactive tty mode
  • --rm will remove the container when the run is exited
  • -v "$(pwd)/src:/app/ will mount the ./src directory in current working directory to the /app inside the container
  • my_app is the image name
  • bash is the command to execute inside the container.

3. Create Django project

First, verify the Django installation inside the container by running the following command inside the container bash session

pip list
Enter fullscreen mode Exit fullscreen mode

It will give a list of installed dependencies inside the container. Verify the Django installation.

Package    Version
---------- -------
Django     3.1.5
Enter fullscreen mode Exit fullscreen mode

As Django is installed, run the below command to create a Django project with name my_app in the current directory.

django-admin startproject my_app .
Enter fullscreen mode Exit fullscreen mode

To confirm the project creation, list the files in the current directory

ls -la
Enter fullscreen mode Exit fullscreen mode

You will see a directory my_app and manage.py file

-rwxr-xr-x 1 root root  662 Jan 16 17:54 manage.py
drwxr-xr-x 7 root root  224 Jan 16 17:54 my_app
Enter fullscreen mode Exit fullscreen mode

Did you noticed something?
You can see the my_app directory and the manage.py in the host src directory as well.
This is because we have mounted the src directory with the /app directory using -v parameter.

This is all for initializing our Django project. In the next step, we will use the docker-compose to run the Django application and mounted the volumes.

Type exit to exit from the docker image shell.

4. Install python dependencies

Since we will be using the MySQL database with the Django project, mysqlclient python dependency needs to be installed.
Add the following to the requirements.txt because its installation will require the build-essential library.

mysqlclient==2.0.3
Enter fullscreen mode Exit fullscreen mode

Top comments (0)