You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dockerfile安装到/tmp的Certbot在AWS Lambda中无法找到

问题排查:Lambda容器镜像中Certbot无法找到的原因与修复

核心问题原因

AWS Lambda运行容器时,/tmp目录是每次执行时临时挂载的全新空目录,你在Dockerfile构建阶段写入/tmp的Certbot文件,不会被带到Lambda运行时环境中。本地Docker运行时不会替换/tmp,所以能正常访问,但部署到Lambda后,原/tmp下的内容全部丢失,导致出现No such file or directory错误。

Lambda仅会保留${LAMBDA_TASK_ROOT}(默认是/var/task)目录下的内容,这是存放函数代码和依赖的固定目录,不会被临时替换。

修复方案

1. 修改Dockerfile,将Certbot安装到非/tmp目录

把Certbot安装到/var/task/opt/certbot目录下,替换原有的/tmp路径:

FROM public.ecr.aws/lambda/nodejs:16

ARG FUNCTION_DIR=${LAMBDA_TASK_ROOT}
# 改为将Certbot安装到任务根目录下的opt文件夹
ARG CERTBOT_DIR="${FUNCTION_DIR}/opt/certbot"
ARG CERTBOT_VERSION=0.31.0

# Install Certbot
RUN mkdir -p ${CERTBOT_DIR}
RUN yum update -y && \
    yum install -y git && \
    git --version && \
    git clone https://github.com/certbot/certbot $CERTBOT_DIR && \
    if [ "${CERTBOT_VERSION}" != "latest" ]; then \
        cd $CERTBOT_DIR && git checkout tags/v${CERTBOT_VERSION} ; \
    fi && \
    $CERTBOT_DIR/certbot-auto --no-bootstrap --no-self-upgrade --help && \
    yum remove -y git && \
    yum clean all && \
    rm -rf /var/cache/yum/*

COPY app.js package*.json ./
RUN npm install

CMD [ "app.handler" ]

2. 修改app.js中的Certbot命令路径

同步更新执行命令的路径,指向新的安装目录:

const util = require('util');
const exec = util.promisify(require('child_process').exec);

exports.handler = async (event, context) => {

  try {
    // 改为任务根目录下的Certbot路径
    const certbotPath = '/var/task/opt/certbot/certbot-auto';

    const { stdout: lsCommand } = await exec(`ls -lah /var/task/opt/certbot/`);
    const lsCommandMessage = "Certbot目录内容: " + lsCommand;

    const { stdout: certbotCommand } = await exec(`${certbotPath} --no-bootstrap --no-self-upgrade --help`);
    const certbotCommandMessage = "Certbot命令结果: " + certbotCommand;

    const resObj = {
      message: "Hello world from Docker!",
      lsCommandMessage: lsCommandMessage,
      certbotCommandMessage: certbotCommandMessage
    };

    const response = {
      statusCode: 200,
      body: resObj,
    };

    return response;

  } catch (error) {
    console.error('Error:', error);
    // 建议返回错误响应,便于调试
    return {
      statusCode: 500,
      body: JSON.stringify({ error: error.message })
    };
  }
  
};

验证步骤

  1. 重新构建镜像并推送到ECR:
docker build -t certbot-lambda:v1 . && docker tag certbot-lambda:v1 <your-ecr-uri>:v1 && docker push <your-ecr-uri>:v1
  1. 更新Lambda函数的容器镜像URI为新推送的镜像
  2. 重新测试Lambda函数,此时Certbot命令应该能正常执行

内容的提问来源于stack exchange,提问作者calendo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 14:15:35