Docker Advance

*Shreyash Bhise | Aspiring Mern Stack Developer and DevOps enthusiast,
- Docker file without multi stage:
Here's an example of a Dockerfile for a simple Flask web application that includes a templates directory and a requirements.txt file:
Create a directory for your project and navigate to it in the terminal.
Create a file named
app.pywith the following content:
python
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def hello():
return render_template('index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
- Create a directory named
templatesand create anindex.htmlfile inside it with the following content:
html
<!DOCTYPE html>
<html>
<head>
<title>Simple Flask App</title>
</head>
<body>
<h1>Hello from Flask!</h1>
</body>
</html>
- Create a file named
requirements.txtwith the following content:
makefile
Flask==2.0.1
Create a file named
Dockerfilein the same directory as yourapp.py,templates, andrequirements.txtfiles.Add the following content to the
Dockerfile:
Dockerfile
# Use the official Python image as the base
FROM python:3.9-slim
# Set the working directory within the container
WORKDIR /app
# Copy the application files to the container
COPY app.py /app/
COPY templates /app/templates/
COPY requirements.txt /app/
# Install Flask and other dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Specify the command to run when the container starts
CMD ["python", "app.py"]
Build and run the Docker image:
bash
docker build -t simple-flask-app . docker run -p 8080:8080 simple-flask-appOpen a web browser and navigate to
http://localhost:8080to see the rendered template with the "Hello from Flask!" message.
This Dockerfile sets up a Flask application, installs the required dependencies from the requirements.txt file, and runs the application. It demonstrates how to include templates and a requirements.txt file in your Flask app's Docker image.
- Dockerfile with multi-stage:
Here's an example of a multi-stage Dockerfile for a simple Flask web application that includes a templates directory and a requirements.txt file:
Create a directory for your project and navigate to it in the terminal.
Create a file named
app.pywith the following content:
python
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def hello():
return render_template('index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
- Create a directory named
templatesand create anindex.htmlfile inside it with the following content:
html
<!DOCTYPE html>
<html>
<head>
<title>Simple Flask App</title>
</head>
<body>
<h1>Hello from Flask!</h1>
</body>
</html>
- Create a file named
requirements.txtwith the following content:
makefile
Flask==2.0.1
Create a file named
Dockerfilein the same directory as yourapp.py,templates, andrequirements.txtfiles.Add the following content to the
Dockerfile:
Dockerfile
# Stage 1: Build the application
FROM python:3.9-slim AS build
WORKDIR /app
COPY app.py /app/
COPY templates /app/templates/
COPY requirements.txt /app/
RUN pip install --no-cache-dir -r requirements.txt
# Stage 2: Create a lightweight runtime image
FROM python:3.9-slim
WORKDIR /app
COPY --from=build /app /app
CMD ["python", "app.py"]
Build and run the Docker image:
bash
docker build -t simple-flask-app . docker run -p 8080:8080 simple-flask-appOpen a web browser and navigate to
http://localhost:8080to see the rendered template with the "Hello from Flask!" message.
This multi-stage Dockerfile builds the application in the first stage and then creates a lightweight runtime image in the second stage, reducing the size of the final image. It demonstrates how to include templates and a requirements.txt file in a multi-stage Flask app Docker image.




