Django App from Scratch Using Docker with Debian Base Image

Edit

Author: Meet Rajesh Gor

#docker #web-development #django

Pull a Fresh Debian Image

Create a docker container from a Debian image, the following command can be used to pull a debain 11-slim image and create a container of it, also enter into the container in a interactive environment -it mode.


docker run -v $(pwd):/var/www --rm -it -p 8001:80 debian:11-slim

Create a Django App from Shell script

Now, since we are inside a Debian container, we can enter a few commands, you can refer to the Mark Gibney's GitHub repository for the script.


apt update

apt install wget

wget https://raw.githubusercontent.com/no-gravity/web_app_from_scratch/main/django.sh

Also, if you want to do a few adjustments, you can install an editor, get used to vim or use nano.


apt install vim 

OR

apt install nano

chmod +x django.sh
bash django.sh

I also have a few adjustment of the original script, that accepts a project name and creates a django project based on the positional parameter given to it. You can get it from the quick-setup-script repository or directly the script.

To use the above file, you need to execute the command as :


chmod +x django_docker.sh
bash django_docker.sh <project_name>

This will generate the project in the /var/www/ folder with the name of the project. The script will prompt you with a few things for setting up at some iterations like basic application setup , static file configuration, basic tempalte setup and the user authentication setup.

Copy the contents from the docker container

You can copy the contents of the folder into your local machine by entering the cp command in docker.


docker cp <container_id>:/var/www/<project_name> /path/in_local_machine/

This will copy the project in the docker container into the local machine for you to extend and tweak it as per your needs.

That's a basic Django Project Setup by using Docker with a Debian Image.

<a class='prev' href='/vim-get-visual-text'>

    <svg width="50px" height="50px" viewbox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M13.5 8.25L9.75 12L13.5 15.75" stroke="var(--prevnext-color-angle)" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"> </path>
    </svg>
    <div class='prevnext-text'>
        <p class='prevnext-subtitle'>prev</p>
        <p class='prevnext-title'>Vim: Get the Text from Visual Selection</p>
    </div>
</a>

<a class='next' href='/django-form-load-frontmatter'>

    <div class='prevnext-text'>
        <p class='prevnext-subtitle'>next</p>
        <p class='prevnext-title'>Django Blog DevLog: Load Frontmatter data into Template/Model Form Fields</p>
    </div>
    <svg width="50px" height="50px" viewbox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M10.5 15.75L14.25 12L10.5 8.25" stroke="var(--prevnext-color-angle)" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
    </svg>
</a>