Dockerising Python Scripts (Python Script Container)

Cargo Container Lot

The following is a generic Docker image for running Python scripts. I personally use this to host long running Python scripts such as MQTT clients and virtualised hardware such as the RF MQTT Gateway and Audio Buzzer.

It doesn’t make sense for me to upload it to Dockerhub because you will have to specify the dependencies of your Python script to be installed during docker build.

If there is a way to make this easier, please let me know. {: .notice–info}

FROM arm32v7/python:3.6.4-slim-jessie

RUN apt-get update && apt-get install -y python-dev python3-dev build-essential pkg-config libfreetype6-dev libpng12-dev

# Specify any dependencies required by your script in here
#RUN pip3 install RPi.GPIO paho-mqtt

RUN mkdir app

WORKDIR /app

CMD ["python", "./script.py"]

The docker-compose.yaml file is where your Python script is loaded into the container. This means you can use the same image to run multiple python scripts (as long as they have the same dependencies)

version: "3"
services:
  my-container:
    build: .
    restart: unless-stopped
    volumes:
      - ./my-python-script.py:/app/script.py

The reason I (have to) create these custom containers is because the offical image sources often do not supply compiled images for arm platforms. Since I run Docker exclusively on Raspbrery Pi, I often find myself compiling (and fixing in the process) images from scratch…

I hope this helps you. As always I welcome suggestions for improvement, the fact that dependencies have to be specified and a new image compiled every time seems weird.

Related posts

I wrote a PDF scan clean up & optimisation tool using Bash, GNU Parallel, ImageMagick convert and Python Tkinter

Implementing a simple Expiring Dictionary in Python

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More