How to Dockerize Applications with Docker Compose (Using SQLite and Flask)
Last updated
Last updated
Below is a set of instructions for Dockerizing a Flask and SQLit project. This can also be done with other mixture of technologies such as using Postgres instead. This is intended to be a guide for other projects as well.
.dockerignore
FileBefore building the Docker image, create a .dockerignore
file to exclude unnecessary files and directories from the Docker image.
Create a .dockerignore
file in the root of your project.
Add common files to ignore:
This will ensure unnecessary files like Python bytecode, Git history, and documentation aren't copied into your Docker image.
Dockerfile
The Dockerfile
defines how to build the Docker image for your app.
Dockerfile
Create a Dockerfile
in the root directory of your project.
Add the following configuration for building the image:
Dockerfile
:FROM python:3.11-slim: Use a minimal Python image.
WORKDIR /usr/src/app: Set the working directory inside the container.
RUN apt-get update && apt-get install gcc: Install system dependencies.
COPY requirements.txt .: Copy your requirements.txt
file into the container.
RUN pip install --no-cache-dir -r requirements.txt: Install Python dependencies.
COPY . .: Copy all project files (excluding those in .dockerignore
) into the container.
EXPOSE 5000: Expose port 5000
, which is where Flask will run.
CMD ["flask", "run", "--host=0.0.0.0", "--port=5000"]: Start the Flask application.
docker-compose.yml
FileFor SQLite, you don’t need to set up a database service in Docker Compose because SQLite is just a file-based database. You’ll only need to configure the web app service.
docker-compose.yml
FileCreate a docker-compose.yml
file in the root directory of your project.
Add the following configuration for your web app:
docker-compose.yml
:version: Specifies the version of the Docker Compose file format. We are using version 3.8
.
services: Defines the services (containers) for your application.
web: The Flask application container.
build: Tells Docker Compose to build the image from the current directory (which includes the Dockerfile
).
ports: Maps port 5000
on your local machine to port 5000
in the container.
environment: Sets the environment variable FLASK_APP=main.py
, which tells Flask to use main.py
as the entry point.
volumes: Maps a local directory (./data
) to a directory in the container (/usr/src/app/data
). This is where the SQLite database file will be stored. This ensures the database persists between container restarts.
Now that your Dockerfile
and docker-compose.yml
are set up, you can build and run your Flask application along with SQLite.
Open a terminal and navigate to the directory where your docker-compose.yml
file is located.
Run the following command to build the images and start the containers:
The --build
flag will rebuild the images if there are changes in your Dockerfile
.
Once the containers are up and running, you can access your application in a web browser at http://localhost:5000
.
If you want to run the containers in the background, use the -d
flag:
This will start the containers in the background and allow you to continue using the terminal.
To stop the containers and remove the associated networks, use the following command:
Since you're using SQLite, the database file will be stored on the host machine and mapped to a directory inside the container (./data:/usr/src/app/data
in the docker-compose.yml
). By default, Flask with SQLite will create a file-based database (e.g., app.db
).
Ensure your Flask app is configured to use the SQLite database file from the mounted volume. For example, in your main.py
file, you might have:
The DATABASE
path is set to /usr/src/app/data/app.db
, which is inside the container at /usr/src/app/data
, but is mapped to a directory (./data
) on the host. This means your SQLite database will be stored in the ./data
folder on your host machine, and the container will be able to read/write from it.
If you want to share your Docker images or deploy your app, you can push the image to Docker Hub.
Tag your image with your Docker Hub username and repository name:
Push the tagged image to Docker Hub:
Database Persistence: By using a volume to store the SQLite database file, you ensure that your data persists even if you stop and remove the container.
Scaling with Docker Compose: If you need to scale your Flask app or add more services later, Docker Compose allows you to easily define and scale multiple instances of your services.
Backup SQLite: You can backup the SQLite database by copying the ./data
directory from your host system.