Slim Python Containers

This Slim Starter Kit will show you how to build a Python Hello World application and use the Slim platform to secure it for production.
sk-g sk-g

Hello Python developers!

Optimize and harden your containerized applications the easy way — with Slim.AI.

This Starter Kit will help you proactively remove vulnerabilities from your applications.

Simply replace the application code here with your own application, run it through Slim.AI's automated container optimization process, and you'll remove up to 90-percent of the image's vulnerabilities while also making it up to 30X smaller.

No more chasing down hard to patch vulns that your application isn't even using, and you can use any base image you want — even python:latest.

Optimization Results

Result of optimized Python image

Slimming this Python container results in 100-percent reduction in overall vulnerabilities.

Get Started

To start a Python application application, you'll need the following libraries installed locally, or running in a dev environment link GitPod, Docker Environments, or Code Spaces.

# requirements.txt
Flask=2.2.1

Sample Application

Our sample application is a simple REST API that merely returns "Hello World!".

While this app is simple, it's a great starting point for more complex development.

Project structure

Dockerfile
|- app
|-- app.py
|-- requirements.txt
|- tests
|-- test.py

Code sample

@app.route("/")
def hello():
    return jsonify({"msg": "Hello, World!"})

Replace this placholder code with your own application code, and install any necessary dependencies, to create your own slimmable container.

Sample Dockerfile

Our Dockerfile builds off of the python:latest image and starts at 943 MB.

FROM python:latest

# upgrade pip
RUN pip install --upgrade pip

# permissions and nonroot user for tightened security
RUN adduser nonroot
RUN mkdir /home/app/ && chown -R nonroot:nonroot /home/app
WORKDIR /home/app
USER nonroot

# copy and install requirements.txt
COPY --chown=nonroot:nonroot ./app/requirements.txt ./requirements.txt
RUN pip install -r requirements.txt

# copy app files
COPY --chown=nonroot:nonroot ./app .

# define the port number the container should expose
EXPOSE 5000

CMD ["python", "app.py"]

The slimming process reduces the size by 33X to just 28 MB.

Try it yourself on Slim.AI