Docker-file

Docker-file

·

2 min read

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:

  1. The file manage.py indicates that the app is a Python-based application, hence the container should have Python image

     FROM python:3.9
    
  2. Next, we need to copy everything related to the application from the current directory to the container

     COPY . .
    
  3. 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
    
  4. This application runs on port 8000, hence that port is to be exposed

     EXPOSE 8000
    
  5. We need to run manage.py file

    1.    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"]
      
  6. Once the docker file is created, we need to build the file to create a docker image.
    Hence we build the docker 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
    
  7. Running a docker image creates a docker container

    Hence, we run the docker 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:

  1. 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
    
  2. Log in to Docker Hub: Use the docker login command to log in to Docker Hub.

     docker login
    
  3. 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
    
  4. 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.