自编译 Transmission-daemon 的 Docker 镜像

狐
为了熟悉 Docker,就想着把 NAS 中的对外服务全部部署在 Docker 上,一是方便以后在其它地方部署,二是在 Docker 中运行服务要比本机相对安全一些。

背景

为了以后方便部署,这里我们使用 Dockerfile 文件来在本地直接生成 Docker 镜像。

在开源界,流行这么一条安装的软件越少就越安全的理论,虽说不完全正确,但在大部分环境中它还是成立的。
依据上面的理论,我们就不选择体积较大的 Debian/Ubuntu 镜像(80+MB)了,转而使用体积小巧的 alpine 镜像(4MB)。

本文所需的文件已发布在Github-haven200

使用hub-docker的镜像服务器

国内用户,为了提高 Docker 基础镜像的拉取速度,这里我们使用 Azure 的镜像服务。

1
2
3
4
5
:~$ cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://dockerhub.azk8s.cn"]
}
EOF && systemctl restart docker

Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
FROM alpine:latest

MAINTAINER haven200 <haven200@gmail.com>

WORKDIR /opt/src

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/' /etc/apk/repositories \
&& apk update && apk add --no-cache transmission-daemon \
&& mkdir -p /etc/transmission-daemon && mkdir -p /mnt/bt/kodi

COPY run.sh /opt/src/run.sh

EXPOSE 12345/tcp 12345/udp 80/tcp

VOLUME [ "/mnt/bt/kodi" ]

CMD [ "/bin/sh","/opt/src/run.sh" ]
HEALTHCHECK CMD netstat -ln | grep 12345 || exit 1
  • FROM:以哪个镜像为基础。
  • MAINTAINER:此镜像的维护者。
  • WORKDIR:镜像的当前默认路径。
  • RUN:构建 Docker 的make指令。
  • COPY:将本地的文件复制到镜像的指定位置
  • EXPOSE:镜像中对外提供服务的端口。
  • VOLUME:需要保存文件的目录。
  • CMD:最终运行指令。
  • HEALTHCHECK:检查Docker镜像是否正常运行,如果出现错误则停止运行。

run.sh

Transmssion-Deamon 的配置选项全在此文件里,编译前可以自行修改。

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
#!/bin/bash

set -e

CONFIG_DIR=/etc/transmission-daemon
SETTINGS=$CONFIG_DIR/settings.json
TRANSMISSION=/usr/bin/transmission-daemon

GATEWAY_IP="$(ip route | grep default | awk '{print $3}')"

if [ -z $USERNAME ]; then
USERNAME="Transmission"
fi
if [ -z $PASSWORD ]; then
PASSWORD="password"
fi
if [ -z $RPC_PORT ]; then
RPC_PORT=80
fi

cat > $SETTINGS <<EOF
{
"bind-address-ipv4": "0.0.0.0",
"bind-address-ipv6": "::",
"download-dir": "/mnt/bt/kodi",
"download-limit": 100,
"download-limit-enabled": 0,
"incomplete-dir": "/mnt/bt/kodi/.unfinished",
"incomplete-dir-enabled": true,
"preallocation": 2,
"prefetch-enabled": true,
"start-added-torrents": false,
"umask": 91,
"watch-dir": "/mnt/bt/kodi/torrents/",
"watch-dir-enabled": true,
"trash-original-torrent-files": false,
"cache-size-mb": 128,
"dht-enabled": true,
"lpd-enabled": false,
"message-level": 2,
"encryption": 1,
"script-torrent-done-enabled": false,
"script-torrent-done-filename": "",
"utp-enabled": true,
"idle-seeding-limit": 30,
"idle-seeding-limit-enabled": true,
"max-peers-global": 200,
"message-level": 1,
"peer-congestion-algorithm": "",
"peer-id-ttl-hours": 6,
"peer-limit-global": 200,
"peer-limit-per-torrent": 60,
"peer-port": 12345,
"peer-port-random-on-start": false,
"peer-port-random-high": 65535,
"peer-port-random-low": 49152,
"peer-socket-tos": "default",
"pex-enabled": true,
"download-queue-enabled": true,
"download-queue-size": 3,
"queue-stalled-enabled": true,
"queue-stalled-minutes": 30,
"port-forwarding-enabled": true,
"ratio-limit": 1,
"ratio-limit-enabled": true,
"rename-partial-files": true,
"rpc-authentication-required": true,
"rpc-enabled": true,
"rpc-port": $RPC_PORT,
"rpc-bind-address": "0.0.0.0",
"rpc-host-whitelist": "",
"rpc-host-whitelist-enabled": true,
"rpc-url": "/transmission/",
"rpc-username": "$USERNAME",
"rpc-password": "$PASSWORD",
"rpc-whitelist": "127.0.0.1,$GATEWAY_IP",
"rpc-whitelist-enabled": true,
"scrape-paused-torrents-enabled": true,
"script-torrent-done-enabled": false,
"script-torrent-done-filename": "",
"seed-queue-enabled": false,
"seed-queue-size": 10,
"speed-limit-down": 2048,
"speed-limit-down-enabled": true,
"speed-limit-up": 500,
"speed-limit-up-enabled": true,
"upload-limit": 100,
"upload-limit-enabled": 0,
"upload-slots-per-torrent": 14,
"alt-speed-down": 10240,
"alt-speed-enabled": false,
"alt-speed-time-begin": 0,
"alt-speed-time-day": 127,
"alt-speed-time-enabled": true,
"alt-speed-time-end": 360,
"alt-speed-up": 2048,
"blocklist-enabled": false,
"blocklist-url": "http://www.example.com/blocklist"
}
EOF

exec $TRANSMISSION -f --config-dir $CONFIG_DIR

编译 Docker 镜像

在 Dockerfile 的目录运行下面的命令

1
docker build -t haven200/transmission-daemon .

运行镜像

1
2
3
4
5
6
docker run -d --name transmission-daemon \
-p 12345:12345/tcp \
-p 12345:12345/udp \
-p 80:80 \
-v /mnt/bt:/mnt/bt/kodi \
haven200/transmission-daemon

配置 Web 管理页面用户名及密码

1
2
3
4
5
6
7
8
docker run -d --name transmission-daemon \
-p 12345:12345/tcp \
-p 12345:12345/udp \
-p 80:8080 \
-v /mnt/bt:/mnt/bt/kodi \
--env USERNAME=myname \
--env PASSWORD=mypassword \
haven200/transmission-daemon
  • USERNAME 变量为登录的用户名
  • PASSWORD 变量为登录的密码

References: