开源库存管理系统InvenTree的安装

本文是应网友 shijie880500 要求折腾的;

什么是 InvenTree ?

InvenTree 是一个开源的库存管理系统,提供强大的低级别库存控制和零件跟踪。InvenTree 系统的核心是 Python/Django 数据库后端,它提供了一个管理界面(基于 web)和一个 REST API,用于与外部接口和应用程序交互。强大的插件系统为自定义应用程序和扩展提供支持。

前期准备

在群晖上以 Docker 方式安装。因为涉及到多个容器,所以采用了 docker-compose 安装

老苏是按生产环境搭建,因为开发模式只能用 localhost 访问,所需的文件都来自于官方的 production 目录:https://github.com/inventree/InvenTree/tree/master/docker/production

版本选择的是 InvenTree 的最新稳定版本stable ,对应的版本为 0.12.8

docker-compose.yml

将下面的内容保存为 docker-compose.yml 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
version: "3.8"

# Docker compose recipe for a production-ready InvenTree setup, with the following containers:
# - PostgreSQL as the database backend
# - gunicorn as the InvenTree web server
# - django-q as the InvenTree background worker process
# - nginx as a reverse proxy
# - redis as the cache manager (optional, disabled by default)

# ---------------------
# READ BEFORE STARTING!
# ---------------------

# -----------------------------
# Setting environment variables
# -----------------------------
# Shared environment variables should be stored in the env.txt file
# Changes made to this file are reflected across all containers!
#
# IMPORTANT NOTE:
# You should not have to change *anything* within this docker-compose.yml file!
# Instead, make any changes in the env.txt file!

# ------------------------
# InvenTree Image Versions
# ------------------------
# By default, this docker-compose script targets the STABLE version of InvenTree,
# image: inventree/inventree:stable
#
# To run the LATEST (development) version of InvenTree,
# change the INVENTREE_TAG variable (in the env.txt file) to "latest"
#
# Alternatively, you could target a specific tagged release version with (for example):
# INVENTREE_TAG=0.7.5
#

services:
# Database service
# Use PostgreSQL as the database backend
inventree-db:
image: postgres:13
container_name: inventree-db
expose:
- ${INVENTREE_DB_PORT:-5432}/tcp
environment:
- PGDATA=/var/lib/postgresql/data/pgdb
- POSTGRES_USER=${INVENTREE_DB_USER:?You must provide the 'INVENTREE_DB_USER' variable in the env.txt file}
- POSTGRES_PASSWORD=${INVENTREE_DB_PASSWORD:?You must provide the 'INVENTREE_DB_PASSWORD' variable in the env.txt file}
- POSTGRES_DB=${INVENTREE_DB_NAME:?You must provide the 'INVENTREE_DB_NAME' variable in the env.txt file}
volumes:
# Map 'data' volume such that postgres database is stored externally
- inventree_data:/var/lib/postgresql/data/
restart: unless-stopped

# redis acts as database cache manager
# only runs under the "redis" profile : https://docs.docker.com/compose/profiles/
inventree-cache:
image: redis:7.0
container_name: inventree-cache
depends_on:
- inventree-db
profiles:
- redis
env_file:
- .env
expose:
- ${INVENTREE_CACHE_PORT:-6379}
restart: always

# InvenTree web server service
# Uses gunicorn as the web server
inventree-server:
# If you wish to specify a particular InvenTree version, do so here
image: inventree/inventree:${INVENTREE_TAG:-stable}
container_name: inventree-server
# Only change this port if you understand the stack.
# If you change this you have to change:
# - the proxy settings (on two lines)
# - only change the exposed port - eg `1338:8000` if you want to expose the server on port 1338
expose:
- 8000
depends_on:
- inventree-db
env_file:
- .env
volumes:
# Data volume must map to /home/inventree/data
- inventree_data:/home/inventree/data
restart: unless-stopped

# Background worker process handles long-running or periodic tasks
inventree-worker:
# If you wish to specify a particular InvenTree version, do so here
image: inventree/inventree:${INVENTREE_TAG:-stable}
container_name: inventree-worker
command: invoke worker
depends_on:
- inventree-server
env_file:
- .env
volumes:
# Data volume must map to /home/inventree/data
- inventree_data:/home/inventree/data
restart: unless-stopped

# nginx acts as a reverse proxy
# static files are served directly by nginx
# media files are served by nginx, although authentication is redirected to inventree-server
# web requests are redirected to gunicorn
# NOTE: You will need to provide a working nginx.conf file!
inventree-proxy:
image: nginx
container_name: inventree-proxy
depends_on:
- inventree-server
env_file:
- .env
ports:
# Default web port is 1337 (can be changed in the env.txt file)
- ${INVENTREE_WEB_PORT:-1337}:80
volumes:
# Provide nginx configuration file to the container
# Refer to the provided example file as a starting point
- ./nginx.prod.conf:/etc/nginx/conf.d/default.conf:ro
# nginx proxy needs access to static and media files
- inventree_data:/var/www
restart: unless-stopped

volumes:
# Persistent data, stored external to the container(s)
inventree_data:
driver: local
driver_opts:
type: none
o: bind
# This directory specified where InvenTree data are stored "outside" the docker containers
device: ${INVENTREE_EXT_VOLUME:?You must specify the 'INVENTREE_EXT_VOLUME' variable in the env.txt file!}

相比官方的 docker-compose.yml,老苏做了 2 处修改,但都不是必须的,你可以直接用官方原版的

  • nginx 的版本,官方用的是 nginx:stable,老苏为了少下载一个镜像,改为了 nginx,也就是 nginx:latest,因为机器上已经有了,这个不是必须要改的;
  • 给每个容器增加了 container_name,只是为了看着舒服,同样也不是必须的;

nginx.prod.conf

将下面的内容保存为 r-compose.yml 文件,不需要做任何改动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
server {

# Listen for connection on (internal) port 80
# If you are exposing this server to the internet, you should use HTTPS!
# In which case, you should also set up a redirect from HTTP to HTTPS, and listen on port 443
# See the Nginx documentation for more details
listen 80;

real_ip_header proxy_protocol;

location / {

proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-By $server_addr:$server_port;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header CLIENT_IP $remote_addr;

proxy_pass_request_headers on;

proxy_redirect off;

client_max_body_size 100M;

proxy_buffering off;
proxy_request_buffering off;

# Do not touch this unless you have a specific reason - this and the docker-compose need to match
proxy_pass http://inventree-server:8000;
}

# Redirect any requests for static files
location /static/ {
alias /var/www/static/;
autoindex on;

# Caching settings
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}

# Redirect any requests for media files
location /media/ {
alias /var/www/media/;

# Media files require user authentication
auth_request /auth;

# Content header to force download
add_header Content-disposition "attachment";
}

# Use the 'user' API endpoint for auth
location /auth {
internal;

proxy_pass http://inventree-server:8000/auth/;

proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
}

}

.env

将下面的内容保存为 .env 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# InvenTree environment variables for a postgresql production setup

# Location of persistent database data (stored external to the docker containers)
# Note: You *must* un-comment this line, and point it to a path on your local machine

# e.g. Linux
INVENTREE_EXT_VOLUME=/volume1/docker/inventree/data

# e.g. Windows (docker desktop)
#INVENTREE_EXT_VOLUME=c:/Users/me/inventree-data

# Default web port for the InvenTree server
INVENTREE_WEB_PORT=1337

# Ensure debug is false for a production setup
INVENTREE_DEBUG=False
INVENTREE_LOG_LEVEL=WARNING

# InvenTree admin account details
# Un-comment (and complete) these lines to auto-create an admin acount
INVENTREE_ADMIN_USER=laosu
INVENTREE_ADMIN_PASSWORD=123456
INVENTREE_ADMIN_EMAIL=wbsu2003@gmail.com

# Database configuration options
# Note: The example setup is for a PostgreSQL database
INVENTREE_DB_ENGINE=postgresql
INVENTREE_DB_NAME=inventree
INVENTREE_DB_HOST=inventree-db
INVENTREE_DB_PORT=5432

# Database credentials - These must be configured before running
# Uncomment the lines below, and change from the default values!
INVENTREE_DB_USER=pguser
INVENTREE_DB_PASSWORD=pgpassword

# Redis cache setup (disabled by default)
# Un-comment the following lines to enable Redis cache
# Note that you will also have to run docker-compose with the --profile redis command
# Refer to settings.py for other cache options
#INVENTREE_CACHE_HOST=inventree-cache
#INVENTREE_CACHE_PORT=6379

# Options for gunicorn server
INVENTREE_GUNICORN_TIMEOUT=90

# Enable custom plugins?
INVENTREE_PLUGINS_ENABLED=False

# Run migrations automatically?
INVENTREE_AUTO_UPDATE=False

# Image tag that should be used
INVENTREE_TAG=stable

COMPOSE_PROJECT_NAME=inventree-production

参数说明:

  • 基本设置
    • INVENTREE_EXT_VOLUME:映射为卷的本地目录,需要根据自己的目录进行修改;
    • INVENTREE_WEB_PORT:访问时的 Web 端口,本地不冲突就可以;
    • INVENTREE_DEBUG:调试模式,默认 False 为关闭;
    • INVENTREE_LOG_LEVEL:日志级别;

【建议】:前两项需要根据自己的情况就行修改;

  • 管理员设置
    • INVENTREE_ADMIN_USER:管理员账号;
    • INVENTREE_ADMIN_PASSWORD:管理员密码;
    • INVENTREE_ADMIN_EMAIL:管理员邮件地址;

【建议】:三项都需要根据自己的情况进行修改;这里如果不设置,就需要用命令行去单独创建管理员;

  • 数据库设置
    • INVENTREE_DB_ENGINE:数据库类型,除了 postgresql 外,还支持 mysql
    • INVENTREE_DB_NAME:数据库库名;
    • INVENTREE_DB_HOST:数据库主机;
    • INVENTREE_DB_PORT:数据库端口;
    • INVENTREE_DB_USER:数据库用户;
    • INVENTREE_DB_PASSWORD:数据库密码;

【建议】:只要修改密码就可以的,其他的不建议修改;

  • Redis 设置
    • INVENTREE_CACHE_HOSTRedis 主机,默认本行被注释,在数据库初始化之前,一定不要取消注释;
    • INVENTREE_CACHE_PORTRedis 端口,默认本行被注释,在数据库初始化之前,一定不要取消注释;

【强烈建议】:不管你后续是否要使用Redis在数据库初始化之前,一定不要取消注释,否则会报错的,切记!切记!

  • 其他
    • INVENTREE_GUNICORN_TIMEOUT:服务器超时设置;
    • INVENTREE_PLUGINS_ENABLED:是否启用自定义插件;
    • INVENTREE_TAG:镜像的 tag 的版本,不建议修改;
    • COMPOSE_PROJECT_NAME:默认就可以;

【建议】:保持默认就可以了;

更多的参数说明,请看官方文档:https://docs.inventree.org/en/stable/start/config/

以上建议是给和老苏一样的小白用户的,高手请忽略

命令行安装

上传文件

文件准备好之后,依次执行下面的命令

1
2
3
4
5
6
7
# 新建文件夹 inventree 和 子目录
mkdir -p /volume1/docker/inventree/data

# 进入 inventree 目录
cd /volume1/docker/inventree

# 将 docker-compose.yml 、.env 、 nginx.prod.conf 放入当前目录

现在的目录

初始化

1
2
# 初始化数据库
docker-compose run inventree-server invoke update

这行初始化命令执行了以下步骤:

  • 确保安装了所需的 python
  • 创建一个新的(空)数据库
  • 执行所需的架构更新以创建所需的数据库表
  • 更新翻译文件
  • 将所有必需的静态文件收集到可由 nginx 提供服务的目录中

一定不要先注释 .env 中的 redis 设置,否则最后 Running InvenTree database migrations... 时会有错误

1
redis.exceptions.ConnectionError: Error -2 connecting to inventree-cache:6379. Name or service not known.

正常应该是下面这样的

创建管理员

  1. 已在 .env 中指定管理员帐户详细信息,可以跳过这一步;
  2. 为了安全起见,请确保在首次运行后从 .env 文件中删除管理员帐户凭据;
1
2
# 创建管理员账号
docker-compose run inventree-server invoke superuser

启动 Docker 容器

现在可以开始启动了

1
2
# 一键启动(不带 redis)
docker-compose up -d

如果需要 Redis 服务 ,首先修改 .env 中的 redis 设置,取消 INVENTREE_CACHE_HOSTINVENTREE_CACHE_PORT 前面的注释

如果你取消了 redis 注释,但是执行的又是不带 redis 的一键启动,也是会存在错误的,或导致容器不断重启;

然后再执行下面的启动命令

1
2
# 一键启动(带 redis)
docker-compose --profile redis up -d

不出意外的话,我们在 Docker 管理器中应该看到下面这样

inventree-production_inventree-server_run_2a703d49eef5 是初始化数据库的时候生成的,看着不爽的话,可以删掉

运行

在浏览器中输入 http://群晖IP:1337 就能看到登录界面

用我们前面设置的管理员账号、密码登录

主界面默认有部分中文显示,显然翻译程度还不高

不知道初始化时最一行显示的 InvenTree translation coverage: 27% 是不是指的中文翻译;

如果看到下面的提示,只要重启 inventree-server 容器即可

1
2
Server Restart Required
A configuration option has been changed which requires a server restart. Contact your system administrator for further information

其他

一些其他可能会用到的命令

1
2
3
4
5
6
7
8
# 一键删除
docker-compose down

# 将数据库导出为 JSON
docker-compose run inventree-server invoke export-records -f /volume1/docker/inventree/data/data.json

# 删除卷
docker volume rm -f inventree_data

参考文档

InvenTree
地址:https://github.com/inventree

InvenTree
地址:https://inventree.org

InvenTree - InvenTree Documentation
地址:https://docs.inventree.org

Docker Production Server - InvenTree Documentation
地址:https://docs.inventree.org/en/stable/start/docker_prod/