This documentation outlines the configuration for Docker within the development environment of the benhall/flask-demo project. The focus is on the setup needed to facilitate local development using Docker, as well as integration with the provided development tasks.

Prerequisites

Before starting with Docker, ensure that you have Docker installed on your machine. You can find installation instructions on the official Docker website.

Dockerfile Setup

The following steps detail how to configure the Dockerfile for a Flask application. This setup will allow you to containerize the Flask application while preserving the development workflow.

Step 1: Create a Dockerfile

In the root directory of the project, create a file named Dockerfile with the following content:

# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /usr/src/app

# Copy the current directory contents into the container at /usr/src/app
COPY . .

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

Step 2: Building the Docker Image

To build the Docker image, navigate to the project directory and execute the following command:

docker build -t flask-demo .

This command uses the Dockerfile to assemble the image, tagging it with the name flask-demo.

Step 3: Running the Container

To run the container based on the image you just built, execute:

docker run -p 5000:5000 flask-demo

This command maps port 5000 on your host to port 5000 on the container, allowing access to the Flask application through your local machine.

Step 4: Using Makefile Targets with Docker

You can extend your Makefile to integrate Docker commands for different operations. For instance, you can add targets for building and running the Docker container.

Update Makefile

Modify your Makefile as follows:

.PHONY: run test docker-build docker-run

docker-build:
    @echo "Building Docker image..."
    docker build -t flask-demo .

docker-run:
    @echo "Starting Docker container..."
    docker run -p 5000:5000 flask-demo

Step 5: Running Development Commands

Leverage the Makefile targets for common development tasks. Here’s how you can do it.

  1. Build the Docker Image:

    make docker-build
    
  2. Run the Docker Container:

    make docker-run
    
  3. Run Tests Inside Docker:

    If you want to run your tests using Docker, you can create another target in your Makefile:

    docker-test:
        @echo "Running tests in Docker..."
        docker run flask-demo python -m unittest discover -s tests
    

Example: Running Tests with Docker

Execute the tests inside the Docker container with the following command:

make docker-test

This command ensures that your tests run in the same environment as your Flask application, providing consistency for development and debugging.

Conclusion

By following these steps, you can leverage Docker within the development environment of the benhall/flask-demo project. This setup allows you to build, manage, and run your Flask application consistently across various development environments without the risk of dependency conflicts or compatibility issues.

All configurations and commands above are designed to maximize workflow efficiency while maintaining the flexibility offered by Docker.