0% found this document useful (0 votes)
6 views4 pages

Build a Flask App with Dockerfile

This document outlines an experiment to create a Docker image for a Python Flask web application using a Dockerfile. It details the steps to set up the application, write the Dockerfile, build the image, and run the container, emphasizing the use of various Dockerfile instructions. The conclusion highlights the benefits of using Docker for consistent application deployment.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Build a Flask App with Dockerfile

This document outlines an experiment to create a Docker image for a Python Flask web application using a Dockerfile. It details the steps to set up the application, write the Dockerfile, build the image, and run the container, emphasizing the use of various Dockerfile instructions. The conclusion highlights the benefits of using Docker for consistent application deployment.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Roll No:160122733163 Week-6 Date:10/10/2025

Exp-7

Aim:
To learn Docker file instructions, build an image for a sample web application using Docker
file.
Description:
A Dockerfile is a text document containing a set of instructions that Docker uses to build an
image [Link] instruction in the Dockerfile creates a new layer in the image.
This experiment focuses on creating a simple Python Flask web application, writing a
Dockerfile for it, building an image, and running the container to serve a webpage.
Concept:
• FROM: Defines the base image (e.g., Python).
• WORKDIR: Sets the working directory inside the container.
• COPY: Copies application files from the local system to the container.
• RUN: Executes commands like installing dependencies.
• EXPOSE: Declares the port the container listens on.
• CMD: Specifies the command to run when the container starts.
Step-by-Step Procedure:
1. Create the project folder
Open PowerShell and create a new folder for the project:
mkdir flask-docker-app
cd flask-docker-app
[Link] the application file:
Inside this folder, create a file named [Link] and add the following code: This simple Flask
app displays “Hello from Docker!” when accessed through a web browser.
[Link] code
from flask import Flask
app = Flask(__name__)
@[Link]('/')
def home():
return "Hello from Docker!"
if __name__ == '__main__':
[Link](host='[Link]', port=5000)
3. Create a requirements file
Create a file named [Link]:
flask
[Link] the Dockerfile

Page No. Signature of the Faculty………………………...


Roll No:160122733163 Week-6 Date:10/10/2025

Create a file named Dockerfile (without any file extension) and add the following content: This
Dockerfile defines how to build an image containing the Flask application.
Dockerfile code
# Step 1: Use an official Python image as the base
FROM python:3.9-slim
# Step 2: Set the working directory in the container
WORKDIR /app
# Step 3: Copy the application files into the container
COPY . /app
# Step 4: Install dependencies from [Link]
RUN pip install -r [Link]
# Step 5: Expose the port Flask will run on
EXPOSE 5000
# Step 6: Command to run the application
CMD ["python", "[Link]"]
5. Build the Docker image
In the same folder as your Dockerfile, run: This command builds the Docker image named
flask-webapp using the instructions from the Dockerfile.
The . indicates the current directory as the build context.
docker build -t flask-webapp .

Page No. Signature of the Faculty………………………...


Roll No:160122733163 Week-6 Date:10/10/2025

[Link] the image


Check whether the image was created successfully: Lists all images available locally, including
the newly built flask-webapp image.
docker images

7. Run the container


Now run a container using the created image:
The -d flag runs the container in the background (detached mode).
The -p 5000:5000 maps the container’s port 5000 to the host’s port 5000.
docker run -d -p 5000:5000 flask-webapp

8. Test the web application: The application is now successfully running inside a Docker
container.
Open your browser and visit:
[Link]

When we open the docker desktop we can see the webapp container is running

Page No. Signature of the Faculty………………………...


Roll No:160122733163 Week-6 Date:10/10/2025

9. Stop the container (after testing): The first command lists all running containers, and the
second stops the one with the given container ID.

To stop the running container:


docker ps
docker stop <container_id>

Conclusion:
This experiment demonstrates how a Dockerfile can be used to automate the process of
building a Docker image for a web [Link] using Dockerfile instructions like FROM,
WORKDIR, COPY, RUN, EXPOSE, and CMD, an image for a Python Flask application was
successfully created and executed inside a [Link] approach simplifies application
deployment and ensures consistent execution across different systems.

Page No. Signature of the Faculty………………………...

You might also like