部署 kamal 加速

qinsicheng · 2025年12月03日 · 61 次阅读

国内墙的原因,导致在使用 kamal 部署到服务器上慢的要死,最近折腾了一下,在不适用代理情况下,第一次部署在两分钟内,之后更新就在三十秒左右。

举例项目

脚手架生成的最基本项目

-> ruby -v
ruby 3.4.6(2025-09-16 revision dbd83256b1) +PRISM [x86_64-linux]

-> rails -v 
Rails 8.1.1

-> rails new demo --css tailwind -d postgresql

dockerfile syntax 卡住

# syntax=docker.m.daocloud.io/docker/dockerfile:1
# check=error=true

实际测试下来比较稳定,偶尔会卡住

docker login 卡住

dockerHub 登录一直卡住

我换成腾讯云 docker 个人仓库,我看免费有十个,个人完全够用了,需要提前绑定密码,账号就是腾讯云用户 Id

# Where you keep your container images.
registry:
  # Alternatives: hub.docker.com / registry.digitalocean.com / ghcr.io / ...
  server: "ccr.ccs.tencentyun.com"

  # Needed for authenticated registries.
  username: "xxxxxxx"

  # Always use an access token rather than real password when possible.
  password:
    - KAMAL_REGISTRY_PASSWORD

docker pull 卡主

镜像名使用毫秒镜像 - 告别蜗牛速度!国内专线拉取镜像!

FROM docker.1ms.run/ruby:$RUBY_VERSION-slim AS base

debain 卡主

# 换清华源
RUN sed -i 's|deb.debian.org|mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list.d/debian.sources && \
  sed -i 's|security.debian.org|mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list.d/debian.sources && \
  sed -i 's|http://|https://|g' /etc/apt/sources.list.d/debian.sources

bundle install 卡住

RUN gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org

另外把 gemfile 里的 source 也改了

source "https://gems.ruby-china.com/"

其他小问题

healthy check

# 检查一下是否正常
get "up" => "rails/health#show", as: :rails_health_check

数据库登录错误

我是先部署了一个 demo 服务,看成功了后。想着把我实际服务部署上去,结果数据库一直显示没有对应配置用户名,因为我在 deploy.yml 中数据库配置的一样,而之前部署的时候数据库应该是已经配置好了,我就把之前的那个实例删掉,重新部署,看正常了

kamal-proxy 显示已经有一个服务了

还是一样的,之前已经配置了一个服务,现在再配一个新的,又是一样的配置信息。参考:https://github.com/basecamp/kamal/issues/1147

总结

感觉 kamal2 已经很方便了,相比版本 1。个人开发者感觉可以很方便部署服务,建议没用过的,先看官网 DHH 演示,基本无坑,除了墙。

Dockerfile 最终版

# syntax=docker.m.daocloud.io/docker/dockerfile:1
# check=error=true

ARG RUBY_VERSION=3.4.6
FROM docker.1ms.run/ruby:$RUBY_VERSION-slim AS base

WORKDIR /rails

RUN sed -i 's|deb.debian.org|mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list.d/debian.sources && \
  sed -i 's|security.debian.org|mirrors.tuna.tsinghua.edu.cn|g' /etc/apt/sources.list.d/debian.sources && \
  sed -i 's|http://|https://|g' /etc/apt/sources.list.d/debian.sources

RUN apt-get update -qq && \
  apt-get install --no-install-recommends -y curl libjemalloc2 libvips postgresql-client && \
  ln -s /usr/lib/$(uname -m)-linux-gnu/libjemalloc.so.2 /usr/local/lib/libjemalloc.so && \
  rm -rf /var/lib/apt/lists /var/cache/apt/archives

ENV RAILS_ENV="production" \
  BUNDLE_DEPLOYMENT="1" \
  BUNDLE_PATH="/usr/local/bundle" \
  BUNDLE_WITHOUT="development" \
  LD_PRELOAD="/usr/local/lib/libjemalloc.so"

FROM base AS build

RUN gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org 

RUN apt-get update -qq && \
  apt-get install --no-install-recommends -y build-essential git libpq-dev libyaml-dev pkg-config && \
  rm -rf /var/lib/apt/lists /var/cache/apt/archives

COPY Gemfile Gemfile.lock vendor ./

RUN bundle install && \
  rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
  # -j 1 disable parallel compilation to avoid a QEMU bug: https://github.com/rails/bootsnap/issues/495
  bundle exec bootsnap precompile -j 1 --gemfile

COPY . .

RUN bundle exec bootsnap precompile -j 1 app/ lib/

RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile


暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号