From c55d2063e1c2c434f540b2b342e214d54132725f Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Thu, 6 Feb 2020 18:43:03 +0200 Subject: [PATCH] add a simple docker example Former-commit-id: cacfa3ad903ce542ce455cb2298c40639c645d3c --- _examples/README.md | 1 + _examples/docker/.dokerignore | 3 +++ _examples/docker/Dockerfile | 15 +++++++++++++++ _examples/docker/README.md | 27 +++++++++++++++++++++++++++ _examples/docker/docker-compose.yml | 8 ++++++++ _examples/docker/go.mod | 7 +++++++ _examples/docker/main.go | 26 ++++++++++++++++++++++++++ 7 files changed, 87 insertions(+) create mode 100644 _examples/docker/.dokerignore create mode 100644 _examples/docker/Dockerfile create mode 100644 _examples/docker/README.md create mode 100644 _examples/docker/docker-compose.yml create mode 100644 _examples/docker/go.mod create mode 100644 _examples/docker/main.go diff --git a/_examples/README.md b/_examples/README.md index d993ff0e..d2333e4e 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -33,6 +33,7 @@ $ go run main.go ### Overview - [Hello world!](hello-world/main.go) +- [Docker](docker/README.md) - [Hello WebAssembly!](webassembly/basic/main.go) - [Glimpse](overview/main.go) - [Tutorial: Online Visitors](tutorial/online-visitors/main.go) diff --git a/_examples/docker/.dokerignore b/_examples/docker/.dokerignore new file mode 100644 index 00000000..ffdca42e --- /dev/null +++ b/_examples/docker/.dokerignore @@ -0,0 +1,3 @@ +.git +node_modules +bin \ No newline at end of file diff --git a/_examples/docker/Dockerfile b/_examples/docker/Dockerfile new file mode 100644 index 00000000..0d1e0227 --- /dev/null +++ b/_examples/docker/Dockerfile @@ -0,0 +1,15 @@ +# docker build -t myapp . +# docker run --rm -it -p 8080:8080 myapp:latest +FROM golang:latest AS builder +RUN apt-get update +ENV GO111MODULE=on \ + CGO_ENABLED=0 \ + GOOS=linux \ + GOARCH=amd64 +WORKDIR /go/src/app +COPY . /go/src/app +RUN go install + +FROM scratch +COPY --from=builder /go/bin/app . +ENTRYPOINT ["./app"] \ No newline at end of file diff --git a/_examples/docker/README.md b/_examples/docker/README.md new file mode 100644 index 00000000..cdb267f8 --- /dev/null +++ b/_examples/docker/README.md @@ -0,0 +1,27 @@ +# Docker Example + +The only requirement for this example is [Docker](https://docs.docker.com/install/). + +## Docker Compose + +The Docker Compose is pre-installed with Docker for Windows. For linux please follow the steps described at: https://docs.docker.com/compose/install/. + +Build and run the application for linux arch and expose it on http://localhost:8080. + +```sh +$ docker-compose up +``` + +See [docker-compose file](docker-compose.yml). + +## Without Docker Compose + +1. Build the image as "myapp" (docker build) +2. Run the image and map exposed ports (-p 8080:8080) +3. Attach the interactive mode so CTRL/CMD+C signals are respected to shutdown the Iris Server (-it) +4. Cleanup the image on finish (--rm) + +```sh +$ docker build -t myapp . +$ docker run --rm -it -p 8080:8080 myapp:latest +``` diff --git a/_examples/docker/docker-compose.yml b/_examples/docker/docker-compose.yml new file mode 100644 index 00000000..54c86b33 --- /dev/null +++ b/_examples/docker/docker-compose.yml @@ -0,0 +1,8 @@ +# docker-compose up [--build] +version: '3' + +services: + app: + build: . + ports: + - 8080:8080 \ No newline at end of file diff --git a/_examples/docker/go.mod b/_examples/docker/go.mod new file mode 100644 index 00000000..0e122131 --- /dev/null +++ b/_examples/docker/go.mod @@ -0,0 +1,7 @@ +module app + +go 1.13 + +require ( + github.com/kataras/iris/v12 v12.1.6 +) diff --git a/_examples/docker/main.go b/_examples/docker/main.go new file mode 100644 index 00000000..521f1126 --- /dev/null +++ b/_examples/docker/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "flag" + + "github.com/kataras/iris/v12" +) + +var addr = flag.String("addr", ":8080", "host:port to listen on") + +// $ docker-compose up +func main() { + flag.Parse() + + app := iris.New() + + app.Get("/", func(ctx iris.Context) { + ctx.HTML("Hello World!") + }) + + app.Get("/api/values/{id:uint}", func(ctx iris.Context) { + ctx.Writef("id: %d", ctx.Params().GetUintDefault("id", 0)) + }) + + app.Run(iris.Addr(*addr)) +}