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).

Hi, I’m currently using Docker 4.65 and this solution doesn’t work.
I have the following error when building the custom image:

ERROR: failed to build: failed to solve: process “/bin/sh -c apk add --no-cache python3 py3-pip” did not complete successfully: exit code: 127

This solution no longer works with docker 4.69 as of 4/20/2026. I’ve been trying to find a solution and it seems like i’m going to have to bake in an installation of APK first. The hardening has removed the apk manager completely from the image.
So at this time we are unable to edit the docker image and we are unable to build using the n8n git repo docker files as they end up not compiling and the ./compile folder is never found.

To try and install the apk manager

#CODE BLOCK
FROM docker.n8n.io/n8nio/n8n:latest AS base

# Restore apk manually
USER root
RUN ARCH=$(uname -m) && \
    if [ "$ARCH" = "x86_64" ]; then ARCH="x86_64"; else ARCH="aarch64"; fi && \
    wget -qO- "http://dl-cdn.alpinelinux.org/alpine/latest-stable/main/${ARCH}/" | \
    grep -o 'href="apk-tools-static-[^\"]*\.apk"' | head -1 | cut -d'"' -f2 | \
    xargs -I {} wget -q "http://dl-cdn.alpinelinux.org/alpine/latest-stable/main/${ARCH}/{}" && \
    tar -xzf apk-tools-static-*.apk && \
    ./sbin/apk.static -X http://dl-cdn.alpinelinux.org/alpine/latest-stable/main \
        -U --allow-untrusted add apk-tools && \
    rm -rf sbin apk-tools-static-*.apk

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

You can do this

FROM n8nio/n8n:latest
USER root

# Restore apk from Alpine (n8n uses Alpine 3.22)
COPY --from=alpine:3.22 /sbin/apk /sbin/apk
COPY --from=alpine:3.22 /lib/apk /lib/apk
COPY --from=alpine:3.22 /etc/apk /etc/apk

# Now install Python + your packages
RUN apk add --no-cache python3 py3-pip && \
    pip3 install --no-cache-dir requests pandas yfinance tqdm --break-system-packages

USER node