From e90bde65bdcab6b23a9d686a9001347793d91bc0 Mon Sep 17 00:00:00 2001 From: Adrian Cruz Date: Sun, 4 Oct 2020 17:22:50 -0400 Subject: [PATCH] Add Docker container support Adding a containerized version to this repo. The main additions here are the Dockerfile and docker-compose.yml. But also, I changed the default port to run on :5000. --- Dockerfile | 12 ++++++++++ README.md | 55 ++++++++++++++++++++++++++++++++++++++++++---- docker-compose.yml | 6 +++++ gohang.go | 6 +++-- 4 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..cf042dd --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM golang:1.14 + +WORKDIR /go/src/app + +COPY . /go/src/app + +RUN env GOOS=linux go build gohang.go +RUN go install . + +ENTRYPOINT /go/src/app/gohang + +EXPOSE 5000 diff --git a/README.md b/README.md index 919c26e..b90efd1 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Run `gohang` ### / ``` -curl -I localhost:80/ +curl -I localhost:5000/ HTTP/1.1 200 OK Date: Sun, 04 Oct 2020 01:38:03 GMT Content-Length: 19 @@ -22,7 +22,7 @@ Content-Type: text/plain; charset=utf-8 ### /404 ``` -$ curl -I localhost:80/404 +$ curl -I localhost:5000/404 HTTP/1.1 404 Not Found Date: Sun, 04 Oct 2020 01:37:17 GMT Content-Length: 26 @@ -32,7 +32,7 @@ Content-Type: text/plain; charset=utf-8 ### /500 ``` -$ curl -I localhost:80/500 +$ curl -I localhost:5000/500 HTTP/1.1 500 Internal Server Error Date: Sun, 04 Oct 2020 01:34:28 GMT Content-Length: 38 @@ -42,9 +42,56 @@ Content-Type: text/plain; charset=utf-8 ### /slow ``` -$ time curl localhost:80/slow +$ time curl localhost:5000/slow { "data": "slow response"} real 0m5.023s user 0m0.006s sys 0m0.006s ``` + +## Build a Docker container + +Did you want to run this in a Docker container? +Simply run `docker-compose build` and then `docker-compose up`. + +### docker-compose build + +``` +$ docker-compose build +Building web +Step 1/7 : FROM golang:1.14 + ---> d6747a138341 +Step 2/7 : WORKDIR /go/src/app + ---> Using cache + ---> e0c45301390b +Step 3/7 : COPY . /go/src/app + ---> 8baa0d29af0d +Step 4/7 : RUN env GOOS=linux go build gohang.go + ---> Running in bde0f5e072ab +Removing intermediate container bde0f5e072ab + ---> 0a454230e0c5 +Step 5/7 : RUN go install . + ---> Running in 9701cb98bf9a +Removing intermediate container 9701cb98bf9a + ---> c2b7cf38e979 +Step 6/7 : ENTRYPOINT /go/src/app/gohang + ---> Running in 60bbd1397191 +Removing intermediate container 60bbd1397191 + ---> ad4f7bc0dbe0 +Step 7/7 : EXPOSE 5000 + ---> Running in e56fedc7f4d6 +Removing intermediate container e56fedc7f4d6 + ---> a15ced7b2772 + +Successfully built a15ced7b2772 +Successfully tagged gohang_web:latest +``` + +### docker-compose up + +``` +$ docker-compose up +Recreating gohang_web_1 ... done +Attaching to gohang_web_1 +web_1 | 2020/10/04 21:16:09 [INFO] Now listening on :5000 +``` diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ab7568d --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,6 @@ +version: "3.8" +services: + web: + build: . + ports: + - "5000:5000" diff --git a/gohang.go b/gohang.go index dd221f8..47deed8 100644 --- a/gohang.go +++ b/gohang.go @@ -6,6 +6,8 @@ import ( "time" ) +const DEFAULT_PORT string = ":5000" + func okTwoHundred(w http.ResponseWriter, r *http.Request) { w.Write([]byte("{ \"data\": \"200 OK\"}")) } @@ -32,6 +34,6 @@ func main() { mux.HandleFunc("/404", notFound) mux.HandleFunc("/slow", slowResponse) - log.Println("[INFO] Now listening on :80") - http.ListenAndServe(":80", mux) + log.Printf("[INFO] Now listening on %s", DEFAULT_PORT) + http.ListenAndServe(DEFAULT_PORT, mux) }