Table of contents
Creating docker file
Hello DevOps enthusiasts! Today, we will be creating a docker file for django based app.
I have cloned the app from Shubham Londhe 's github and created a docker file to containerize the application.
Github Repo:
https://github.com/sowmya-bm/django-notes-app
Once we clone the above repo, we can understand the following things:
The file manage.py indicates that the app is a Python-based application, hence the container should have Python image
FROM python:3.9
Next, we need to copy everything related to the application from the current directory to the container
COPY . .
By checking out requiremnts.txt file, we understand that the application also uses django and sql hence we should install everything from requirements.txt
RUN pip install -r requirements.txt
This application runs on port 8000, hence that port is to be exposed
EXPOSE 8000
We need to run manage.py file
CMD ["python","manage.py","runserver","0.0.0.0:8000"]
Our docker file will be of the below format:
FROM python:3.9 COPY . . RUN pip install -r requirements.txt EXPOSE 8000 CMD ["python","manage.py","runserver","0.0.0.0:8000"]
Once the docker file is created, we need to build the file to create a docker image.
Hence webuild
thedocker
file in the current directory(represented by . in command) and tag(-t
) it as the 'latest
' with the image name(django-app)
docker build . -t django-app:latest
Running a docker image creates a docker container
Hence, we
run
thedocker
file in a detached mode(d
) and bind the ports(-p
) of the container with that of our system (8000:8000
) followed by the image name (django-app:latest
)docker run -d -p 8000:8000 django-app:latest
The status of the container can be checked with docker ps
command:
Pushing the image to the docker hub
The above image can be pushed to the docker hub by following the below steps:
Tag the image: First, we need to tag our image with our Docker Hub username followed by image name
docker tag django-app sowmyabm/django-app:latest
Log in to Docker Hub: Use the
docker login
command to log in to Docker Hub.docker login
Push the image: After logging in, push the image to Docker Hub using the
docker push
command. Specify the tagged image name you created in the previous step.docker image push sowmyabm/django-app:latest
Verify the upload: Once the push command completes successfully, you can go to the Docker Hub website and check your repository. The pushed image should be visible in your account.