J2897

Building an n8n custom Docker image

Sep 12th, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!

Step-by-Step Guide

  1. Create a Directory for Your Docker Setup

    Create a new directory on your local machine to store the Dockerfile and build script.

    mkdir custom-n8n
    cd custom-n8n
  2. Create the Dockerfile

    Inside the custom-n8n directory, create a file named Dockerfile:

    touch Dockerfile

    Then, open it in a text editor (e.g., nano, vim, or any code editor) and paste the contents of the Dockerfile provided earlier:

    Dockerfile:

    # Start from the latest n8n image
    FROM n8nio/n8n:latest
    
    # Install dependencies required for Pyenv and Python builds
    USER root
    RUN apk add --no-cache bash curl git make gcc g++ zlib-dev bzip2-dev readline-dev sqlite-dev openssl-dev tk-dev libffi-dev linux-headers xz-dev
    
    # Set environment variables for Pyenv
    ENV PYENV_ROOT="/root/.pyenv"
    ENV PATH="$PYENV_ROOT/bin:$PATH"
    
    # Install Pyenv
    RUN curl https://pyenv.run | bash
    
    # Set up Pyenv in the shell environment
    RUN echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> /root/.bashrc && \
       echo 'eval "$(pyenv init --path)"' >> /root/.bashrc && \
       echo 'eval "$(pyenv init -)"' >> /root/.bashrc && \
       echo 'eval "$(pyenv virtualenv-init -)"' >> /root/.bashrc
    
    # Install multiple Python versions
    RUN bash -c "source /root/.bashrc && \
       pyenv install 3.9.19 && \
       pyenv install 3.10.14 && \
       pyenv install 3.11.9 && \
       pyenv install 3.12.6 && \
       pyenv global 3.12.6"
    
    # Set the user back to 'node' (default user for n8n)
    USER node
  3. Create the Build Script

    Still in the custom-n8n directory, create a file named build_n8n_custom.sh:

    touch build_n8n_custom.sh

    Open it in a text editor and paste the following script:

    build_n8n_custom.sh:

    #!/bin/bash
    
    # Define the image name and tag
    IMAGE_NAME="custom-n8n"
    IMAGE_TAG="latest"
    
    # Build the custom Docker image
    echo "Building the custom n8n image..."
    docker build -t $IMAGE_NAME:$IMAGE_TAG .
    
    # Display success message
    if [ $? -eq 0 ]; then
       echo "Custom n8n image built successfully: $IMAGE_NAME:$IMAGE_TAG"
    else
       echo "Failed to build the custom n8n image."
       exit 1
    fi

    Make the script executable by running:

    chmod +x build_n8n_custom.sh
  4. Build the Custom Docker Image

    Run the build_n8n_custom.sh script to build your custom n8n image with the desired Python versions:

    ./build_n8n_custom.sh

    This will create a Docker image named custom-n8n:latest based on the Dockerfile you created.

  5. Run Your Custom n8n Image

    After building the image, you can start your custom n8n instance using the following command:

    docker run -d --rm \
       --name n8n \
       -p 5678:5678 \
       -e GENERIC_TIMEZONE="Europe/London" \
       -e TZ="Europe/London" \
       -v n8n_data:/home/node/.n8n \
       -v /home/J2897/Projects:/home/node/Projects \
       custom-n8n:latest

Now, you have a Docker image with n8n and multiple Python versions set up via Pyenv. When a new n8n version is released, simply update the FROM n8nio/n8n:latest line in the Dockerfile, re-run the build script, and your custom environment will be ready with the latest version.

Add Comment
Please, Sign In to add comment