Docker如何启动SuperSet实现报表功能

发布于:2025-02-28 ⋅ 阅读:(15) ⋅ 点赞:(0)

目录

1 拉取Mysql和Redis镜像

2 启动MySQL和redis容器

3 拉去superset镜像

4 创建自定义配置文件

5 配置superset容器

5.1 创建数据库

5.2 初始化数据库

5.3 创建管理员用户

5.4 初始化superset

5.5 访问url


1 拉取Mysql和Redis镜像

sudo docker pull mysql
sudo docker pull redis

2 启动MySQL和redis容器

因为需要在同一个网络下访问,所以需要创建一个docker网络

docker network create superset

创建Mysql配置文件

sudo mkdir -p /temp/mysql/data
sudo mkdir -p /temp/mysql/conf

Mysql启动命令

sudo docker run -p 3306:3306 \
  --name mysql \
  --network superset \
  -v /temp/mysql/data:/var/lib/mysql \
  -e MYSQL_ROOT_PASSWORD=123456 \
  -d mysql:latest

创建Redis配置文件

sudo mkdir -p /temp/redis/conf
sudo mkdir -p /temp/redis/data 

百度自寻下载配置文件redis.conf放到/temp/redis/conf该目录下

redis启动命令

docker run -p 6379:6379 \
  --name redis \
  --network your_network_name \
  -v /temp/redis/conf/redis.conf:/etc/redis/redis.conf \
  -v /temp/redis/data:/data \
  -d redis:latest \
  redis-server /etc/redis/redis.conf

3 拉去superset镜像

docker search superset
#这里使用社区对象 因为官方对象总是出错
docker pull amancevice/superset 

4 创建自定义配置文件

sudo vim /temp/docker/superset/superset_config.py

superset_config.py配置文件如下:

#Superset specific config
ROW_LIMIT = 5000
​
SUPERSET_WEBSERVER_PORT = 8088
​
# Flask App Builder configuration
# Your App secret key will be used for securely signing the session cookie
# and encrypting sensitive information on the database
# Make sure you are changing this key for your deployment with a strong key.
# Alternatively you can set it with `SUPERSET_SECRET_KEY` environment variable.
# You MUST set this for production environments or the server will not refuse
# to start and you will see an error in the logs accordingly.
#这里使用openssl rand -base64 42生成自己的密钥
SECRET_KEY = '9c6z9gay6TsaG1lTRnIHeHkuy6G7OzsqGm/mqAsbS5Xcs+N5Bp1VcBPg'
​
# The SQLAlchemy connection string to your database backend
# This connection defines the path to the database that stores your
# superset metadata (slices, connections, tables, dashboards, ...).
# Note that the connection information to connect to the datasources
# you want to explore are managed directly in the web UI
# The check_same_thread=false property ensures the sqlite client does not attempt
# to enforce single-threaded access, which may be problematic in some edge cases
​
SQLALCHEMY_DATABASE_URI = 'mysql://root:123456@mysql:3306/superset'
SQLALCHEMY_TRACK_MODIFICATIONS = False  # 禁用修改追踪,减少性能开销
​
​
# Flask-WTF flag for CSRF
WTF_CSRF_ENABLED = True
# Add endpoints that need to be exempt from CSRF protection
WTF_CSRF_EXEMPT_LIST = []
# A CSRF token that expires in 1 year
WTF_CSRF_TIME_LIMIT = 60 * 60 * 24 * 365
​
LANGUAGES = { 'en': {'flag': 'us', 'name': 'English'}, 'zh': {'flag': 'cn', 'name': 'Chinese'}, }
​
# Set this API key to enable Mapbox visualizations
MAPBOX_API_KEY = ''
sudo mkdir -p /temp/docker/superset/data

6 启动镜像

docker run \
  --name superset \  # 设置容器名称为 superset
  -d \  # 在后台运行容器
  -p 8088:8088 \  # 映射宿主机的 8088 端口到容器的 8088 端口
  --network superset \  # 指定容器使用的 Docker 网络为 superset
  -v /temp/docker/superset/superset_config.py:/etc/superset/superset_config.py \  # 将宿主机的配置文件挂载到容器内
  -v /temp/docker/superset/data:/var/lib/superset \  # 将宿主机的数据目录挂载到容器内
  amancevice/superset  # 使用 amancevice/superset 镜像启动容器

查看容器运行状态

docker ps

5 配置superset容器

5.1 创建数据库

docker exec -it mysql mysql -u root -p123456 -e "DROP DATABASE IF EXISTS superset;"
docker exec -it mysql mysql -u root -p123456 -e "CREATE DATABASE superset;"

5.2 初始化数据库

docker exec -it superset superset db upgrade

5.3 创建管理员用户

docker exec -it superset superset fab create-admin

5.4 初始化superset

docker exec -it superset superset init 

5.5 访问url

浏览器输入127.0.0.1:8088即可访问

测试的数据库文件如下

CREATE TABLE phone_sales (
    id INT AUTO_INCREMENT PRIMARY KEY,
    date DATE NOT NULL,
    phone_type VARCHAR(255) NOT NULL,
    quantity_sold INT NOT NULL,
    price DECIMAL(10, 2) NOT NULL,
    total_sales DECIMAL(10, 2) AS (quantity_sold * price) STORED
);
INSERT INTO phone_sales (date, phone_type, quantity_sold, price)
VALUES
('2025-02-01', 'iPhone 14', 10, 799.99),
('2025-02-01', 'Samsung Galaxy S21', 15, 699.99),
('2025-02-01', 'OnePlus 9', 12, 649.99),
('2025-02-01', 'Google Pixel 6', 8, 599.99),
('2025-02-01', 'iPhone 13', 20, 699.99),
('2025-02-02', 'iPhone 14', 14, 799.99),
('2025-02-02', 'Samsung Galaxy S21', 18, 699.99),
('2025-02-02', 'OnePlus 9', 10, 649.99),
('2025-02-02', 'Google Pixel 6', 6, 599.99),
('2025-02-02', 'iPhone 13', 22, 699.99),
('2025-02-03', 'iPhone 14', 13, 799.99),
('2025-02-03', 'Samsung Galaxy S21', 20, 699.99),
('2025-02-03', 'OnePlus 9', 14, 649.99),
('2025-02-03', 'Google Pixel 6', 7, 599.99),
('2025-02-03', 'iPhone 13', 18, 699.99),
('2025-02-04', 'iPhone 14', 16, 799.99),
('2025-02-04', 'Samsung Galaxy S21', 12, 699.99),
('2025-02-04', 'OnePlus 9', 9, 649.99),
('2025-02-04', 'Google Pixel 6', 5, 599.99),
('2025-02-04', 'iPhone 13', 24, 699.99),
('2025-02-05', 'iPhone 14', 10, 799.99),
('2025-02-05', 'Samsung Galaxy S21', 17, 699.99),
('2025-02-05', 'OnePlus 9', 13, 649.99),
('2025-02-05', 'Google Pixel 6', 6, 599.99),
('2025-02-05', 'iPhone 13', 20, 699.99),
('2025-02-06', 'iPhone 14', 18, 799.99),
('2025-02-06', 'Samsung Galaxy S21', 14, 699.99),
('2025-02-06', 'OnePlus 9', 11, 649.99),
('2025-02-06', 'Google Pixel 6', 7, 599.99),
('2025-02-06', 'iPhone 13', 25, 699.99),
('2025-02-07', 'iPhone 14', 12, 799.99),
('2025-02-07', 'Samsung Galaxy S21', 16, 699.99),
('2025-02-07', 'OnePlus 9', 14, 649.99),
('2025-02-07', 'Google Pixel 6', 9, 599.99),
('2025-02-07', 'iPhone 13', 22, 699.99),
('2025-02-08', 'iPhone 14', 14, 799.99),
('2025-02-08', 'Samsung Galaxy S21', 19, 699.99),
('2025-02-08', 'OnePlus 9', 12, 649.99),
('2025-02-08', 'Google Pixel 6', 8, 599.99),
('2025-02-08', 'iPhone 13', 26, 699.99),
('2025-02-09', 'iPhone 14', 15, 799.99),
('2025-02-09', 'Samsung Galaxy S21', 14, 699.99),
('2025-02-09', 'OnePlus 9', 10, 649.99),
('2025-02-09', 'Google Pixel 6', 5, 599.99),
('2025-02-09', 'iPhone 13', 23, 699.99),
('2025-02-10', 'iPhone 14', 17, 799.99),
('2025-02-10', 'Samsung Galaxy S21', 13, 699.99),
('2025-02-10', 'OnePlus 9', 13, 649.99),
('2025-02-10', 'Google Pixel 6', 6, 599.99),
('2025-02-10', 'iPhone 13', 27, 699.99);