Deploy a Container using Docker Compose & .env

Deploy a Container using Docker Compose & .env

Prerequisites

⭐ Gold Standard Example Layout

/opt/
  containers/
    mautic-prod/
    wordpress-blog/
    n8n-automation/

  docker-data/
    mautic-prod/
    wordpress-blog/
    n8n-automation/

  backups/
    mautic/
    wordpress/

  logs/
    mautic/
    nginx/

🚀 Pro Tips from Production Environments

  1. Use lowercase + hyphens only
good: mautic-prod
bad: Mautic_Prod
  1. Never rename services casually
    Docker volumes & scripts may break.

  2. 🏆Document naming rules once
    Keep a README:
    /opt/containers/README.md

move to directory of choice

root@your-server:~# pwd
/opt/containers
root@your-server:~# ls -lh
total 60K
drwxr-xr-x 3 root root 4.0K Jan  8 04:20 backups
drwxr-xr-x 2 root root 4.0K Dec  1 07:57 evolution-api
-rw-r--r-- 1 root root   88 Dec  4 10:16 hbbs_key
drwxr-xr-x 2 root root 4.0K Jan 10 08:29 homebox
drwxr-xr-x 2 root root 4.0K Jan  8 05:34 karakeep
drwxr-xr-x 2 root root 4.0K Dec  1 09:21 monitoring
drwxr-xr-x 2 root root 4.0K Dec 16 16:42 n8n
drwxr-xr-x 4 root root 4.0K Dec  9 17:01 nginx-proxy-manager
drwxr-xr-x 2 root root 4.0K Dec  1 07:21 postgres
drwxr-xr-x 2 root root 4.0K Dec  1 07:55 redis
drwxr-xr-x 4 root root 4.0K Jan 17 15:03 rustdesk
drwxr-xr-x 2 root root 4.0K Jan 17 15:50 rustdesk-keys-backup
drwxr-xr-x 2 root root 4.0K Jan  8 04:20 scripts
drwxr-xr-x 3 root root 4.0K Dec  1 06:18 web-kamath
drwxr-xr-x 5 root root 4.0K Dec  9 15:46 webserver

i did not follow the best practice named n8n instead of n8n-automation

mkdir n8n && cd n8n

Docker Compose & Env

Ensure you are in n8n ie cd /opt/containers/n8n
make a backup before you edit the docker-compose.yml or .env
cp docker-compose.yml docker-compose.yml.backup
cp .env env.backup
ensure you have excluded the .env and the backup file from git repo

# Create .gitignore entry
echo ".env" >> .gitignore
echo ".env.example" >> .gitignore

or

nano .gitignore
.env
.env.backup
env.backup

docker compose file (docker-compose.yml) (docker compose v2)

services:
  n8n:
    image: n8nio/n8n:latest
    restart: unless-stopped
    networks:
      - web
    env_file:
      - .env
    volumes:
      - n8n_data:/home/node/.n8n
      # - /opt/docker-data/n8n:/home/node/.n8n

networks:
  web:
    external: true

volumes:
  n8n_data:

the .env file

# .env

# N8n Configuration
N8N_HOST=n8n.yourdomain.com
N8N_PORT=5678
N8N_PROTOCOL=https
WEBHOOK_URL=https://n8n.yourdomain.com/

# Authentication
N8N_BASIC_AUTH_ACTIVE=true
N8N_BASIC_AUTH_USER=admin
N8N_BASIC_AUTH_PASSWORD=CHANGE_ME

# Timezone
TZ=Asia/Kolkata
GENERIC_TIMEZONE=Asia/Kolkata

# Execution history cleanup 
EXECUTIONS_DATA_PRUNE=true 
EXECUTIONS_DATA_MAX_AGE=168

## using SQLite and not postgres
# Database Configuration
# DB_TYPE=postgresdb
# DB_POSTGRESDB_DATABASE=n8n_db
# DB_POSTGRESDB_USER=n8n_user
# DB_POSTGRESDB_PASSWORD=N8N_STRONG_PASS
# DB_POSTGRESDB_HOST=postgres
# DB_POSTGRESDB_PORT=5432

Finally to deploy the container

docker compose down
docker compose pull
docker compose up -d
docker image prune -f

Find your container name:

docker ps | grep n8n

Where is the actual location of the volume n8n_data

The full path depends on your project name:
/var/lib/docker/volumes/<project_name>-n8n_data/_data
(Docker adds your project folder name as a prefix)
How to find it:

# See all volumes
docker volume ls

# See exactly where n8n_data is stored
docker volume inspect project_name_n8n_data

will give

root@your-server:~/n8n# docker volume inspect n8n_n8n_data
[
    {
        "CreatedAt": "2025-12-01T05:45:31Z",
        "Driver": "local",
        "Labels": {
            "com.docker.compose.config-hash": "38d498770863d6bc2243973ba19c368c74a2195c888479e037b71146d586c1bb",
            "com.docker.compose.project": "n8n",
            "com.docker.compose.version": "2.37.1",
            "com.docker.compose.volume": "n8n_data"
        },
        "Mountpoint": "/var/lib/docker/volumes/n8n_n8n_data/_data",
        "Name": "n8n_n8n_data",
        "Options": null,
        "Scope": "local"
    }
]

Reference

Docker Commands