跳至主要內容

Dokploy-Heroku、Vercel 和 Netlify 的开源替代品

程序员李某某大约 4 分钟

Dokploy-Heroku、Vercel 和 Netlify 的开源替代品

简介

官方文档 https://docs.dokploy.com/docs/coreopen in new window

部署

在线部署

需要外网下docker镜像

curl -sSL https://dokploy.com/install.sh | sh

手动安装

同样需要外网,可以修改环境变量

  • PORT——非常适合避免与其他服务发生冲突。
  • TRAEFIK_SSL_PORT - 如果您想对 SSL 使用不同的端口,请设置为另一个端口。
  • TRAEFIK_PORT - 如果您想为 Traefik 使用不同的端口,请设置为另一个端口。
  • ADVERTISE_ADDR-如果您想为 Swarm 使用不同的 IP 地址,请设置为另一个 IP 地址。
  • RELEASE_TAG - 设置为 dokploy docker hub 标签(latest、canary、feature 等)
#!/bin/bash
# 确保脚本以 root 身份运行
if [ "$(id -u)" != "0" ]; then
    echo "此脚本必须以 root 身份运行" >&2
    exit 1
fi
 
# 检查 Linux作系统(不是 macOS 或在 Docker 容器内)
if [ "$(uname)" = "Darwin" ]; then
    echo "此脚本必须在本机 Linux 主机上运行" >&2
    exit 1
fi
 
if [ -f /.dockerenv ]; then
    echo "此脚本必须在本机 Linux 主机上运行" >&2
    exit 1
fi
 
# 检查已占用的端口
if ss -tulnp | grep ':80 ' >/dev/null; then
    echo "错误:端口 80 已在使用" >&2
    exit 1
fi
 
if ss -tulnp | grep ':443 ' >/dev/null; then
    echo "错误:端口 443 已在使用" >&2
    exit 1
fi
 
# 检查命令是否存在
command_exists() {
  command -v "$@" > /dev/null 2>&1
}
 
# 如果未安装 Docker,则安装 Docker
if command_exists docker; then
  echo "Docker already installed"
else
  curl -sSL https://get.docker.com | sh
fi
 
# 初始化 Docker Swarm
docker swarm leave --force 2>/dev/null
 
    get_ip() {
        # 尝试获取 IPv4
        local ipv4=$(curl -4s https://ifconfig.io 2>/dev/null)
 
        if [ -n "$ipv4" ]; then
            echo "$ipv4"
        else
            # 尝试获取 IPv6
            local ipv6=$(curl -6s https://ifconfig.io 2>/dev/null)
            if [ -n "$ipv6" ]; then
                echo "$ipv6"
            fi
        fi
    }
 
 advertise_addr="${ADVERTISE_ADDR:-$(get_ip)}"
 
  docker swarm init --advertise-addr $advertise_addr
 
  if [ $? -ne 0 ]; then
      echo "错误:无法初始化 Docker Swarm" >&2
      exit 1
  fi
 
  docker network rm -f dokploy-network 2>/dev/null
  docker network create --driver overlay --attachable dokploy-network
 
  echo "Network created"
 
  mkdir -p /etc/dokploy
 
  chmod 777 /etc/dokploy
 
# 拉取和部署 Dokploy
docker pull dokploy/dokploy:latest
docker service create \
  --name dokploy \
  --replicas 1 \
  --network dokploy-network \
  --mount type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock \
  --mount type=bind,source=/etc/dokploy,target=/etc/dokploy \
  --publish published=3000,target=3000,mode=host \
  --update-parallelism 1 \
  --update-order stop-first \
  -e PORT=<Value For PORT eg(3000)> \
  -e TRAEFIK_SSL_PORT=<Value For SSL PORT eg(444)> \
  -e TRAEFIK_PORT=<VALUE FOR TRAEFIK HTTP PORT eg(81)> \
  -e ADVERTISE_ADDR=$advertise_addr \
  dokploy/dokploy:latest
 
# Output success message
GREEN="\033[0;32m"
YELLOW="\033[1;33m"
BLUE="\033[0;34m"
NC="\033[0m" # No Color
printf "${GREEN}恭喜,Dokploy 已安装! ${NC}\n"
printf "${BLUE}等待 15 秒,让服务器启动. ${NC}\n"
printf "${YELLOW}请访问 http://${advertise_addr}:3000 ${NC}\n\n"

离线部署

提前下载好镜像,版本必须是这个

docker pull postgres:16
docker pull redis:7
docker pull traefik:v3.1.2
docker pull dokploy/dokploy:latest

mkdir -p dokploy/images
docker save -o dokploy/images/postgres:16.tar postgres:16
docker save -o dokploy/images/redis:7.tar redis:7
docker save -o dokploy/images/traefik:v3.1.2.tar traefik:v3.1.2
docker save -o dokploy/images/dokploy:latest.tar dokploy/dokploy:latest
## 写入如下脚本
vim dokploy/install.sh
cd dokploy
chmod +x install.sh
install.sh

install.sh

#!/bin/bash

if [ "$(id -u)" != "0" ]; then
    echo "This script must be run as root" >&2
    exit 1
fi

# check if is Mac OS
if [ "$(uname)" = "Darwin" ]; then
    echo "This script must be run on Linux" >&2
    exit 1
fi

# check if is running inside a container
if [ -f /.dockerenv ]; then
    echo "This script must be run on Linux" >&2
    exit 1
fi

# check if something is running on port 80
if ss -tulnp | grep ':80 ' >/dev/null; then
    echo "Error: something is already running on port 80" >&2
    exit 1
fi

# check if something is running on port 443
if ss -tulnp | grep ':443 ' >/dev/null; then
    echo "Error: something is already running on port 443" >&2
    exit 1
fi

command_exists() {
    command -v "$@" > /dev/null 2>&1
}

if command_exists docker; then
    echo "Docker already installed"
else
    curl -sSL https://get.docker.com | sh
fi

docker swarm leave --force 2>/dev/null

get_ip() {
    # local ip=""
    
    # # Try IPv4 first
    # # First attempt: ifconfig.io
    # ip=$(curl -4s --connect-timeout 5 https://ifconfig.io 2>/dev/null)
    
    # # Second attempt: icanhazip.com
    # if [ -z "$ip" ]; then
    #     ip=$(curl -4s --connect-timeout 5 https://icanhazip.com 2>/dev/null)
    # fi
    
    # # Third attempt: ipecho.net
    # if [ -z "$ip" ]; then
    #     ip=$(curl -4s --connect-timeout 5 https://ipecho.net/plain 2>/dev/null)
    # fi

    # # If no IPv4, try IPv6
    # if [ -z "$ip" ]; then
    #     # Try IPv6 with ifconfig.io
    #     ip=$(curl -6s --connect-timeout 5 https://ifconfig.io 2>/dev/null)
        
    #     # Try IPv6 with icanhazip.com
    #     if [ -z "$ip" ]; then
    #         ip=$(curl -6s --connect-timeout 5 https://icanhazip.com 2>/dev/null)
    #     fi
        
    #     # Try IPv6 with ipecho.net
    #     if [ -z "$ip" ]; then
    #         ip=$(curl -6s --connect-timeout 5 https://ipecho.net/plain 2>/dev/null)
    #     fi
    # fi

    if [ -z "$ip" ]; then
        echo "Error: Could not determine server IP address automatically (neither IPv4 nor IPv6)." >&2
        echo "Please set the ADVERTISE_ADDR environment variable manually." >&2
        echo "Example: export ADVERTISE_ADDR=<your-server-ip>" >&2
        exit 1
    fi

    echo "$ip"
}

advertise_addr="${ADVERTISE_ADDR:-$(get_ip)}"
echo "Using advertise address: $advertise_addr"

docker swarm init --advertise-addr $advertise_addr

    if [ $? -ne 0 ]; then
    echo "Error: Failed to initialize Docker Swarm" >&2
    exit 1
fi

echo "Swarm initialized"

docker network rm -f dokploy-network 2>/dev/null
docker network create --driver overlay --attachable dokploy-network

echo "Network created"

mkdir -p /etc/dokploy

chmod 777 /etc/dokploy

# docker pull postgres:16
# docker pull redis:7
# docker pull traefik:v3.1.2
# docker pull dokploy/dokploy:latest

for file in ./images/*.tar; do
    docker load -i "$file"
done

docker images

# Installation
docker service create \
    --name dokploy \
    --replicas 1 \
    --network dokploy-network \
    --mount type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock \
    --mount type=bind,source=/etc/dokploy,target=/etc/dokploy \
    --mount type=volume,source=dokploy-docker-config,target=/root/.docker \
    --publish published=3000,target=3000,mode=host \
    --update-parallelism 1 \
    --update-order stop-first \
    --constraint 'node.role == manager' \
    -e ADVERTISE_ADDR=$advertise_addr \
    dokploy/dokploy:latest

GREEN="\033[0;32m"
YELLOW="\033[1;33m"
BLUE="\033[0;34m"
NC="\033[0m" # No Color

format_ip_for_url() {
    local ip="$1"
    if echo "$ip" | grep -q ':'; then
        # IPv6
        echo "[${ip}]"
    else
        # IPv4
        echo "${ip}"
    fi
}

formatted_addr=$(format_ip_for_url "$advertise_addr")
echo ""
printf "${GREEN}Congratulations, Dokploy is installed!${NC}\n"
printf "${BLUE}Wait 15 seconds for the server to start${NC}\n"
printf "${YELLOW}Please go to http://${formatted_addr}:3000${NC}\n\n"

卸载

docker swarm leave --force
docker rm -f $(docker ps -a --filter "name=dokploy" -q)
docker volume rm -f dokploy-postgres-database redis-data-volume
docker network rm -f dokploy-network
sudo rm -rf /etc/dokploy
上次编辑于:
贡献者: 李元昊