java - Docker 容器中的 AWS SAM CLI 导致 ClassNotFoundException
问题描述
我正在尝试基于此项目使 AWS SAM CLI 成为我的 docker-compose 文件的一部分。一切似乎都出现了,除了当我调用我的 Lambda 函数时,我得到一个 ClassNotFoundException。我见过几个人的权限问题表现类似,但即使我将所有文件设为 777,也没有任何变化。我还可以在日志中看到它正在解压正确的 jar 文件,并且我自己解压了该文件并看到它“找不到”的类。
这是我的 Docker 文件:
FROM alpine:3.8
RUN apk add --no-cache python3 python3-dev gcc musl-dev && \
python3 -m ensurepip && \
rm -r /usr/lib/python*/ensurepip && \
pip3 install --upgrade pip setuptools && \
if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi && \
if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi && \
rm -r /root/.cache
ENV PATH $PATH:/root/.local/bin
RUN pip3 install --user awscli
RUN pip3 install --user aws-sam-cli
COPY conf/lambda /var/opt/lambda
COPY lib/lambda.jar /var/opt/lambda/lambda.jar
RUN chmod -R 0777 /var/opt/lambda
RUN python -m site --user-base
WORKDIR /var/opt/lambda
以及 docker-compose.yml 的相关部分:
sam:
build:
context: .
dockerfile: Dockerfile-sam
networks:
- sam-local
command: sam local invoke StartPiSessionFunction -e event.json --template template.yaml
hostname: sam
expose:
- 3000
ports:
- 3000:3000
volumes:
- /var/run/docker.sock:/var/run/docker.sock
SAM 模板:
AWSTemplate
FormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Resources:
StartPiSessionFunction:
Type: 'AWS::Serverless::Function'
Properties:
Runtime: java8
Handler: com.company.StartPiSession
Timeout: 60
CodeUri: lambda.jar
我的输出日志:
2018-08-10 15:32:41 Invoking com.company.StartPiSession (java8)
2018-08-10 15:32:41 Starting new HTTP connection (1): 169.254.169.254
2018-08-10 15:32:42 Decompressing /var/opt/lambda/lambda.jar
Fetching lambci/lambda:java8 Docker container image......
2018-08-10 15:33:18 Mounting /tmp/tmpsth810ki as /var/task:ro inside runtime container
START RequestId: eb907a6f-68c3-4afe-90b7-87a73a957322 Version: $LATEST
java.lang.ClassNotFoundException: com.company.StartPiSession
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
END RequestId: eb907a6f-68c3-4afe-90b7-87a73a957322
REPORT RequestId: eb907a6f-68c3-4afe-90b7-87a73a957322 Duration: 3.84 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 3 MB
解决方案
添加- /tmp:/tmp
到安装的卷中,docker-compose
否则您的 lambda 子 docker 将无法在父容器中的 jar 中找到未压缩的类
推荐阅读
- python - 如何将文件与在 Python 中引用它们的 xml 关联?
- python - “命令”对象没有属性“has_permissions”
- asp.net-core - 机密客户端应用程序缓存 - GetAccountAsync
- c# - 为什么我不能在 Visual Studio 中移动 Windows 窗体?
- python - 从 python 在符号链接目录中运行 Makefile
- c# - 转到行/循环切换语句?
- rust - Rust - 类型的值不能从 `std::iter::Iterator<>` 构建
- c++ - c ++:原子变量的值在保存在局部变量中时无效
- .net - .net 核心网络应用程序 docker 映像如何在 Mac OS 上运行?
- css - 在导航栏中以小分辨率对齐搜索输入和提交按钮时遇到问题