首先需要一台墙外的VPS,可以选择的服务提供商如:AWS、Bandwagon、Vultr、DigitalOcean等,这不是本文的重点,不再展开。

安装dockerdocker-compose

以Debian系Linux为例,其他请参照官方文档

1
2
apt-get update
apt-get install -y docker.io docker-compose

应用目录结构

1
2
3
4
5
v2ray
 |-- Dockerfile
 |-- docker-compose.yml
 |-- config
 |   |-- config.json (启动后生成)

创建文件

新建镜像构建文件Dockerfile,内容如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
FROM docker.io/alpine:latest

ARG V2RAY_VERSION=4.39.2
ENV PATH=$PATH:/opt/v2ray

RUN wget -O v2ray-linux-64.zip https://github.com/v2fly/v2ray-core/releases/download/v${V2RAY_VERSION}/v2ray-linux-64.zip &&\
    unzip v2ray-linux-64.zip -d /opt/v2ray &&\
    rm -f v2ray-linux-64.zip &&\
    mkdir /etc/v2ray
RUN printf "#!/bin/sh \n\
if [ ! -f /etc/v2ray/config.json ]; then \n  cp /opt/v2ray/config.json /etc/v2ray \nfi \n\
v2ray -config=/etc/v2ray/config.json" > entrypoint.sh && chmod +x entrypoint.sh

CMD ./entrypoint.sh

新建容器编排文件docker-compose.yml,内容如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
version: "2.2"

services:
  v2ray:
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    ports:
      - "${HTTP_PORT:-1080:1080}"
    volumes:
      - ./config:/etc/v2ray
      - ./log:/var/log/v2ray

运行

在应用目录下,执行docker-compose up -d --build来构建容器并启动
docker-compose restart重启
docker-compose down关闭

修改配置

在启动后将在应用目录下生成config/config.json配置文件,修改为以下内容后重启生效:

建议将inbounds->settings->clients->idUUIDa09d0fda-f8ed-4c39-9051-97c409af1823重新生成,避免泄漏。网上有很多在线生成UUID的工具,不再赘述。
默认端口号1080,修改之后记得同时更新docker-compose.yml文件的开放端口。
如需修改为VMessTrojan等其他协议,修改该配置文件即可。
具体请参考官方文档

  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
{
  "log" : {
    "access": "/var/log/v2ray/access.log",
    "error": "/var/log/v2ray/error.log",
    "loglevel": "warning"
  },
  "inbounds": [{
    "port": 1080,
    "listen": "0.0.0.0",
    "protocol": "vless",
    "settings": {
      "clients": [
        {
          "id": "a09d0fda-f8ed-4c39-9051-97c409af1823",
          "level": 1
        }
      ],
      "decryption": "none"
    },
    "streamSettings": {
      "network": "ws",
      "wsSettings": {
        "path": "/ws"
      }
    },
    "sniffing": {
      "enabled": true,
      "destOverride": [
        "http",
        "tls"
      ]
    }
  }],
  "outbounds": [
    {
      "protocol": "freedom",
      "settings": {}
    }
  ],
  "routing": {
    "domainStrategy": "IPOnDemand",
    "rules": [
      {
        "type": "field",
        "ip": [
          "0.0.0.0/8",
          "10.0.0.0/8",
          "100.64.0.0/10",
          "127.0.0.0/8",
          "169.254.0.0/16",
          "172.16.0.0/12",
          "192.0.0.0/24",
          "192.0.2.0/24",
          "192.168.0.0/16",
          "198.18.0.0/15",
          "198.51.100.0/24",
          "203.0.113.0/24",
          "::1/128",
          "fc00::/7",
          "fe80::/10"
        ],
        "outboundTag": "blocked"
      },
      {
        "type": "field",
        "inboundTag": [
          "tg-in"
        ],
        "outboundTag": "tg-out"
      },
      {
        "type": "field",
        "domain": [
          "domain:epochtimes.com",
          "domain:epochtimes.com.tw",
          "domain:epochtimes.fr",
          "domain:epochtimes.de",
          "domain:epochtimes.jp",
          "domain:epochtimes.ru",
          "domain:epochtimes.co.il",
          "domain:epochtimes.co.kr",
          "domain:epochtimes-romania.com",
          "domain:erabaru.net",
          "domain:lagranepoca.com",
          "domain:theepochtimes.com",
          "domain:ntdtv.com",
          "domain:ntd.tv",
          "domain:ntdtv-dc.com",
          "domain:ntdtv.com.tw",
          "domain:minghui.org",
          "domain:renminbao.com",
          "domain:dafahao.com",
          "domain:dongtaiwang.com",
          "domain:falundafa.org",
          "domain:wujieliulan.com",
          "domain:ninecommentaries.com",
          "domain:shenyun.com"
        ],
        "outboundTag": "blocked"
      },
      {
        "type": "field",
        "protocol": [
          "bittorrent"
        ],
        "outboundTag": "blocked"
      }
    ]
  }
}

后记

此时的V2Ray是没有TLS证书加密的。
为提高安全性,建议使用NginxApache+TLS证书反向代理至1080端口即可。
使用Let’s Encrypt申请免费证书

扩展阅读

本文GitHub传送门