N8n fresh install on Linux laptop with Docker - general questions

Hi there!

I want to test n8n in Docker on my computer to get familiar with Docker and the Local File Trigger node.
I have n8n up and running (and able to save changes).

now I have few questions :

  1. when I close the terminal windows it kills n8n…

I followed Docker | n8n Docs but it doesn’t explain it:

  • How can I run n8n and be able to close it?
  • I’m trying to restart n8n by typing: sudo docker run n8n but it doesn’t work…
  • how can I update n8n to the latest version and make sure it’s the one starting…?
  1. How can I find the path inside the Docker container to have access to my laptop directories?

image

  1. Is it easier to use Portainer to manage my Docker container?

  2. What are the advantages to use a Postgres database?

Thanks a lot for your help :slight_smile:

n8n setup

  • n8n version: 0.233.1
  • Database: SQLite
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via Docker
  • Operating system: Linux Mint 21.1

To run a docker container in the background or detached mode from the terminal, you can use the docker run command followed by the -d flag (or detached flag) and followed by the name of the docker image you need to use in the terminal. For example:

docker run -d <image-name>

This will start the container in detached mode and return the container ID. You can then use this ID to manage your container. If you want to stop your container, you can use the following command:

docker stop <container-id>

I hope that helps! Let me know if you have any other questions.

2 Likes

To map a folder from your local machine to a folder in your Docker container, you can use the -v flag when running the docker run command. The -v flag takes two arguments: the path to the folder on your local machine and the path to the folder in the container. Here’s an example:

docker run -v /path/on/local/machine:/path/in/container <image-name>

This will map the /path/on/local/machine folder on your local machine to the /path/in/container folder in the container.

then point to the path in the container and put the files in the folder on the local machine

I hope that helps!

ok, great @RedPacketSec. I’ll do that. here’s my questions:

  1. I was able to use n8n last night after running sudo docker run -it --rm --name n8n -p 5678:5678 -v ~/.n8n:/home/node/.n8n docker.n8n.io/n8nio/n8n (cf n8n tutorial), but today how can I launch that image?

what I’m doing that doesn’t work:

  • after closing that attached mode window I ran sudo docker ps -l to get the image name and id

  • but when I’m running docker run -d awesome_heyrovsky or docker run -d bcf9a607e80a I’m getting this error:
Unable to find image 'bcf9a607e80a:latest' locally
docker: Error response from daemon: pull access denied for bcf9a607e80a, repository does not exist or may require 'docker login': denied: requested access to the resource is denied.
See 'docker run --help'.

Unable to find image 'awesome_heyrovsky:latest' locally
docker: Error response from daemon: pull access denied for awesome_heyrovsky, repository does not exist or may require 'docker login': denied: requested access to the resource is denied.
See 'docker run --help'.
  1. Also I’d appreciate you to tell me how to update n8n in the future :slight_smile:

Thanks a lot @RedPacketSec :+1:

How can I find that path? I’m confused… :woozy_face:
thanks for your help

anyone has some answers?

Personally, I always prefer to use Portainer to install and manage docker containers. Especially the ‘Stacks’ section, where you can paste in the docker-compose file you have and get the app up and running. I have included my compose file below as an example.

Here, I am using Postgres as the database backend for n8n, since it provides good reliability and long-term support. Also, I would always name my containers (“container_name” attribute) to have them displayed properly and reference them later in the nodes if needed due to the standard docker network protocols.

My volume structure is:

Parent folder: /data/n8n (folder specific to n8n) You can have this linked to any folder in your system, as long as it is isolated and has proper read/write permissions. For example, /home/username/n8n etc.

Sub-Folders: /data/n8n/db - For the Postgres database, and /data/n8n/app - For n8n config files.

Portainer stack:

version: '3.8'

services:
  postgres:
    image: postgres
    restart: always
    container_name: n8n_db
    environment:
      - POSTGRES_USER
      - POSTGRES_PASSWORD
      - POSTGRES_DB
      - POSTGRES_NON_ROOT_USER
      - POSTGRES_NON_ROOT_PASSWORD
    volumes:
      - /data/n8n/db:/var/lib/postgresql/data
      - ./init-data.sh:/docker-entrypoint-initdb.d/init-data.sh
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -h localhost -U ${POSTGRES_USER} -d ${POSTGRES_DB}']
      interval: 5s
      timeout: 5s
      retries: 10  

  n8napp:
    image: n8nio/n8n
    restart: always
    container_name: n8n_app
    environment:
      - DB_TYPE=postgresdb
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
      - DB_POSTGRESDB_USER=${POSTGRES_NON_ROOT_USER}
      - DB_POSTGRESDB_PASSWORD=${POSTGRES_NON_ROOT_PASSWORD}
      - N8N_EDITOR_BASE_URL=https://n8n.myurl.com/
      - WEBHOOK_URL=https://api.myurl.com/
      - EXECUTIONS_PROCESS=main
      - EXECUTIONS_TIMEOUT=-1
      - EXECUTIONS_TIMEOUT_MAX=3600
      - GENERIC_TIMEZONE=Asia/Kolkata
      - TZ=Asia/Kolkata
    ports:
      - 5678:5678
    links:
      - postgres
    volumes:
      - /data/n8n/app:/home/node/.n8n
    command: /bin/sh -c "n8n start --tunnel"
    depends_on:
      postgres:
        condition: service_healthy

Environment Variables:

(In Portainer, you can click on the “Advanced Mode” to just copy and paste this)

POSTGRES_USER=jayavel
POSTGRES_PASSWORD=MyPassword
POSTGRES_DB=n8n
POSTGRES_NON_ROOT_USER=jayavel
POSTGRES_NON_ROOT_PASSWORD=MyPassword2
N8N_BASIC_AUTH_USER=jayavel
N8N_BASIC_AUTH_PASSWORD=MyPassword3

I do not have many workflows using the local files, but with this setup, you can reference the files stored in your laptop with this path: /home/node/.n8n/whateverFileYouWant
For example, inside my “/data/n8n/app” directory, I have a new folder called Files. So, If I need to use the image/something stored in that folder, I can reference that in a workflow as “/home/node/.n8n/files/*.png

Example:

If you’d like to have a separate directory from your system served to the n8n app to read/write files, you can modify the “volumes” section in the stack as follows:

volumes:
      - /data/n8n/app:/home/node/.n8n
      - /home/username/documents:/local/files

Here, “/home/username/documents” becomes the Path on Local Machine, and “/local/files” becomes the Path on Container. So, in your nodes, you can now use the path “/local/files/mountain.png” to get the input/binary data.

Hope this helps!

2 Likes

Thank you so much @Jayavel for those detailed advice! I’m going to test all of this and will get back to you :slight_smile: have a great one!

2 Likes

Hello @Jayavel!
I’m getting back to you after learning more about docker, docker-compose.yml file and portainer stack! (I found this great video: Docker Tutorial for Beginners - A Full DevOps Course on How to Run Applications in Containers - YouTube).
This technology is so cool!

I have few questions that I wrote in comments in your Portainer stack file:

version: '3.8'

services:
  postgres:
    image: postgres
    restart: always
    container_name: n8n_db
    environment:
      - POSTGRES_USER
      - POSTGRES_PASSWORD
      - POSTGRES_DB
      - POSTGRES_NON_ROOT_USER
      - POSTGRES_NON_ROOT_PASSWORD
    volumes:
      - /data/n8n/db:/var/lib/postgresql/data
      - ./init-data.sh:/docker-entrypoint-initdb.d/init-data.sh
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -h localhost -U ${POSTGRES_USER} -d ${POSTGRES_DB}']
      interval: 5s
      timeout: 5s
      retries: 10  

  n8napp:
    image: n8nio/n8n
    restart: always
    container_name: n8n_app
    environment:
      - DB_TYPE=postgresdb 
      - DB_POSTGRESDB_HOST=postgres
      - DB_POSTGRESDB_PORT=5432
      - DB_POSTGRESDB_DATABASE=${POSTGRES_DB}
      - DB_POSTGRESDB_USER=${POSTGRES_NON_ROOT_USER}
      - DB_POSTGRESDB_PASSWORD=${POSTGRES_NON_ROOT_PASSWORD}
      - N8N_EDITOR_BASE_URL=https://n8n.myurl.com/ # what should I type when using localhost? 
      - WEBHOOK_URL=https://api.myurl.com/ # paste the url I'm getting in the webhook node for production right?
      - EXECUTIONS_PROCESS=main
      - EXECUTIONS_TIMEOUT=-1
      - EXECUTIONS_TIMEOUT_MAX=3600
      - GENERIC_TIMEZONE=Asia/Kolkata # change for my time zone
      - TZ=Asia/Kolkata # change for my time zone
    ports:
      - 5678:5678
    links:
      - postgres
    volumes:
      - /data/n8n/app:/home/node/.n8n
    command: /bin/sh -c "n8n start --tunnel"
    depends_on:
      postgres:
        condition: service_healthy

I would only change those commented lines (if I keep the same host path) and the user and passwords in the Environment Variables right?

Also regarding this:

  • The node directory in /home/node/.n8n/whateverFileYouWant is just specific to your file tree naming choice right?
  • what is the advantage of having a separate directory ? (I understand you don’t use it for this n8n stack because you don’t have many workflows using the local files, correct?)
  • could you tell me what is this volume for? (and what is in this script file?)

image

Thanks a lot for your help! :pray:

Hey @headset907,

Great to see that you are learning about Docker. The part I like the most is the docker image file contains all the required things to run the app. Be it n8n, Postgres, or any other software. It takes a big headache away from having to manually fiddle with versioning, dependencies, and other complicated stuff.

Regarding your stack questions.

  1. If you are not planning to expose n8n to the internet using a domain, you can remove the following environment variables:
N8N_EDITOR_BASE_URL
WEBHOOK_URL

Once they are removed, if you use the webhook node, the URL will default to localhost. These variables are only required if you own a domain and plan to connect something like n8n.example.com to your instance.

Also, I used two different URLs here in my setup. This is because of an internal process we are running, where I just need to give the Webhook URL to my teammates, as they are only required to send files to that URL. I edit and maintain the n8n instance, so I use n8n.myurl, and my team uses api.myurl in the their apps.

  1. Yes, please update the time zones according to your local time.

Regarding your question on the node directory.

I used the stack example posted here as a reference to set up my n8n instance. This is taken from n8n’s official repo, which is actively updated (kudos to the team for that). Here, if you notice, they have referenced the internal directory structure as /home/node/.n8n in line 41 (at the time of this writing). So, we should not touch this part.

However, as you said, I do not have the need to reference local files, so I haven’t mounted any separate folders. Instead, I created a folder called “Files” inside my “/data/n8n/app” directory (Not advisable :sweat_smile:) and used that reference inside the workflows I use. So, for example, if your stack has the following volumes:

volumes:
      - n8n_storage:/home/node/.n8n
      - /home/username/documents:/local/files

you should use the path “/local/files/” inside the workflow nodes instead of “/home/node/.n8n/Files/” to use the local files stored in your laptop.

This is one of the advantages of having a separate directory. You can be assured that you are not messing with the app’s config files.

Just a quick side note:

I see that n8n recommends setting up docker volumes for both the app and the database. Since our requirements differ regarding the backup and other stuff, we had to mount the local directory to the docker instance. This is why you see /data/n8n/app:/home/node/.n8n in my compose.

Also, for your question on the /init-data.sh, I’m not an expert in coding and databases, but the primary thing is, Postgres has the option to run custom commands during startup, etc. n8n must be using some commands/custom work to ensure that the database is starting correctly and all the workflows are loaded, when the system restarts or something like that.

The n8n team can best answer this question.

Hi @Jayavel,
thanks a lot for those details.

I finally created and launched my first stack!

but I’m running into problems :slight_smile:
Unable to connect localhost:5678

  • when I look at the n8n_app logs it says:
    Error: command /bin/sh not found

After looking at the stack example provided by n8n I see they don’t have this line so I removed it:

  • now the logs give me:
    Error: EACCES: permission denied, open '/home/node/.n8n/crash.journal' Error: Exiting due to an error.

I try to enter the terminal to check out the crash.journal file but it fails…
image

image

I wait for it to run again but same problem…

Do you have an idea of what’s happening?

1 Like

I am guessing this will be because of the new migration requirements.

If permission errors appear in your n8n container logs when starting n8n, you may need to update the permissions by executing the following command on the Docker host:

docker run --rm -it --user root -v ~/.n8n:/home/node/.n8n --entrypoint chown n8nio/base:16 -R node:node /home/node/.n8n

I am guessing, you’d need to first stop the current stack fully. Both n8n and postgres. Run this command directly or as sudo based on your config and then try to start the stack without the command line argument as you did earlier.

It should work.

1 Like

I still have the problem with the same log:

:thinking:

Hey @headset907,

Are you using something like Portainer? There is an extra step needed if you are when using 1.0 as it doesn’t correctly update the settings.

If you go to your containers advanced settings in the UI make sure the user is now node instead of root and the working directory is /home/node/ instead of /data

Hi @Jon, thanks for chiming in.

Yes, that’s right I’m using Portainer.

Ok cool thanks for added details. I have 2 questions:

  • which container are you talking about? the n8n_app one or the n8n_db one?

image

  • when I’m in the containers I can’t find the advanced container settings and access the window you’re showing… the only advanced settings I have access to are for the image…

image

Thanks

Hey @headset907,

It will be the n8n_app container, You can find the setting I shared by clicking on n8n_app then clicking Dupelicate / Edit on the top menu then scroll down to Advanced container settings and it will be there.

hey @Jon, sorry for the late response and thanks for your response unfortunately my settings are exactly the one you sent me but it’s not working.


I did redeploy with Interactive & TTY selected but it’s still not working…

Hey @headset907,

That is intersting, That should be all you need to do. Can you try running the command below.

docker run --rm -it --user root -v ~/.n8n:/home/node/.n8n --entrypoint chown n8nio/base:16 -R node:node /home/node/.n8n

Make sure you sway out ~/.n8n with the correct path to your volume.

1 Like

Hi @Jon, unfortunately it doesn’t work… :anguished:
Would you happen to have some complete steps for a sure build on localhost with portainer and postgres? ( I’m ready to restart everything)
thank you

Hey @headset907,

I am fairly confident in the step above as it has resolved this same issue for others if it has not worked it could be that ~/.n8n was not replaced.

We don’t have instructions specific to Portainer but if you can share a screenshot of your volumes for the n8n container I can get you the correct command to run.