How to install python on 1.x image using docker image?

Hello, on version 0.x I have used the following scheme using docker:

FROM n8nio/n8n
RUN apk add --update python3 py3-pip

# installs requests library
RUN python3 -m pip install requests

# upgrades pip (not necessary)
RUN python3 -m pip install --upgrade pip

On 1.x it doesn't work. Does anyone have a clue how to add python and some libraries to 1.x docker?

Regards,

Maciej

Hey @Maciej_Brylewicz,

Welcome to the community :tada:

What happens when you try that on v1? Do you get an error?

My Dockerfile:
FROM n8nio/n8n:latest
RUN apk add --update python3 py3-pip

RUN python3 -m pip install phonenumbers
RUN python3 -m pip install datetime

Result:
[+] Building 1.8s (5/7) docker:default
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 277B 0.0s
=> [internal] load metadata for docker.io/n8nio/n8n:latest 1.3s
=> CACHED [1/4] FROM docker.io/n8nio/n8n:latest@sha256:1b5480d076538a967 0.0s
=> ERROR [2/4] RUN apk add --update python3 py3-pip 0.4s

[2/4] RUN apk add --update python3 py3-pip:
0.377 ERROR: Unable to lock database: Permission denied
0.378 ERROR: Failed to open apk database: Permission denied


Dockerfile:2

1 | FROM n8nio/n8n:latest
2 | >>> RUN apk add --update python3 py3-pip
3 |
4 | RUN python3 -m pip install phonenumbers

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

You will need to specify ‘root’ as the user before installing/updating packages.

This should work:

FROM n8nio/n8n:latest

USER root
WORKDIR /data
RUN apk add --update --no-cache python3 curl
RUN python3 -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools

Starting with n8n release 1, n8n runs under the ‘node’ user for security purposes. Make sure to specify the ‘node’ user in your docker-compose.yaml file for n8n.

docker-compose.yaml should look similar to this:

version: '3.8'

services:
  n8n:
    image: n8n-python # the image you built with your Dockerfile
    user: "node" # specify 'node' as the user running the n8n app
    restart: unless-stopped
    container_name: n8n-python
    environment:
      GENERIC_TIMEZONE: ${TIMEZONE}
      TZ: ${TIMEZONE}
    ports:
      - "5678:5678"
    volumes:
      - ./n8n_data:/home/node/.n8n
      - ./local-files:/data/files # by default workdir == /data
      - ./python_scripts:/data/py_scripts
      - ./requirements.txt:/data/requirements.txt

Also, don’t forget to declare TIMEZONE (along with other variables) in your .env file.

1 Like

Sadly still something is odd here:

[+] Building 14.6s (8/9) docker:default
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.1s
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 249B 0.1s
=> [internal] load metadata for docker.io/n8nio/n8n:latest 1.2s
=> CACHED [1/6] FROM docker.io/n8nio/n8n:latest@sha256:1b5480d076538a967 0.0s
=> [2/6] WORKDIR /data 0.0s
=> [3/6] RUN apk add --update --no-cache python3 curl 4.8s
=> [4/6] RUN python3 -m ensurepip 6.7s
=> ERROR [5/6] RUN pip3 install --no-cache --upgrade pip setuptool 1.9s

[5/6] RUN pip3 install --no-cache --upgrade pip setuptool:
0.998 Requirement already satisfied: pip in /usr/lib/python3.11/site-packages (23.2.1)
1.468 ERROR: Could not find a version that satisfies the requirement setuptool (from versions: none)
1.468 ERROR: No matching distribution found for setuptool


Dockerfile:7

5 | RUN apk add --update --no-cache python3 curl
6 | RUN python3 -m ensurepip
7 | >>> RUN pip3 install --no-cache --upgrade pip setuptool
8 | RUN python3 -m pip install phonenumbers

9
ERROR: failed to solve: process “/bin/sh -c pip3 install --no-cache --upgrade pip setuptool” did not complete successfully: exit code: 1
maciej@smoobu:~/devel/tools/n8n$

Make sure it’s ‘setuptools’, not ‘setuptool’.
RUN pip3 install --no-cache --upgrade pip setuptools

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.