How to install python in local host via docker

Describe the problem/error/question

I’m trying to run n8n via docker and my flow requires python and community lib. I tried to run several times and there was always error which is mainly about apk. Honestly, I’m low code person so it is mainly gemini code. But here is how my Dockerfile looks like
FROM n8nio/n8n:latest
USER root
RUN apk add --update --no-cache
python3
py3-pip
build-base
python3-dev
libffi-dev
libxml2-dev
libxslt-dev
RUN python3 -m venv /opt/venv
ENV PATH=“/opt/venv/bin:$PATH”
RUN pip install --no-cache-dir
pdfplumber
python-docx
openpyxl
pandas
USER node

And here is my yml file
services:
n8n:
build: .
container_name: n8n-mine
restart: always
ports:

  • “5678:5678”
    environment:
  • N8N_SECURE_COOKIE=false
  • EXECUTIONS_DATA_PRUNE=true
  • EXECUTIONS_DATA_MAX_AGE=168
  • GENERIC_TIMEZONE=Asia/Bangkok
  • TZ=Asia/Bangkok
  • N8N_BLOCK_FS_READ_ACCESS=false
  • N8N_BLOCK_FS_WRITE_ACCESS=false
  • N8N_RESTRICT_FILE_ACCESS_TO=/files
    volumes:
  • n8n_data:/home/node/.n8n
  • ./files:/files
    volumes:
    n8n_data:

The error code is below
I will really appreciate any suggestions. Thanks!

What is the error message (if any)?

=> ERROR [2/4] RUN apk add --update --no-cache python3 py3-pip build-base python3-dev libffi 0.1s

------

> [2/4] RUN apk add --update --no-cache python3 py3-pip build-base python3-dev libffi-dev libxml2-dev libxslt-dev:

0.130 /bin/sh: apk: not found

------

[+] up 0/1

⠙ Image n8n-sertis-n8n Building 1.8s

Dockerfile:3

--------------------

2 | USER root

3 | >>> RUN apk add --update --no-cache \

4 | >>> python3 \

5 | >>> py3-pip \

6 | >>> build-base \

7 | >>> python3-dev \

8 | >>> libffi-dev \

9 | >>> libxml2-dev \

10 | >>> libxslt-dev

11 | RUN python3 -m venv /opt/venv

--------------------

failed to solve: process “/bin/sh -c apk add --update --no-cache python3 py3-pip build-base python3-dev libffi-dev libxml2-dev libxslt-dev” did not complete successfully: exit code: 127

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: macos

according to n8n Docs :slight_smile:

To get Python available in your local n8n instance running via Docker, you typically extend the official n8n image and install Python inside it, then run that custom image locally.

1. Create a Dockerfile that adds Python

Create a file named Dockerfile in an empty folder:

FROM docker.n8n.io/n8nio/n8n:latest

# Install Python and pip in the Alpine-based n8n image
USER root
RUN apk add --no-cache python3 py3-pip
USER node

This follows the same pattern used in community solutions where Python is added to the n8n container via apk add python3 py3-pip on Alpine-based images. [Can’t set up Whisper; Hostinger + Python]

2. Build the custom image

From the same folder as the Dockerfile:

docker build -t n8n-python .

3. Run n8n with the new image on localhost

Use the usual n8n Docker run command, but reference your new image:

docker volume create n8n_data

docker run -it --rm \
  --name n8n \
  -p 5678:5678 \
  -e GENERIC_TIMEZONE="Europe/Berlin" \
  -e TZ="Europe/Berlin" \
  -e N8N_ENFORCE_SETTINGS_FILE_PERMISSIONS=true \
  -e N8N_RUNNERS_ENABLED=true \
  -v n8n_data:/home/node/.n8n \
  n8n-python

This is the same pattern the docs use to start n8n locally with Docker, just pointing to your custom image instead of the stock one. [Docker installation]

After this, Python will be available inside the n8n container (for example to use from Execute Command, or for tools that rely on a system Python).

1 Like