Skip to content

Instantly share code, notes, and snippets.

@mcastelino
Created October 30, 2018 23:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcastelino/2e0158447f09d7b72fcf48cc5be703af to your computer and use it in GitHub Desktop.
Save mcastelino/2e0158447f09d7b72fcf48cc5be703af to your computer and use it in GitHub Desktop.
Running nuclio standalone
  1. mkdir somedir
  2. cd somedir
  3. create a file function.go with
/*
Copyright 2017 The Nuclio Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
        "github.com/nuclio/nuclio-sdk-go"
)

func Handler(context *nuclio.Context, event nuclio.Event) (interface{}, error) {
        context.Logger.Info("This is an unstrucured %s", "log")

        return nuclio.Response{
                StatusCode:  200,
                ContentType: "application/text",
                Body:        []byte("Hello, from nuclio :]"),
        }, nil
}
  1. Create a config.yaml with
PlatformConfig: null
metadata:
  labels:
    nuclio.io/project-name: 861269c8-c42c-4da3-ba79-74e44dabede1
  name: test
  namespace: nuclio
spec:
  build:
    codeEntryType: sourceCode
    functionSourceCode: LyoKQ29weXJpZ2h0IDIwMTcgVGhlIE51Y2xpbyBBdXRob3JzLgoKTGljZW5zZWQgdW5kZXIgdGhlIEFwYWNoZSBMaWNlbnNlLCBWZXJzaW9uIDIuMCAodGhlICJMaWNlbnNlIik7CnlvdSBtYXkgbm90IHVzZSB0aGlzIGZpbGUgZXhjZXB0IGluIGNvbXBsaWFuY2Ugd2l0aCB0aGUgTGljZW5zZS4KWW91IG1heSBvYnRhaW4gYSBjb3B5IG9mIHRoZSBMaWNlbnNlIGF0CgogICAgaHR0cDovL3d3dy5hcGFjaGUub3JnL2xpY2Vuc2VzL0xJQ0VOU0UtMi4wCgpVbmxlc3MgcmVxdWlyZWQgYnkgYXBwbGljYWJsZSBsYXcgb3IgYWdyZWVkIHRvIGluIHdyaXRpbmcsIHNvZnR3YXJlCmRpc3RyaWJ1dGVkIHVuZGVyIHRoZSBMaWNlbnNlIGlzIGRpc3RyaWJ1dGVkIG9uIGFuICJBUyBJUyIgQkFTSVMsCldJVEhPVVQgV0FSUkFOVElFUyBPUiBDT05ESVRJT05TIE9GIEFOWSBLSU5ELCBlaXRoZXIgZXhwcmVzcyBvciBpbXBsaWVkLgpTZWUgdGhlIExpY2Vuc2UgZm9yIHRoZSBzcGVjaWZpYyBsYW5ndWFnZSBnb3Zlcm5pbmcgcGVybWlzc2lvbnMgYW5kCmxpbWl0YXRpb25zIHVuZGVyIHRoZSBMaWNlbnNlLgoqLwoKcGFja2FnZSBtYWluCgppbXBvcnQgKAoJImdpdGh1Yi5jb20vbnVjbGlvL251Y2xpby1zZGstZ28iCikKCmZ1bmMgSGFuZGxlcihjb250ZXh0ICpudWNsaW8uQ29udGV4dCwgZXZlbnQgbnVjbGlvLkV2ZW50KSAoaW50ZXJmYWNle30sIGVycm9yKSB7Cgljb250ZXh0LkxvZ2dlci5JbmZvKCJUaGlzIGlzIGFuIHVuc3RydWN1cmVkICVzIiwgImxvZyIpCgoJcmV0dXJuIG51Y2xpby5SZXNwb25zZXsKCQlTdGF0dXNDb2RlOiAgMjAwLAoJCUNvbnRlbnRUeXBlOiAiYXBwbGljYXRpb24vdGV4dCIsCgkJQm9keTogICAgICAgIFtdYnl0ZSgiSGVsbG8sIGZyb20gbnVjbGlvIDpdIiksCgl9LCBuaWwKfQo=
    runtimeAttributes:
      repositories: []
  description: Showcases unstructured logging and a structured response.
  handler: main:Handler
  image: nuclio/processor-test:latest
  maxReplicas: 1
  minReplicas: 1
  platform: {}
  resources: {}
  runtime: golang
  1. Create a Dockerfile with
ARG NUCLIO_LABEL=latest
ARG NUCLIO_ARCH=amd64
ARG NUCLIO_BASE_IMAGE=alpine:3.7
ARG NUCLIO_ONBUILD_IMAGE=nuclio/handler-builder-golang-onbuild:0.7.0-amd64-alpine

# Supplies processor uhttpc, used for healthcheck
FROM nuclio/uhttpc:0.0.1-amd64 as uhttpc

# Builds source, supplies processor binary and handler plugin
FROM ${NUCLIO_ONBUILD_IMAGE} as builder

# From the base image
FROM ${NUCLIO_BASE_IMAGE}

# Copy required objects from the suppliers
COPY --from=builder /home/nuclio/bin/processor /usr/local/bin/processor
COPY --from=builder /home/nuclio/bin/handler.so /opt/nuclio/handler.so
COPY --from=uhttpc /home/nuclio/bin/uhttpc /usr/local/bin/uhttpc

# Readiness probe
HEALTHCHECK --interval=1s --timeout=3s CMD /usr/local/bin/uhttpc --url http://127.0.0.1:8082/ready || exit 1

# Run processor with configuration and platform configuration
CMD [ "processor" ]

  1. Build the image
docker build -t helloworld-from-df .
  1. Now run the image standalone
docker run -v ./config.yaml:/etc/processor.yaml -it helloworld-from-df /bin/sh
processor -alsologtostderr -config /etc/processor.yaml
18.10.30 23:38:48.960            processor.http (I) Starting {"listenAddress": ":8080", "readBufferSize": 4096}
18.10.30 23:38:48.960 processor.webadmin.server (I) Listening {"listenAddress": ":8081"}
  1. Now based on the IP assigned to the container you can invoke the function
curl http://172.17.0.2:8080
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment