# Dockerize serverless in go: no such file or directory

**URL:** <https://forum.serverless.com/t/dockerize-serverless-in-go-no-such-file-or-directory/9452>\
**Category:** Serverless Framework\
**Created:** [September 25, 2019, 9:30pm UTC](https://forum.serverless.com/t/dockerize-serverless-in-go-no-such-file-or-directory/9452 "2019-09-25T21:30:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![myoshida](https://avatars.discourse-cdn.com/v4/letter/m/3bc359/32.png) [@myoshida](https://forum.serverless.com/u/myoshida)\
**Post date:** [September 25, 2019, 9:30pm UTC](https://forum.serverless.com/t/dockerize-serverless-in-go-no-such-file-or-directory/9452/1 "2019-09-25T21:30:09Z")

</div>

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

```auto
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

```auto
{
  "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

```auto
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

---

<div class="post-metadata">

**Author:** ![myoshida](https://avatars.discourse-cdn.com/v4/letter/m/3bc359/32.png) [@myoshida](https://forum.serverless.com/u/myoshida)\
**Post date:** [September 27, 2019, 10:57pm UTC](https://forum.serverless.com/t/dockerize-serverless-in-go-no-such-file-or-directory/9452/2 "2019-09-27T22:57:24Z")

</div>

This may not be appropriate for this forum, but this is how I solved the problem.  
Instead of using serverless, execute codes directly in lambci/lambda since it is actually the image that serverless use to execute code on

I used docker to compile go files into its appropriate environment.

```auto
# base image
FROM golang:1.13

WORKDIR /app
COPY go.mod go.sum ./

RUN go mod download

#build executable and plugins
COPY . .
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

```

In Makefile, build and run the image, copy the binary files to local, and finally attach those files as volume to lamci/lambda image and run.

```auto
dockerBuild:
	docker build -t go-serverless .
	docker run -d --name go-serverless go-serverless
	docker cp go-serverless:/app/bin/ $(PWD)/bin
	docker cp go-serverless:/app/opt/ $(PWD)/opt
	docker stop go-serverless
	docker rm go-serverless

dockerInvoke: dockerBuild
	 cat event.json | docker run --rm -v $(PWD):/var/task -v $(PWD)/opt:/opt -i -e DOCKER_LAMBDA_USE_STDIN=1 --env-file env.list lambci/lambda:go1.x bin/main

```

By doing so, I am certain that I am attaching the executable files to its appropriate place, and being able to run the lambda function

I hope this is helpful for someone else, and let me know if you encounter problems.
