From 3a0f551e33c7565cc26a0b63af716cdb1b1b249a Mon Sep 17 00:00:00 2001 From: lfache <61111030+lfache@users.noreply.github.com> Date: Tue, 7 Apr 2020 21:32:26 +0200 Subject: [PATCH] Adding simple Traefik example. (#44) Adding simple Traefik example. Signed-off-by: lfache <61111030+lfache@users.noreply.github.com> --- README.md | 1 + traefik-golang/README.md | 97 +++++++++++++++++++++++++++++++ traefik-golang/backend/Dockerfile | 11 ++++ traefik-golang/backend/main.go | 30 ++++++++++ traefik-golang/docker-compose.yml | 19 ++++++ 5 files changed, 158 insertions(+) create mode 100644 traefik-golang/README.md create mode 100644 traefik-golang/backend/Dockerfile create mode 100644 traefik-golang/backend/main.go create mode 100644 traefik-golang/docker-compose.yml diff --git a/README.md b/README.md index db14a80..e172208 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ with Spring framework and a Postgres database. - [`VueJS`](https://github.com/docker/awesome-compose/tree/master/vuejs) - [`Flask`](https://github.com/docker/awesome-compose/tree/master/flask) - [`PHP`](https://github.com/docker/awesome-compose/tree/master/apache-php) +- [`Traefik`](https://github.com/docker/awesome-compose/tree/master/traefik-golang) - [`Django`](https://github.com/docker/awesome-compose/tree/master/django) ## Basic setups for different platforms (not production ready - useful for personal use) - [`Gitea / PostgreSQL`](https://github.com/docker/awesome-compose/tree/master/gitea-postgres) diff --git a/traefik-golang/README.md b/traefik-golang/README.md new file mode 100644 index 0000000..1d3c678 --- /dev/null +++ b/traefik-golang/README.md @@ -0,0 +1,97 @@ +## Compose sample application +### TRAEFIK proxy with GO backend + +Project structure: +``` +. +├── backend +│   ├── Dockerfile +│   └── main.go +├── docker-compose.yml +└── README.md +``` + +[_docker-compose.yaml_](docker-compose.yaml) +``` +version: "3.7" +services: + frontend: + image: traefik:2.2 + command: --providers.docker --entrypoints.web.address=:80 --providers.docker.exposedbydefault=false + ports: + # The HTTP port + - "80:80" + volumes: + # So that Traefik can listen to the Docker events + - /var/run/docker.sock:/var/run/docker.sock + depends_on: + - backend + backend: + build: backend + labels: + - "traefik.enable=true" + - "traefik.http.routers.go.rule=Path(`/`)" + - "traefik.http.services.go.loadbalancer.server.port=80" + +``` +The compose file defines an application with two services `frontend` and `backend`. +When deploying the application, docker-compose maps port 80 of the frontend service container to the same port of the host as specified in the file. +Make sure port 80 on the host is not already being in use. + +## Deploy with docker-compose + +``` +$ docker-compose up -d +Creating network "traefik-golang_default" with the default driver +Building backend +Step 1/7 : FROM golang:1.13 AS build +1.13: Pulling from library/golang +... +Successfully built 22397f6cd4bc +Successfully tagged traefik-golang_backend:latest +WARNING: Image for service backend was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`. +Pulling frontend (traefik:2.2)... +2.2: Pulling from library/traefik +aad63a933944: Pull complete +f365f1b91ebb: Pull complete +dc367a6045f5: Pull complete +ff697159d003: Pull complete +Digest: sha256:615483752426932469aa2229ef3f0825b33b3ad7e1326dcd388205cb3a74352e +Status: Downloaded newer image for traefik:2.2 +Creating traefik-golang_backend_1 ... done +Creating traefik-golang_frontend_1 ... done +``` + +## Expected result + +Listing containers must show two containers running and the port mapping as below: +``` +$ docker ps +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS + NAMES +e845f50da9e6 traefik:2.2 "/entrypoint.sh --pr…" 55 seconds ago Up 54 seconds 0.0.0.0:80->80/tcp traefik-golang_frontend_1 +e164ffd692e8 traefik-golang_backend "/usr/local/bin/back…" 55 seconds ago Up 54 seconds + traefik-golang_backend_1 +``` + +After the application starts, navigate to `http://localhost:80` in your web browser or run: +``` +$ curl localhost:80 + + ## . + ## ## ## == + ## ## ## ## ## === +/"""""""""""""""""\___/ === +{ / ===- +\______ O __/ + \ \ __/ + \____\_______/ + + +Hello from Docker! +``` + +Stop and remove the containers +``` +$ docker-compose down +``` diff --git a/traefik-golang/backend/Dockerfile b/traefik-golang/backend/Dockerfile new file mode 100644 index 0000000..949f8a3 --- /dev/null +++ b/traefik-golang/backend/Dockerfile @@ -0,0 +1,11 @@ +FROM golang:1.13 AS build + +WORKDIR /compose/hello-docker +COPY main.go main.go +RUN CGO_ENABLED=0 go build -o backend main.go + +FROM scratch +COPY --from=build /compose/hello-docker/backend /usr/local/bin/backend +CMD ["/usr/local/bin/backend"] + + diff --git a/traefik-golang/backend/main.go b/traefik-golang/backend/main.go new file mode 100644 index 0000000..a273432 --- /dev/null +++ b/traefik-golang/backend/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "log" + "net/http" +) + +func handler(w http.ResponseWriter, r *http.Request) { + fmt.Println(r.URL.RawQuery) + fmt.Fprintf(w, ` + ## . + ## ## ## == + ## ## ## ## ## === +/"""""""""""""""""\___/ === +{ / ===- +\______ O __/ + \ \ __/ + \____\_______/ + + +Hello from Docker! + +`) +} + +func main() { + http.HandleFunc("/", handler) + log.Fatal(http.ListenAndServe(":80", nil)) +} diff --git a/traefik-golang/docker-compose.yml b/traefik-golang/docker-compose.yml new file mode 100644 index 0000000..1032869 --- /dev/null +++ b/traefik-golang/docker-compose.yml @@ -0,0 +1,19 @@ +version: "3.7" +services: + frontend: + image: traefik:2.2 + command: --providers.docker --entrypoints.web.address=:80 --providers.docker.exposedbydefault=false + ports: + # The HTTP port + - "80:80" + volumes: + # So that Traefik can listen to the Docker events + - /var/run/docker.sock:/var/run/docker.sock + depends_on: + - backend + backend: + build: backend + labels: + - "traefik.enable=true" + - "traefik.http.routers.go.rule=Path(`/`)" + - "traefik.http.services.go.loadbalancer.server.port=80"