Possible to run Serverless in a Docker container?

I’m trying to containerize so that I don’t have to mess around with different versions of Node (among other thing) for different projects. The title says it all, is it possible to run Serverless in a Docker container?

I created a Dockerfile:

FROM node:8-alpine

RUN apk update

RUN npm install -g serverless && \
    npm install -g serverless-offline && \
    npm install -g yarn

WORKDIR /usr/src/app

COPY package*.json ./

RUN yarn

COPY . .

EXPOSE 3000

CMD [ "sls", "offline" ]

Build it: docker build -t serverless/docker .
Run it: docker run -p 49160:3000 serverless/docker

Response: curl: (56) Recv failure: Connection reset by peer

Any help or links are greatly appreciated!

According to the serverless-offline documentation, you should use serverless offline start or sls offline start to run it.

Maybe using CMD [ "sls", "offline", "start" ] would help?

I probably should have elaborated a bit more. It runs fine inside the container and lists the routes etc.

Serverless: Starting Offline: local/us-east-1.

Serverless: Routes for test:
Serverless: GET /{name}

Serverless: Offline listening on http://localhost:3000

However trying to access it from my host machine at 0.0.0.0:49160 yields the ‘Connection reset by peer’ error.

In that case, maybe trying to change the host serverless-offline is listening on to 0.0.0.0 would help. You can do so by adding the snippet below to your serverless.yml.

custom:
  serverless-offline:
    host: 0.0.0.0
3 Likes

Thanks so much! That worked perfectly.

I’ve included a link to the example in case anybody else is curious.

https://gitlab.com/dupkey/serverless-examples/tree/master/docker-serverless-development-environment

1 Like

Thanks @dupkey and @teddy-gustiaux this was useful.

In case anyone wants to set the host in the Dockerfile only you can do it like this

CMD [ "sls", "offline", "--host", "0.0.0.0" ]

I had some issues with my base image not being able to find the sls or serverless commands automatically and I ended up having to actually provide a path to the serverless bin file, I also created a Github Gist to hopefully give a little more help with this in the future to anyone needing a little bit more clarification.

Thanks to everyone on this thread for pointing me in the right direction with the host flag :slight_smile: