Skip to main content

Command Palette

Search for a command to run...

Building and Testing a Flask App with Docker: A Step-by-Step Guide ๐Ÿš€

Published
โ€ข2 min read
Building and Testing a Flask App with Docker: A Step-by-Step Guide ๐Ÿš€

Creating and testing a Flask application using Docker can streamline your development process, ensuring consistency across different environments. This guide will walk you through the steps to set up and test a Flask application using Docker. Let's get started!

Before we begin, make sure you have the following installed:

  • install Docker on your AWS machine

  • AWS EC2 Machine

    ๐Ÿ›  Step-by-Step Guide

    1. ๐Ÿ“‚ Set Up Your Project Directory

    clone my repo on Github :

      git clone https://github.com/omkarthorbole15701/Docker-material.git
    

Change directory:

cd Docker-materials

Go to directory where is your Docker file.

cd FLASK

๐Ÿณ Create a Docker file

# Use a specific version of Python with Alpine
FROM python:2.7-alpine3.10

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

# Install dependencies
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

# Copy the application files
COPY app.py ./
COPY templates/index.html ./templates/

# Expose the port the app runs on
EXPOSE 5000

# Define the command to run the application
CMD ["python", "app.py"]

๐Ÿš€ Build Your Docker image:

docker build -t divyasatpute/sanity-test .

Run your container

docker run -p 8080:5000 --name testcase omkarthhorbole15701/sanity-test

Now โœ… Testing Your Flask App

๐ŸŒ Access Your Flask Application

Copy public IP of your Instance and paste on tab

3.80.43.227:8080

Conclusion

By following these steps, you've successfully deployed your Flask application using Docker and exposed it to the internet with a public IP address. This setup is suitable for testing and development purposes. For production, consider additional steps for security, scalability, and performance optimization. Happy coding! ๐Ÿš€

Here are my POC Result