I’m currently stuck at /fork/exec /var/task/main no such file or directory
when running serverless function written in go in Docker.
I want to dockerize the serverless function so that it’s not necessary to install go or node on machine.
I use multi stage build inside docker. first stage is to compile into go binary, and second stage uses node image to execute serverless. Below is sample
FROM golang as builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY main.go .
RUN GOOS=linux go build -ldflags '-s -w' -a -installsuffix cgo -o /bin/main main.go
RUN GOOS=linux go build -ldflags '-s -w' -buildmode=plugin -a -installsuffix cgo -o /opt/pi.so plugins/pi.go
FROM node:12-alpine
RUN apk add --no-cache bash
RUN apk add --update alpine-sdk
# install docker
RUN apk add docker
RUN apk update
RUN apk add openrc --no-cache
RUN rc-update add docker boot
RUN npm install -g serverless
WORKDIR /app
RUN npm install --save-dev serverless-offline
COPY --from=builder /bin/main ./main
COPY --from=builder /opt/pi ./pi
COPY serverless.yml ./
ENV SLS_DEBUG=*
EXPOSE 3000
RUN serverless slstats --disable
CMD [ "sls", "offline", "start"]
When running image, docker run -v /var/run/docker.sock:/var/run/docker.sock -p 3000:3000 --name sample sample
. to mount docker lock so that the sample container can bring up sls-docker
This successfully creates .serverless in app directory and everything within .serverless just like when i run locally, it builds docker image and run sls-docker, but output was
{
"errorMessage": "fork/exec /var/task/bin/main: no such file or directory",
"errorType": "PathError"
}
I tried to remove the WORKDIR /app from second stage and put everything in root directory. if i do that, excluding development dependencies take forever and can’t bring docker image up.
Also I tried individually, and it takes too long
Below is serverless.yml
layers:
Pi:
path: ./opt/
package:
include:
- ./opt/pi.so
compatibleRuntimes:
- go1.x
description: plugins
functions:
main:
handler: bin/main
package:
exclude:
- ./**
include:
- ./bin/main
layers:
- {Ref: PiLayer}
name: main_lambda
description: >-
lambda func.
memorySize: 256
tracing: PassThrough
events:
- http:
path: public-api-seed
method: POST
enabled: true
anyone have ideas to resolve this, why i can’t execute in docker?
I want to inspect the contents of the docker container created by serverless, but it destroys the container every time when it’d done their job