Docker容器中的AWS SAM CLI导致ClassNotFoundException

时间:2018-08-10 15:43:04

标签: java docker aws-lambda aws-sam-cli

我正在尝试基于this project使AWS SAM CLI成为我的docker-compose文件的一部分。除了调用Lambda函数时,我都收到ClassNotFoundException异常,一切似乎都可以正常进行。我已经看到有几个人在权限方面表现出类似的问题,但是即使我将所有文件设为777,也没有任何变化。我还可以在日志中看到它正在解压缩正确的jar文件,而我自己已经解压缩了该文件,并看到了“找不到”的类。

这是我的Docker文件:

public class Question1 extends Fragment {
    private List<Question> questionsList;
    private Question currentQuestion;

    private TextView txtQuestion, tvNoOfQs;
    private RadioButton rbtnA, rbtnB, rbtnC, rbtnD;

    private int obtainedScore = 0;
    private int questionId = 0;

    private int answeredQsNo = 0;

    ArrayList<String> myAnsList;

    public Question1() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        final DBAdapter dbAdapter = new DBAdapter(getContext());
        questionsList = dbAdapter.getAllQuestions();
        currentQuestion = questionsList.get(questionId);

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_question1, container, false);
        final RadioGroup grp = (RadioGroup)view.findViewById(R.id.radioGroup1);
        tvNoOfQs = (TextView)view.findViewById(R.id.tvNumberOfQuestions);
        txtQuestion = (TextView)view.findViewById(R.id.tvQuestion);
        rbtnA = (RadioButton)view.findViewById(R.id.radio0);
        rbtnB = (RadioButton)view.findViewById(R.id.radio1);
        rbtnC = (RadioButton)view.findViewById(R.id.radio2);
        rbtnD = (RadioButton)view.findViewById(R.id.radio3);
        setPertanyaan();

        myAnsList = new ArrayList<String>();
        return view;
    }

    private void setPertanyaan() {
        rbtnA.setChecked(false);
        rbtnB.setChecked(false);
        rbtnC.setChecked(false);
        rbtnD.setChecked(false);

        answeredQsNo = questionId+1;
        tvNoOfQs.setText(+answeredQsNo+"/"+questionsList.size());

        txtQuestion.setText(currentQuestion.getQUESTION());
        rbtnA.setText(currentQuestion.getOptionA());
        rbtnB.setText(currentQuestion.getOptionB());
        rbtnC.setText(currentQuestion.getOptionC());
        rbtnD.setText(currentQuestion.getOptionD());

        questionId++;
    }
}

以及docker-compose.yml的相关部分:

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

SAM模板:

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

我的输出日志:

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

1 个答案:

答案 0 :(得分:0)

- /tmp:/tmp添加到docker-compose挂载的卷中,否则您的lambda子泊坞窗将无法在父容器中找到从jar中解压缩的类