Skip to content

Commit

Permalink
Add Docker container support
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
drincruz committed Oct 4, 2020
1 parent ead06ef commit e90bde6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 6 deletions.
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
55 changes: 51 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
```
6 changes: 6 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: "3.8"
services:
web:
build: .
ports:
- "5000:5000"
6 changes: 4 additions & 2 deletions gohang.go
Original file line number Diff line number Diff line change
Expand Up @@ -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\"}"))
}
Expand All @@ -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)
}

0 comments on commit e90bde6

Please sign in to comment.