Losing data when restart docker (build with Python)

I’m using n8n, and I lose data every time I use the command “sudo docker compose down.”
Here’s my docker-compose.yml file:

version: "3.7"

services:
  caddy:
    image: caddy:latest
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ${DATA_FOLDER}/caddy_data:/data
      - ${DATA_FOLDER}/caddy_config:/config
      - ${DATA_FOLDER}/caddy_config/Caddyfile:/etc/caddy/Caddyfile

  n8n:
    image: n8n-custom
    restart: always
    ports:
      - 5678:5678
    environment:
      - N8N_HOST=${SUBDOMAIN}.${DOMAIN_NAME}
      - N8N_PORT=5678
      - N8N_PROTOCOL=https
      - NODE_ENV=production
      - WEBHOOK_URL=https://${SUBDOMAIN}.${DOMAIN_NAME}/
      - GENERIC_TIMEZONE=${GENERIC_TIMEZONE}
      - NODE_FUNCTION_ALLOW_EXTERNAL=module1,module2 
    volumes:
      - ${DATA_FOLDER}/local_files:/files
      - ${DATA_FOLDER}/.n8n:/home/node/.n8n
      - /home/n8n/code:/home/n8n/code
volumes:
  caddy_data:
    external: true
  caddy_config:

My .env file

DATA_FOLDER=/home/n8n/n8n-docker-caddy
DOMAIN_NAME=xxx.yyy
SUBDOMAIN=n8n
GENERIC_TIMEZONE=Europe/Berlin
[email protected]

My Dockerfile to build n8n-custom image:

FROM n8nio/n8n:latest
USER root
RUN apk add --update python3 py3-pip
RUN apk add \
    build-base \
    freetds-dev \
    g++ \
    gcc \
    tar \
    gfortran \
    gnupg \
    libffi-dev \
    libpng-dev \
    libsasl \
    openblas-dev \
    openssl-dev 
RUN npm install -g require
RUN npm install -g humanparser

My data folder’s permission:

n8n@n8n-test:~/n8n-docker-caddy$ ls -l -a
total 48
drwxrwxrwx 7 n8n  n8n  4096 Aug 21 04:20 .
drwxr-x--- 7 n8n  n8n  4096 Aug 21 03:21 ..
-rwxrwxrwx 1 n8n  n8n   643 Aug 14 08:40 .env
drwxrwxrwx 8 n8n  n8n  4096 Aug 11 02:44 .git
drwxrwxrwx 2 n8n  n8n  4096 Aug 21 04:41 .n8n
-rwxrwxrwx 1 n8n  n8n   317 Aug 21 03:59 Dockerfile
-rwxrwxrwx 1 root root 1082 Aug 11 02:44 LICENSE
-rwxrwxrwx 1 root root  779 Aug 11 02:44 README.md
drwxrwxrwx 3 n8n  n8n  4096 Aug 11 02:44 caddy_config
drwxrwxrwx 3 n8n  n8n  4096 Aug 11 02:44 caddy_data
-rwxrwxrwx 1 n8n  n8n   865 Aug 21 03:41 docker-compose.yml
drwxrwxrwx 2 n8n  n8n  4096 Aug 11 02:44 local_files

I noticed that no files are saved in the /home/n8n/n8n-docker-caddy/.n8n folder (using the ls -a command), even though I’ve created an account and generated flows. Could this be the reason for the data loss?
Thank you for your assistance.

  • **n8n version: 1.1.1
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • **Running n8n via (Docker, npm, n8n cloud, desktop app): docker
  • **Operating system: Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-71-generic x86_64)

Hey @phuong,

Welcome to the community :cake:

Can you check in your image which user n8n is running as? It should be running as the node user but looking at your custom image you swap the user to root and don’t change it back at the end, It could be that the issue is because of that.

3 Likes

I added “USER node” to the end of the Dockerfile, and it runs perfectly fine. You did help me a lot. Thank you

2 Likes

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