Preface
A while ago, I wrote an article:Docker Series: Building a Simple Video Gallery Based on WordPress + MinIO, but I always felt it wasn't quite right. First, the playback experience was not very good—clicking the image directly wouldn't play it, you had to click the text link; second, uploading a video was quite a hassle, and I had to manually specify thumbnails... In short, it felt more and more "low"! So I thought about it and decided to just deploy a separate video sharing platform to store the videos.
After some research, I chose PeerTube. PeerTube is an open-source, distributed, and federated video platform, similar to YouTube, but decentralized. It uses WebTorrent (the first Torrent client running in the browser) technology to implement a P2P network to reduce the load on a single server. Anyone can set up PeerTube on a server and share uploaded videos.
Preparation
PeerTube deployment is a bit complex as it depends on PostgreSQL (>= 9.6) and Redis (>= 2.8.18), so PostgreSQL and Redis need to be prepared first. PostgreSQL also needs to be initialized. This time, I will no longer use docker-compose, otherwise, would I have to pull countless PostgreSQL instances in the future? This time, I will use the docker run command for deployment, which is also to facilitate future reuse. The official initialization file .env has already been provided, and the link is as follows:Official env file, which contains both the initialization information for PostgreSQL and the initial information for PeerTube. Since I am not using the docker-compose command, I have to translate it into docker run commands myself. The link to the official docker-compose yml file is as follows:Official yml file, but the parameter descriptions on it are not complete. For the complete ones, you can refer to:Official Complete Custom Parameter Description, you can download it yourself if needed.
Create bridge
Since a bridge named public-net will be used below, it needs to be created in advance:
docker network create public-net
Deploy PostgreSQL
Create directory:
mkdir -p /docker/postgres/data
Create postgres container:
docker run --name postgres -d --restart=always --net=public-net \
-p 5432:5432 \ #映射宿主机端口5432到容器的5432端口
-e POSTGRES_USER=root \ #设置postgres的登录用户名
-e POSTGRES_PASSWORD=123456 \ #设置postgres的登录密码
-e POSTGRES_DB=peertube \ #添加peertube对应的库
-e PGDATA=/var/lib/postgresql/data/pgdata \ #指定包含数据的子目录
-v /docker/postgres/data:/var/lib/postgresql/data \ #指定容器数据目录在宿主机的实际挂载路径
postgres:15.4
When using the above commands, please delete the comment content after # yourself.
-e PGDATA
Official description: “This optional variable can be used to define another location (such as a subdirectory) for the database files. The default location is /var/lib/postgresql/data. If the data volume used is a filesystem mount point (like a GCE persistent disk), or a remote folder that cannot be owned by the postgres user (like some NFS mounts), or contains folders/files (like lost+found), Postgres initdb needs to create a subdirectory containing data within the mount point.”
In plain English, it means the -v parameter cannot directly map the files inside the container/var/lib/postgresql/dataout; you must first specify a subdirectory under it as the data directory. This really tripped me up; the container wouldn't start at first, and I only figured it out after looking at the official parameters for a long time.
Deploy redis
Create directory:
mkdir -p /docker/redis/data
Create redis container:
docker run --name redis -d --restart=always --net=public-net \
-p 6379:6379 \ #映射宿主机6379端口到容器的6379端口
-v /docker/redis/data:/data #指定容器数据目录在宿主机的实际挂载路径
redis
Deploy PeerTube
Create directory:
mkdir -p /docker/redis/data
Create PeerTube:
docker run --name peertube -d --restart=always --net=public-net \
-p 1935:1935 -p 9500:9000 \ #映射宿主机的1935端口和9500端口到容器的1935和9000,大家根据自己的实际情况修改即可,1395端口是rmtp协议的默认端口
-e PEERTUBE_DB_HOSTNAME=postgres \ #指定postgres数据库的地址
-e PEERTUBE_DB_USERNAME=root \ #指定postgres数据库的登录用户名
-e PEERTUBE_DB_PASSWORD=123456 \ #指定postgres数据库的登录用户名密码
-e PEERTUBE_DB_NAME=peertube \ #指定peertube在postgres数据库上使用的库名字
-e PEERTUBE_DB_SSL=false \ #指定连接数据库是否使用SSL
-e PEERTUBE_DB_PORT=5432 \ #指定连接数据库使用的端口
-e PEERTUBE_REDIS_HOSTNAME=redis \ #指定redis数据库地址
-e PEERTUBE_REDIS_PORT=6379 \ #指定连接redis数据库使用的端口
-e PEERTUBE_WEBSERVER_HOSTNAME=video.example.com \ #指定peertube发布使用的域名,这个必须在开始部署之前就确定,一旦指定了就不能更改,最好是备过案的有443端口的域名,如果没备案的云主机或者是家庭宽带,就只能使用非标端口了
-e PEERTUBE_WEBSERVER_PORT=55555 \ #指定客户端访问peertube的实际端口,例如通过有公网IP的家宽非标准端口发布而直接访问,就是路由器上的映射端口(比如本文中的55555);如果是像我一样,家宽作为源站,实际放在CDN上,则这里就要改成443端口
-e PEERTUBE_WEBSERVER_HTTPS=true \ #指定发布是否使用https
-v /docker/peertube/data:/data \ #指定容器目录在宿主机真实路径的挂载点
-v /docker/peertube/config:/config \
-v assets:/app/client/dist \
-e PEERTUBE_SECRET=xxxxxxx \ #找个linux主机在终端里运行openssl rand -hex 32获取
-e PEERTUBE_SMTP_USERNAME=abc \ #主要不要加邮箱后缀,后面其他SMTP的设置按照你使用的邮件供应商的实际信息填写即可
-e PEERTUBE_SMTP_PASSWORD=xxxxx \ #你的邮件提供商生成的授权码,非邮箱登录密码
-e PEERTUBE_SMTP_HOSTNAME=smtp.163.com \
-e PEERTUBE_SMTP_PORT=25 \
-e [email protected] \
-e PEERTUBE_SMTP_TLS=false \
-e PEERTUBE_SMTP_DISABLE_STARTTLS=false \
-e [email protected] \ #指定管理员邮箱地址,这个很重要
-e PEERTUBE_SIGNUP_ENABLED=true \ #是否支持注册
chocobozzz/peertube:v5.2.1-bullseye
(Optional) Use in conjunction with COS system
Earlier I wrote an article:Docker Series: Building a MinIO-based Video COS Sharing Platform, which can be used right here. Combined with PeerTube, uploaded videos can be stored directly in your own self-built COS system. This is a step up in coolness compared to uploading directly to PeerTube. If you are interested, you can give it a try.
-e PEERTUBE_OBJECT_STORAGE_ENABLED=true #是否支持对象存储COS
-e PEERTUBE_OBJECT_STORAGE_ENDPOINT= #对象存储的网址,只填域名,默认为https
-e PEERTUBE_OBJECT_STORAGE_REGION= #对象存储的区域,有则填,没以后则不填
-e PEERTUBE_OBJECT_STORAGE_CREDENTIALS_ACCESS_KEY_ID=xxxx #对象存储的access-key-id
-e PEERTUBE_OBJECT_STORAGE_CREDENTIALS_SECRET_ACCESS_KEY=xxxxx #对象存储的secret-key
-e PEERTUBE_OBJECT_STORAGE_STREAMING_PLAYLISTS_BUCKET_NAME= #串流对象存放的桶名称
-e PEERTUBE_OBJECT_STORAGE_STREAMING_PLAYLISTS_PREFIX=streaming #串流存放的桶子目录,如果这个和上传视频存放位置不是一个桶,则可以不填
-e PEERTUBE_OBJECT_STORAGE_VIDEOS_BUCKET_NAME= #上传视频存放的桶名称
-e PEERTUBE_OBJECT_STORAGE_VIDEOS_PREFIX= #上传视频存放的桶子目录,如果和串流存放的不是一个桶,这个可以不填
If you want to use it with a reverse proxy and enable HTTPS, you need to make the corresponding configurations in advance so that it can be used tohttps://video.example.com:55555access PeerTube. For the specific method of configuring the reverse proxy, you can refer to my other two articles:Linux Panel Series: Configuring Reverse Proxy and Publishing Using Non-443 Ports和Docker Series: Building Your Own Reverse Proxy Based on NPM Using Docker, remember to modify the parameters in the Nginx configurationclient_max_body_size 50m, which defaults to 50MB. Your uploaded videos cannot exceed 50MB in size, so change it to the value you need.
Just to vent a bit, this is truly the most complex container deployment I have ever encountered. If it were deployed using Docker Compose, it would be much simpler, because actually many of the environment variables above have default values, such as Redis. Because my PeerTube deployment kept failing, I had to go to the official website to find the complete parameters and use the elimination method to find which variable was causing the issue, which is why I wrote so much... Now I understand why the official documentation doesn't even mention the command to run using docker run; they probably just couldn't be bothered, so they directly provide an env environment file in the yml file to be used for several containers at the same time. It's just because I wanted to write everything down in a single txt file that I had to slowly try it myself. I guess I'm the only one on the entire internet using docker run to do this.
Since PeerTube's complete functional chain also requires a website, SSL certificate, mail, and other functions, the official `docker-compose.yml` also includes webserver, certbot, and postfix containers. However, if you have an existing reverse proxy, certificate, and mailbox, you can directly use the above environment variables instead.
The most tricky environment variable isPEERTUBE_OBJECT_STORAGE_MAX_UPLOAD_PART和 PEERTUBE_TRUST_PROXY=, as long as these 2 parameters are added, the container won't start. In the end, I had to leave them out. It probably works fine when using docker compose, those interested can give it a try.
Although the process is quite a hassle, I feel that if you just need to set it up and don't care about the technical details, using docker compose is fine; but if you want to gain something from the technical details, you might as well use the docker run command more. After all, using the docker run command has an advantage: you only need a single txt file to back up all containers, a must-have for lazy people, haha.
Configure PeerTube
Log in
Do not directly usehttp://宿主机IP:9500the port method to access, otherwise an error will occur:

Cannot retrieve OAuth Client credentials: Getting client tokens for host 192.168.10.99:9500 is forbidden. Ensure you have correctly configured PeerTube (config/ directory), in particular the "webserver" section.The reason for the error is that I have already told PeerTube in the environment variables that it will be accessed using video.example.com, port 55555, and https, so the correct way to open it is to use
https://video.example.com:55555to log in.
Get root password
There are 3 ways to get the root password, here we mainly introduce 2 of them.
Obtain from the host mount path
Use vim to open the following file (if you have a graphical interface, you can also open it directly with a text editor):
vim /docker/peertube/data/logs/peertube.log
Directly type ”/root”, then press Enter to search for the word "root":

The blurred field inside the quotes after ”password”: is the root password.
Retrieve password using email
If the mail provider information configured earlier is correct, you can directly click ”I forgot my password” in the red box on the login page:

Enter the administrator email address set with the ”PEERTUBE_ADMIN_EMAIL” variable when creating the container earlier, and then click reset:

Then you will receive an email from PeerTube:

Directly click the link in the red box in the image above, and the following interface will appear:

Enter the new password, and then click the button in the red box in the image above to reset the password.
Note: If an error occurs when resetting the password, please add the following code to the reverse proxy configuration:
proxy_set_header X-Forwarded-Protocol https;
proxy_redirect http:// https://;
I also encountered a similar problem when deploying Chevereto, and solved it in the same way. This is still an issue caused by using http for the backend and https for the frontend. For the specific problem symptoms, please refer to:Docker Series: Solving the Issue of Chevereto Working Abnormally After Being Deployed Behind a Reverse Proxy。
Log in to the system using the root account
After clicking the login button in the upper left corner and logging into the system using the root account (or the administrator email address) and password, the following interface will appear:

Next, you can start configuring your PeerTube.
See also:Docker Series: Building a Private Video Sharing Platform Based on PeerTube (Part 2)