Skip to main content

Command Palette

Search for a command to run...

Docker Advance

Published
3 min read
Docker Advance
S

*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:

  1. Create a directory for your project and navigate to it in the terminal.

  2. Create a file named app.py with 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)
  1. Create a directory named templates and create an index.html file 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>
  1. Create a file named requirements.txt with the following content:
makefile
Flask==2.0.1
  1. Create a file named Dockerfile in the same directory as your app.py, templates, and requirements.txt files.

  2. 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"]
  1. Build and run the Docker image:

     bash
    
  1.  docker build -t simple-flask-app .
     docker run -p 8080:8080 simple-flask-app
    
  2. Open a web browser and navigate to http://localhost:8080 to 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:

  1. Create a directory for your project and navigate to it in the terminal.

  2. Create a file named app.py with 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)
  1. Create a directory named templates and create an index.html file 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>
  1. Create a file named requirements.txt with the following content:
makefile
Flask==2.0.1
  1. Create a file named Dockerfile in the same directory as your app.py, templates, and requirements.txt files.

  2. 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"]
  1. Build and run the Docker image:

     bash
    
  1.  docker build -t simple-flask-app .
     docker run -p 8080:8080 simple-flask-app
    
  2. Open a web browser and navigate to http://localhost:8080 to 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.

T

crisp and creative

More from this blog

Shreyash Bhise's blog

55 posts