Dockerfile! (Dockerize Python Application)

Dockerfile! (Dockerize Python Application)

·

3 min read

image

🌟How to build the docker file to a running Container?

To convert a Dockerfile into a Docker container, you need to follow these steps:

  • Create Docker file

  • Build the Docker Image

  • Verify the image

  • Run the container from that image

🌟What is a Docker file?

  • A Dockerfile is a text file that contains a series of commands or instructions.

  • These instructions are executed in the order in which they are written.

  • Execution of these instructions takes place on a base image.

  • You will use a Dockerfile to create your own custom Docker image

🌟How to write Dockerfile?

Let's go through the step-by-step process of writing a Dockerfile with an example:

Step 1: Choose a Base Image Let's say we want to build a Docker image for a simple Python application. We'll choose the official Python base image as our starting point.

# Use a base image with Python pre-installed
FROM python:3.9-slim

Step 2: Set the Working Directory Specify the working directory inside the container where our application code will be copied and executed.

# Set the working directory inside the container
WORKDIR /app

Step 3: Copy Files Copy the application files from the host machine to the container's working directory.

# Copy the application files to container app directory 
COPY . /app

Step 4: Install Dependencies Since our Django application has dependencies defined in the requirements.txt file, we need to install them inside the container.

# Install all the dependencies written in requirement.txt
RUN install -r requirements.txt

In the requirement.txt file, what dependencies do you want to install according to your project?

asgiref==3.2.3
Django==3.0.3
django-cors-headers==3.2.1
djangorestframework==3.11.0
pytz==2019.3
sqlparse==0.3.0

Step 5: Expose Ports Specify which ports should be published when running a container from the image. In this case, we'll expose port 8001, which is the port our Django application will be listening on.

# Expose port 8001 for the application
EXPOSE 8001

Step 6: Define CMD the Startup Command Specify the command that should be executed when a container is launched from the image. In this case, we'll run our Django application using the command given below.

# Set the command to run when the container starts
CMD ["python", "manage.py", "runserver", "0.0.0.0:8001"]

🌟How to build Docker image?

Build the Docker Image Open a terminal or command prompt, navigate to the directory containing the Dockerfile, and run the following command to build the Docker image:

docker build -t my-djando-app .

🌟How to verify the Docker image?

Every Docker image has a unique identifier called the Image ID. You can check the ID of a locally available image using the command. Look for the image name and corresponding ID in the output.

docker images

🌟How to run Docker Container?

Run the Docker Container Once the Docker image is built and verify the image. You can create and run a container from it using the following command:

docker run -d -p 8001:8001 --name django-app-cont my-django-app

In the above command '-d' daemon process or background process, '-p' or '--publish' it maps port 8001 of the container to port 8001 of the host machine, '--name' gives the name to the container you create.

You have to specify inbound and outbound rules before accessing your application on the web.

Now you have a Dockerfile that builds a Docker image for a Python Django application. Running the container in daemon mode will start your application on port 8001, and you can access it from your ec2 instance machine by visiting ip-address-ec2-instance:8001 .

Docker Commands Comming soon on Linkedin, the link is given below.

THANKS!!

\...................................................................................................................................................

The above information is up to my understanding. Suggestions are always welcome.

#docker #docker container #docker hub #DevOps #TrainWithShubham

#90daysofdevopsc #happylearning

Shubham Londhe Sir

Follow for many such contents:

LinkedIn: linkedin.com/in/dushyant-kumar-dk

Blog: dushyantkumark.hashnode.dev

Â