Docker Cargo Container Lot
Chanaka via Pexels
Home / Dockerising Python Scripts (Python Script Container)
Python

Dockerising Python Scripts (Python Script Container)

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.

Continue your adventure here

Leave a Comment

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

DON’T MISS OUT!
Subscribe To Newsletter
Be the first to get latest updates and exclusive content straight to your email inbox.
Stay Updated
Give it a try, you can unsubscribe anytime.
close-link