Dockerizing the django app step-by-step procedure.
Launch EC2 instance. >> Ubuntu >> t2.micro >> Access the instance using ssh.
- Update the Ubuntu OS:
sudo apt update
- Create project directory
mkdir project
cd project
- Clone the git repository:
git clone https://github.com/shreys7/django-todo.git
cd django-todo/
- Install pip3
sudo apt install python3-pip
- Install django
pip3 install django
- Every application has a database associated with it. If we need to create a table required for the application we use manage.py migrate command.:
python3 manage.py migrate
\>> The command `python3 manage.py migrate` is used to apply the changes made in the models (the Python classes that define the structure of the database tables) to the actual database. This command is typically used with Django, a popular web framework for Python.
The command works by reading the migration files (the Python files that describe the changes made in the models) and executing the SQL statements that correspond to those changes. The migration files are generated by another command, `python3 manage.py makemigrations`, which should be run whenever you modify the models.
The command `python3 manage.py migrate` can also take some optional arguments, such as the name of the app or the specific migration file you want to apply. For example, if you have an app called `blog` and you want to apply only the migration file `0002_post_content.py`, you can run:
python3 manage.py migrate blog 0002_post_content
- Runserver
python3 manage.py runserver
-
* If any timezone issue then (optional)
vim todoApp/settings.py
Change Time zone >> TIME_ZONE = 'Asia/Dhaka'
* Run server on 0.0.0.0:8000
python3 manage.py runserver 0.0.0.0:8000
* Go to AWS console select instance. GO to security tab
* Click on security group >> edit inbond rules >> add rule >> Custom TCP >> port > 8000 >> 0.0.0.0/0 >>Save rule.
* Copy public IP of the aws ec2 instance and paste in the browser with port number > IP:8000
* Go to settings.py and do changes
vim todoApp/settings.py
Add in >>> ALLOWED_HOSTS = [''] >> Save and exit
* Run server on 0.0.0.0:8000
python3 manage.py runserver 0.0.0.0:8000
* To run the app in background use command:
nohup python3 manage.py runserver 0.0.0.0:8000 &
* To list open network connections on a specific port, in this case, port 8000. It shows which processes or programs are using that port.
lsof -i:8000
* Kill the process:
kill -9 3177
### Use Docker:
* Install docker
sudo apt install docker.io -y
* Write docker file:
For more information related to Dockerfile check old blog >> https://hashnode.com/edit/clo9twllq000009lcenxv4dhi
vim Dockerfile
# Dockerfile for a Django Todo App
# Author: Vinayak
# Use the official Python 3 image
FROM python:3
# Install the Django framework
RUN pip install django
# Copy the current directory into the container
COPY . .
# Run Django migrations
RUN python manage.py migrate
# Start the Django development server and make it accessible from outside the container
CMD [ "python", "manage.py", "runserver", "0.0.0.0:8000" ]
Build the image:
docker build . -t yesicanvinayak/django-todo-app
-
docker images
- Run the container
-
-
docker run -d -p 8000:8000 yesicanvinayak/django-todo-app
- Copy public IP of the aws ec2 instance and paste in the browser with port number > IP:8000
Your application is successfully dockerised and the application is running on docker-container.
If you find the blog helpful, please do like it and share it with your friends. If you have any doubts or questions related to the topics covered in the blog, feel free to ask them in the comments section. I'll be more than happy to help! ๐๐๐ฅ