• Stars
    star
    254
  • Rank 160,264 (Top 4 %)
  • Language
  • License
    Apache License 2.0
  • Created almost 8 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Functions as Docker containers

Funker: Functions as Docker containers

Funker allows you to package up pieces of your application as Docker containers and have them run on-demand on a swarm.

You can define functions like this as Docker services:

var funker = require('funker');

funker.handler(function(args, callback) {
  callback(args.x + args.y);
});

Then call them from other Docker services on any node in the swarm:

>>> import funker
>>> funker.call("add", x=1, y=2)
3

These functions are being called demand, scale effortlessly, and make your application vastly simpler. It's a bit like serverless, but just using Docker.

Getting started

Creating a function

First, you need to package up a piece of your application as a function. Let's start with a trivial example: a function that adds two numbers together.

Save this code as handler.js:

var funker = require('funker');

funker.handler(function(args, callback) {
  callback(args.x + args.y);
});

We also need to define the Node package in package.json:

{
  "name": "app",
  "version": "0.0.1",
  "scripts": {
    "start": "node handler.js"
  },
  "dependencies": {
    "funker": "^0.0.1"
  }
}

Then, we package it up inside a Docker container by creating Dockerfile:

FROM node:7-onbuild

And building it:

$ docker build -t add .

To run the function, you create a service:

$ docker network create --attachable -d overlay funker
$ docker service create --name add --network funker add

The function is now available at the name add to other things running inside the same network. It has booted up a warm version of the function, so calls made to it will be instant.

Calling a function

Let's try calling the function from a Python shell:

$ docker run -it --net funker funker/python

(The funker/python image is just a Python image with the funker package installed.)

You should now see a Python prompt. Try importing the package and running the function we just created:

>>> import funker
>>> funker.call("add", x=1, y=2)
3

Cool! So, to recap: we've put a function written in Node inside a container, then called it from Python. That function is run on-demand, and this is all being done with plain Docker services and no additional infrastructure.

Implementations

There are implementations of handling and calling Funker functions in various languages:

Example applications

Deploying with Compose

Functions are just services, so they are really easy to deploy using Compose. You simply define them alongside your long-running services.

For example, to deploy a function called process-upload:

version: "2"
services:
  web:
    image: oscorp/web
  db:
    image: postgres
  process-upload:
    image: oscorp/process-upload
    restart: always

In all the services in this application, the function will be available under the name process-upload. For example, you could call it with a bit of code like this:

funker.call("process-upload", bucket="some-s3-bucket", filename="upload.jpg")

Architecture

The architecture is intentionally very simple. It leans on Docker services as the base infrastructure, and avoids any unnecessary complexity (daemons, queues, storage, consensus systems, and so on).

Functions run as Docker services. When they boot up, they open a TCP socket and sit there waiting for a connection.

To call functions, another Docker service connects to the function at its hostname. This can be done anywhere in a swarm due to Docker's overlay networking. It sends function arguments as JSON, then the function responds with a return value as JSON.

Once it has been called, the function refuses any other connections. Once it has responded, the function closes the socket and quits immediately. Docker's state reconciliation will then boot up a fresh copy of the function ready to receive calls again.

So, each function only processes a single request. To process functions in parallel, we need to have multiple warm functions running in parallel, which is easy to do with Docker's service replication. The idea is to do this automatically, but this is incomplete. See this issue for more background and discussion.

Alternative architectures

An alternative implementation considered was for the function caller to create the service directly, as has been done in some previous experiments.

The upside of Funker over this implementation is that functions are warm and ready to receive calls, and you don't need the complexity of giving containers access to create Docker services somehow.

The disadvantage is that it doesn't scale easily. We need some additional infrastructure to be able to scale functions up and down to handle demand.

## Credits

More Repositories

1

jsnes

A JavaScript NES emulator.
JavaScript
5,921
star
2

jsnes-web

A browser UI for JSNES, a JavaScript NES emulator
JavaScript
586
star
3

django-shorturls

A URL shortening app for Django
Python
405
star
4

serverless-docker

Build serverless apps with Docker
306
star
5

dynamicaudio.js

An interface for the Web Audio API with a Flash shim for older browsers
JavaScript
168
star
6

loom

Elegant deployment with Fabric and Puppet.
Python
116
star
7

whiskerboard

Stashboard, now with less vendor lock-in.
Python
112
star
8

otter

A server that runs your client-side apps.
CoffeeScript
82
star
9

django-class-based-views

Work on class-based views, now part of Django 1.3. For a backport to Django 1.2, see https://github.com/sorl/django-cbv
Python
79
star
10

serverless-docker-voting-app

A serverless web app built with Docker
HTML
61
star
11

naming-test-suite

How I name products/projects/tools
39
star
12

vNES

A mirror of Jamie Sanders' Java NES emulator, which once lived at virtualnes.com
Java
38
star
13

django-docker-heroku-template

Get a Django app up and running in dev, test, and production with best practices in 10 minutes
Python
36
star
14

python-echoprint

Python library for the Echoprint music identification service
C++
27
star
15

dockerrun

A dead simple Python library for running Docker commands
Python
25
star
16

funker-example-voting-app

An example app using Funker
JavaScript
25
star
17

pspec

Python testing for humans.
Python
21
star
18

syndication-view

Django feeds as class based views, now part of Django 1.2.
Python
21
star
19

pytest_django

A Django plugin for py.test.
Python
19
star
20

django-database-files

A storage system for Django that stores uploaded files in the database.
Python
19
star
21

docker-export-volumes

A Docker image to export volumes from containers
Python
16
star
22

go-dcgi

CGI, but with Docker containers
Go
13
star
23

django-docker-cloud-template

A template for creating Django applications that run on Docker Cloud
Python
12
star
24

funker-go

Funker for Go
Go
12
star
25

heroku-tarsnap-backups

Make offsite backups of Heroku apps on Tarsnap
Shell
11
star
26

docker-redoc

A Docker image which serves ReDoc documentation
HTML
11
star
27

django-mptt

It's MPTT, but with abstract model awesomeness.
Python
11
star
28

funker-python

Python
9
star
29

swarm-viz

HTML
9
star
30

docker-reticulate-splines

Shell
8
star
31

miles

A JavaScript framework for actually getting things done
JavaScript
8
star
32

dotfiles

Vim Script
8
star
33

virtualdocker

Go
8
star
34

rrun

rsync the current directory and run a command over ssh
Shell
7
star
35

herokup

Upload stuff to Heroku that isn't in Git.
Shell
7
star
36

funker-node

JavaScript
7
star
37

jstunes

A JavaScript audio player
JavaScript
7
star
38

docker-sdk-go

WIP: Prototype of a better Go client for Docker
Go
5
star
39

paperpets

Mini origami pets for Little Printer
CSS
5
star
40

fir.sh

SCSS
5
star
41

jsnes-roms

4
star
42

charingcross

Project planning with GitHub issues
Python
4
star
43

docker-api-reference

WIP: Better API reference for Docker, built on Swagger
HTML
3
star
44

vqgan-clip

Python
3
star
45

antsy

Multi-line console animation in Go
Go
2
star
46

cog-segformer

Python
2
star
47

ip

Python
2
star
48

docker-go-client-example

Go
2
star
49

backbone-otter

Tools for writing Otter apps with Backbone
JavaScript
2
star
50

haikuplayer

Python
2
star
51

puppet-cloudkick

A Puppet module for Cloudkick.
Shell
2
star
52

docker-sdk-python

WIP
Python
2
star
53

riffusion-cog

Riffusion as a Cog model
Python
2
star
54

docker-cloud-subdomains

Automatic Heroku-style subdomains for Docker Cloud
Python
2
star
55

internetarchive-pdfjs-isbn-links

JavaScript
2
star
56

hubot-shipping-forecast

CoffeeScript
1
star
57

replicate-screenshots

1
star
58

docker-oauth2_proxy

1
star
59

interlock-haproxy-swarm

Interlock configured to use a Swarm set up by Machine
Shell
1
star
60

python-sdk

Facebook Platform Python SDK
Python
1
star
61

patchwork

JavaScript
1
star
62

compose-mongodb-demo

Python
1
star