Containerizing a Flask, React, and MySQL Application
Creating a Dockerfile is essential for containerizing your application. This guide will walk you through the steps to create a Dockerfile for a service with a backend and frontend.
Backend Dockerfile
Section titled “Backend Dockerfile”First, let’s create a Dockerfile for the backend service. This example uses Python and Flask.
Step 1: Create a Dockerfile
Section titled “Step 1: Create a Dockerfile”-
Create a file named
Dockerfilein the root of your backend directory. -
Add the following content to the
Dockerfile:# Use the official Python image from the Docker HubFROM python:3.9-slim# Set the working directoryWORKDIR /app# Copy the requirements file into the containerCOPY requirements.txt requirements.txt# Install the dependenciesRUN pip install -r requirements.txt# Copy the rest of the application code into the containerCOPY . .# Expose the port the app runs onEXPOSE 6000# Run the application with GunicornCMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:6000", "wsgi:app"]
Step 2: Create a requirements.txt File
Section titled “Step 2: Create a requirements.txt File”Ensure you have a requirements.txt file in your backend directory with the necessary dependencies:
Flask==2.3.3Flask-Cors==4.0.0Flask-JWT-Extended==4.5.2Flask-Migrate==4.0.5Flask-SQLAlchemy==3.1.1gunicorn==21.2.0Step 3: Create a wsgi.py File
Section titled “Step 3: Create a wsgi.py File”Create a wsgi.py file to serve your Flask application:
from portfolio_app import create_app
app = create_app()
if __name__ == "__main__": app.run(host="0.0.0.0", port=6000)Frontend Dockerfile
Section titled “Frontend Dockerfile”Next, let’s create a Dockerfile for the frontend service. This example uses React.
Step 1: Create a Dockerfile
Section titled “Step 1: Create a Dockerfile”-
Create a file named
Dockerfilein the root of your frontend directory. -
Add the following content to the
Dockerfile:# Use the official Node.js image from the Docker HubFROM node:14# Set the working directoryWORKDIR /app# Copy the package.json and package-lock.json files into the containerCOPY package*.json ./# Install the dependenciesRUN npm install# Copy the rest of the application code into the containerCOPY . .# Build the applicationRUN npm run build# Install serve to serve the buildRUN npm install -g serve# Expose the port the app runs onEXPOSE 4000# Run the applicationCMD ["serve", "-s", "build"]
Step 2: Create a package.json File
Section titled “Step 2: Create a package.json File”Ensure you have a package.json file in your frontend directory with the necessary dependencies:
{ "name": "frontend", "version": "0.0.0", "private": true, "scripts": { "dev": "vite", "build": "vite build", "serve": "serve -s build" }, "dependencies": { "react": "^18.2.0", "react-dom": "^18.2.0" }, "devDependencies": { "@vitejs/plugin-react": "^4.0.3", "vite": "^4.4.5" }}Docker Compose
Section titled “Docker Compose”To manage both the backend and frontend services, you can use Docker Compose.
Step 1: Create a docker-compose.yml File
Section titled “Step 1: Create a docker-compose.yml File”-
Create a file named
docker-compose.ymlin the root of your project directory. -
Add the following content to the
docker-compose.ymlfile:version: '3.8'services:backend:build:context: ./backendports:- "6000:6000"environment:- FLASK_ENV=productionfrontend:build:context: ./frontendports:- "4000:4000"
Step 2: Build and Run the Docker Containers
Section titled “Step 2: Build and Run the Docker Containers”-
Open a terminal and navigate to the root of your project directory.
-
Run the following command to build and start the containers:
Terminal window docker-compose up --build
Your backend should now be running on http://localhost:6000 and your frontend on http://localhost:4000.
Conclusion
Section titled “Conclusion”By following these steps, you have successfully created Dockerfiles for both the backend and frontend services and used Docker Compose to manage them. This setup allows you to easily build, run, and manage your application in a containerized environment.