This is how I package my flask app for docker container

Here is my steps to copy my app to testing server and package it ready for production.

  1. tar ball the complete folder and upload it to testing server
tar -zcf ~/docker/my-flask-app_$(date +"%Y-%m-%d_%H-%M-%S").tar.gz -C ~/codes/python my-flask-app

rsync -a ~/docker/my-flask-app*.tar.gz username@testing-server:~/docker
  1. Un tar the package and prepare for Dockerization
tar -xvzf ~/docker/my-flask-app_2024-02-13_15-50-00.tar.gz

cd my-flask-app
# If you are using VENV (which you should use)
source ./venv/bin/activate
# Create the requirement.txt file
pip freeze > requirements.txt
  1. Create Dockerfile for creating docker image
# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory in the container

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

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

# Make port 5000 available to the world outside this container

# Define environment variable

# Run when the container launches
CMD ["flask", "run", "--host="]
  1. Next step is to build the image
docker build -t my-flask-app .

# build from m1 mac for amd64 machine
docker build --platform linux/amd64 -t my-flask-app .
  1. Docker run to test the image
docker run -d -p 5001:5000 --name my-flask-app --rm my-flask-app

# docker run
# -d detach mode
# -p port 5001 as outside port : 5000 as the flask app port
# --name name the container
# --rm remove the container after the container stop
# my-flask-app use my-flask-app image
  1. Export the docker image for production
docker save -o /path/to/save/your_image.tar your_image_name_or_id
  1. After upload the image to production server use -i to import the docker image
docker load -i /path/to/save/your_image.tar
# rename import image
docker tag <image_id> <new_image_name>:<new_tag>

My Flask app

python3 -m venv venv
source ./venv/bin/activate
pip install --upgrade pip
pip install Flask

# After create the flask app
flask run --host= --port=8000  

Log In

How to Use Docker + Webtop to Secure Your Online Activities