前言

由于Netbox 4.x 版本 官方的中文语言日渐完善,所以新出一个使用官方Docker源部署和升级的教程。

Netbox 系列文章:https://songxwn.com/categories/NetBox/

环境介绍

Rocky Linux 9.x (理论上也适用于RHEL系列的7-9版本)

南京大学镜像源ISO镜像下载:https://mirror.nju.edu.cn/rocky/9/isos/x86_64/Rocky-9-latest-x86_64-minimal.iso

环境配置

1
2
3
4
5
systemctl disable --now firewalld
sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config && setenforce 0
# 关闭防火墙和SELinux。
dnf install tree vim bash-completion tar git -y
# 安装一些工具,用于之后的部署

Docker-CE 环境安装

1
2
3
4
yum install -y yum-utils
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sed -i 's+https://download.docker.com+https://mirrors.tuna.tsinghua.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo
yum install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

参考清华大学源:https://mirrors.tuna.tsinghua.edu.cn/help/docker-ce/

Docker国内镜像加速器配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
sudo mkdir -p /etc/docker
# 创建文件夹
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": [
"https://docker.1ms.run",
"https://docker.1panel.live",
"https://docker.m.ixdev.cn",
"https://hub.rat.dev",
"https://docker.xuanyuan.me"
]
}
EOF
# 指定镜像源
sudo systemctl daemon-reload
sudo systemctl restart docker
# 重载重启后生效
docker info | grep https
# 验证
docker pull hello-world
# 拉取镜像验证

PS: 或者参考 https://songxwn.com/cf-works-DockerHub-Proxy/ 自行搭建

Netbox部署

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cd /opt
git clone -b release https://github.com/netbox-community/netbox-docker.git
# git获取官方库,国内可使用git clone -b release https://gitee.com/songxwn/netbox-docker.git
cd /opt/netbox-docker
tee docker-compose.override.yml <<EOF
services:
netbox:
ports:
- 8000:8080
EOF
# 创建端口映射规则文件,使用8000端口对外访问
docker compose pull
# 拉取镜像
docker compose up -d
# 启动镜像,第一次会比较久
docker compose logs netbox
# 查看日志,确认状态

创建用户(需要输入账号、邮箱和两次密码。密码要求大于12位)

1
2
docker compose exec netbox /opt/netbox/netbox/manage.py createsuperuser
## 等容器启动完成后,创建后可访问 8000端口进行登录。

配置Nginx 作为反向代理 (可选)

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
dnf install nginx -y
# 安装Nginx
vim /etc/nginx/conf.d/netbox.conf
# 创建配置文件,注意修改netbox.songxwn.com 为自己的域名。反向代理到8000端口,如果不是此端口也需要自己修改。
# 修改的域名,需要解析IP到netbox服务器。
server {
listen 80;
# CHANGE THIS TO YOUR SERVER'S NAME
server_name netbox.songxwn.com;
client_max_body_size 25m;
fastcgi_connect_timeout 1200s;
fastcgi_send_timeout 1200s;
fastcgi_read_timeout 1200s;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
location /static/ {
alias /opt/netbox/netbox/static/;
}
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
}
}
systemctl enable --now nginx
# 配置启动并开机启动
systemctl status nginx
# 查看状态

Netbox 升级

先修改 docker-compose.yml 文件中的netbox拉去版本为latest。

例如⬇️ 修改第三行即可:

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
services:
netbox: &netbox
image: netboxcommunity/netbox:latest
depends_on:
- postgres
- redis
- redis-cache
env_file: env/netbox.env
user: "netbox:root"
healthcheck:
test: curl -f http://localhost:8080/login/ || exit 1
start_period: 90s
timeout: 3s
interval: 15s
volumes:
- ./configuration:/etc/netbox/config:z,ro
- netbox-media-files:/opt/netbox/netbox/media:rw
- netbox-reports-files:/opt/netbox/netbox/reports:rw
- netbox-scripts-files:/opt/netbox/netbox/scripts:rw
netbox-worker:
<<: *netbox
depends_on:
netbox:
condition: service_healthy
command:
- /opt/netbox/venv/bin/python
- /opt/netbox/netbox/manage.py
- rqworker
healthcheck:
test: ps -aux | grep -v grep | grep -q rqworker || exit 1
start_period: 20s
timeout: 3s
interval: 15s

# postgres
postgres:
image: docker.io/postgres:18-alpine
healthcheck:
test: pg_isready -q -t 2 -d $$POSTGRES_DB -U $$POSTGRES_USER
start_period: 20s
timeout: 30s
interval: 10s
retries: 5
env_file: env/postgres.env
volumes:
- netbox-postgres:/var/lib/postgresql

# redis
redis:
image: docker.io/valkey/valkey:9.1-alpine
command:
- sh
- -c # this is to evaluate the $REDIS_PASSWORD from the env
- valkey-server --appendonly yes --requirepass $$REDIS_PASSWORD ## $$ because of docker-compose
healthcheck: &redis-healthcheck
test: '[ $$(valkey-cli --pass "$${REDIS_PASSWORD}" ping) = ''PONG'' ]'
start_period: 5s
timeout: 3s
interval: 1s
retries: 5
env_file: env/redis.env
volumes:
- netbox-redis-data:/data
redis-cache:
image: docker.io/valkey/valkey:9.1-alpine
command:
- sh
- -c # this is to evaluate the $REDIS_PASSWORD from the env
- valkey-server --requirepass $$REDIS_PASSWORD ## $$ because of docker-compose
healthcheck: *redis-healthcheck
env_file: env/redis-cache.env
volumes:
- netbox-redis-cache-data:/data

volumes:
netbox-media-files:
driver: local
netbox-postgres:
driver: local
netbox-redis-cache-data:
driver: local
netbox-redis-data:
driver: local
netbox-reports-files:
driver: local
netbox-scripts-files:
driver: local


然后执行下面操作。

1
2
3
4
5
6
cd /opt/netbox-docker
docker compose pull
# 拉最新镜像
docker compose down
docker compose up -d
# 以最新镜像重新启动

技术交流群

发送邮件到 ➡️ me@songxwn.com

或者关注WX公众号:网工格物

微信扫码

博客(最先更新)

https://songxwn.com/