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
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 .
After the image building has completed, you can see the image by running
docker images
2. Run the image
Run the bash terminal using the image
docker run -it --rm -v "$(pwd)"/src:/app/ app_tut bash
-
-itwill run the image in interactive tty mode -
--rmwill remove the container when the run is exited -
-v "$(pwd)/src:/app/will mount the./srcdirectory in current working directory to the/appinside the container -
my_appis the image name -
bashis 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
It will give a list of installed dependencies inside the container. Verify the Django installation.
Package Version
---------- -------
Django 3.1.5
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 .
To confirm the project creation, list the files in the current directory
ls -la
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
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
Top comments (0)