From ee04e789a8fcf903bc636cdbb453f0bf117152e8 Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Tue, 20 Sep 2022 11:35:56 +0100 Subject: [PATCH 01/15] Initial commit of IMPLEMENTATION.md To accelerate this project, we'll begin with a set of tutorials which introduce the concepts with real-world use. Just this will take several hours for someone who hasn't done this before. Then the project will be to bring it all together: the implementation will do this, hopefully also including CI/CD. --- docker-cloud/IMPLEMENTATION.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 docker-cloud/IMPLEMENTATION.md diff --git a/docker-cloud/IMPLEMENTATION.md b/docker-cloud/IMPLEMENTATION.md new file mode 100644 index 000000000..a45bf69a4 --- /dev/null +++ b/docker-cloud/IMPLEMENTATION.md @@ -0,0 +1,33 @@ +# docker-cloud implementation + +To get familiar with Docker, complete parts 1, 2 and 3 of [this tutorial](https://docs.docker.com/get-started/): + +- Running applications with docker: `docker run -dp 80:80 docker/getting-started` +- Containers and images: process & filesystem isolation +- `Dockerfile`: a text-based script of instructions that is used to create a container image +- Starting, managing processes and images: `docker ps` and `docker rm -f`s + +Next work through the [Go Docker tutorial](https://docs.docker.com/language/golang/): + +- Dockerising a go application +- Starting and stopping containers +- Volumes & networking between docker containers +- Basics of docker-compose and CockroachDB +- GitHub actions for pushing the image to Docker Hub + +To get familiar with ECS, run through the [AWS tutorial](https://aws.amazon.com/getting-started/hands-on/deploy-docker-containers/): + +- Container & task: like a blueprint for your application +- Service & load balancing: launches and maintains copies of the task definition in your cluster +- Cluster: compute resources used to run the service & load balancing + +--- + +The task will be to bring this all together to run a local application on Elastic Container Service: + +- Build a simple Go server +- Dockerise it to run locally within a container +- Write tests that run against the docker container +- Push the image to ECR (not Docker Hub) +- Launch it in ECS using the UI +- Build GitHub actions automate CI/CD From c2145a275ac989455e0dd6ec14264068fe70e74c Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Tue, 20 Sep 2022 14:45:58 +0100 Subject: [PATCH 02/15] add basic server and Dockerfile --- docker-cloud/Dockerfile | 30 +++++++++++++++++++ docker-cloud/IMPLEMENTATION.md | 54 ++++++++++++++++++++++++++++++++++ docker-cloud/go.mod | 3 ++ docker-cloud/main.go | 23 +++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 docker-cloud/Dockerfile create mode 100644 docker-cloud/go.mod create mode 100644 docker-cloud/main.go diff --git a/docker-cloud/Dockerfile b/docker-cloud/Dockerfile new file mode 100644 index 000000000..88b6ebb6f --- /dev/null +++ b/docker-cloud/Dockerfile @@ -0,0 +1,30 @@ +# syntax=docker/dockerfile:1 + +## Build +FROM golang:1.19-bullseye as build + +WORKDIR /app + +COPY go.mod . +# COPY go.sum . + +RUN go mod download + +COPY *.go ./ + +RUN go build -o /out + +CMD [ "/out" ] + +## Deploy +FROM gcr.io/distroless/base-debian11 + +WORKDIR / + +COPY --from=build /out /out + +EXPOSE 8080 + +USER nonroot:nonroot + +ENTRYPOINT ["/out"] \ No newline at end of file diff --git a/docker-cloud/IMPLEMENTATION.md b/docker-cloud/IMPLEMENTATION.md index a45bf69a4..e19b38a89 100644 --- a/docker-cloud/IMPLEMENTATION.md +++ b/docker-cloud/IMPLEMENTATION.md @@ -31,3 +31,57 @@ The task will be to bring this all together to run a local application on Elasti - Push the image to ECR (not Docker Hub) - Launch it in ECS using the UI - Build GitHub actions automate CI/CD + +## Server + +```console +> curl localhost:8090/ping +pong +``` + +`Dockerfile` inc multi-stage build: + +```Dockerfile +# syntax=docker/dockerfile:1 + +## Build +FROM golang:1.19-bullseye as build + +WORKDIR /app + +COPY go.mod . +# COPY go.sum . + +RUN go mod download + +COPY *.go ./ + +RUN go build -o /out + +CMD [ "/out" ] + +## Deploy +FROM gcr.io/distroless/base-debian11 + +WORKDIR / + +COPY --from=build /out /out + +EXPOSE 8080 + +USER nonroot:nonroot + +ENTRYPOINT ["/out"] +``` + +Build & run: + +```console +> docker build . -t docker-cloud +[+] Building 22.6s (15/15) FINISHED +... +> docker run -dp 8090:8090 docker-cloud +306cf309f3970d5380cd07c3a54aead7ee8cf4f6726b752fecaec39e40da69f5 +> curl localhost:8090/ping +pong +``` diff --git a/docker-cloud/go.mod b/docker-cloud/go.mod new file mode 100644 index 000000000..f2dd1e215 --- /dev/null +++ b/docker-cloud/go.mod @@ -0,0 +1,3 @@ +module github.com/CodeYourFuture/immersive-go-course/docker-cloud + +go 1.19 diff --git a/docker-cloud/main.go b/docker-cloud/main.go new file mode 100644 index 000000000..0d67fab80 --- /dev/null +++ b/docker-cloud/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + "log" + "net/http" +) + +type Config struct { + Port int +} + +func main() { + config := Config{ + Port: 8090, + } + + http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("pong")) + }) + + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)) +} From 6034d4952f972a0c8e95e8065a1f68d0e91d6543 Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Tue, 20 Sep 2022 17:39:55 +0100 Subject: [PATCH 03/15] Add a basic test with dockertest --- docker-cloud/Dockerfile | 2 +- docker-cloud/IMPLEMENTATION.md | 10 +++ docker-cloud/go.mod | 29 +++++++ docker-cloud/go.sum | 145 +++++++++++++++++++++++++++++++++ docker-cloud/main.go | 2 +- docker-cloud/main_test.go | 64 +++++++++++++++ 6 files changed, 250 insertions(+), 2 deletions(-) create mode 100644 docker-cloud/go.sum create mode 100644 docker-cloud/main_test.go diff --git a/docker-cloud/Dockerfile b/docker-cloud/Dockerfile index 88b6ebb6f..0ae75f693 100644 --- a/docker-cloud/Dockerfile +++ b/docker-cloud/Dockerfile @@ -6,7 +6,7 @@ FROM golang:1.19-bullseye as build WORKDIR /app COPY go.mod . -# COPY go.sum . +COPY go.sum . RUN go mod download diff --git a/docker-cloud/IMPLEMENTATION.md b/docker-cloud/IMPLEMENTATION.md index e19b38a89..bf84cbe14 100644 --- a/docker-cloud/IMPLEMENTATION.md +++ b/docker-cloud/IMPLEMENTATION.md @@ -85,3 +85,13 @@ Build & run: > curl localhost:8090/ping pong ``` + +## Tests + +[dockertest](https://github.com/ory/dockertest) — principle is to test against real running services, end to end + +```console +go get -u github.com/ory/dockertest/v3 +``` + +Following [docs here](https://github.com/ory/dockertest) and [example here](https://github.com/olliefr/docker-gs-ping), write some tests. diff --git a/docker-cloud/go.mod b/docker-cloud/go.mod index f2dd1e215..4034a4c27 100644 --- a/docker-cloud/go.mod +++ b/docker-cloud/go.mod @@ -1,3 +1,32 @@ module github.com/CodeYourFuture/immersive-go-course/docker-cloud go 1.19 + +require ( + github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect + github.com/Microsoft/go-winio v0.5.2 // indirect + github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect + github.com/cenkalti/backoff/v4 v4.1.3 // indirect + github.com/containerd/continuity v0.3.0 // indirect + github.com/docker/cli v20.10.18+incompatible // indirect + github.com/docker/docker v20.10.18+incompatible // indirect + github.com/docker/go-connections v0.4.0 // indirect + github.com/docker/go-units v0.5.0 // indirect + github.com/gogo/protobuf v1.3.2 // indirect + github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect + github.com/imdario/mergo v0.3.13 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae // indirect + github.com/opencontainers/go-digest v1.0.0 // indirect + github.com/opencontainers/image-spec v1.0.2 // indirect + github.com/opencontainers/runc v1.1.4 // indirect + github.com/ory/dockertest/v3 v3.9.1 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/sirupsen/logrus v1.9.0 // indirect + github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect + github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect + github.com/xeipuuv/gojsonschema v1.2.0 // indirect + golang.org/x/net v0.0.0-20220919232410-f2f64ebce3c1 // indirect + golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect +) diff --git a/docker-cloud/go.sum b/docker-cloud/go.sum new file mode 100644 index 000000000..d00ef7fb7 --- /dev/null +++ b/docker-cloud/go.sum @@ -0,0 +1,145 @@ +github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8= +github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/Microsoft/go-winio v0.5.2 h1:a9IhgEQBCUEk6QCdml9CiJGhAws+YwffDHEMp1VMrpA= +github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY= +github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw= +github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk= +github.com/cenkalti/backoff/v4 v4.1.3 h1:cFAlzYUlVYDysBEH2T5hyJZMh3+5+WCBvSnK6Q8UtC4= +github.com/cenkalti/backoff/v4 v4.1.3/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw= +github.com/checkpoint-restore/go-criu/v5 v5.3.0/go.mod h1:E/eQpaFtUKGOOSEBZgmKAcn+zUUwWxqcaKZlF54wK8E= +github.com/cilium/ebpf v0.7.0/go.mod h1:/oI2+1shJiTGAMgl6/RgJr36Eo1jzrRcAWbcXO2usCA= +github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U= +github.com/containerd/continuity v0.3.0 h1:nisirsYROK15TAMVukJOUyGJjz4BNQJBVsNvAXZJ/eg= +github.com/containerd/continuity v0.3.0/go.mod h1:wJEAIwKOm/pBZuBd0JmeTvnLquTB1Ag8espWhkykbPM= +github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/cyphar/filepath-securejoin v0.2.3/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/docker/cli v20.10.18+incompatible h1:f/GQLsVpo10VvToRay2IraVA1wHz9KktZyjev3SIVDU= +github.com/docker/cli v20.10.18+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8= +github.com/docker/docker v20.10.18+incompatible h1:SN84VYXTBNGn92T/QwIRPlum9zfemfitN7pbsp26WSc= +github.com/docker/docker v20.10.18+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ= +github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= +github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= +github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/godbus/dbus/v5 v5.0.6/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= +github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= +github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/moby/sys/mountinfo v0.5.0/go.mod h1:3bMD3Rg+zkqx8MRYPi7Pyb0Ie97QEBmdxbhnCLlSvSU= +github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae h1:O4SWKdcHVCvYqyDV+9CJA1fcDN2L11Bule0iFy3YlAI= +github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae/go.mod h1:E2VnQOmVuvZB6UYnnDB0qG5Nq/1tD9acaOpo6xmt0Kw= +github.com/mrunalp/fileutils v0.5.0/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ= +github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= +github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= +github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM= +github.com/opencontainers/image-spec v1.0.2/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0= +github.com/opencontainers/runc v1.1.4 h1:nRCz/8sKg6K6jgYAFLDlXzPeITBZJyX28DBVhWD+5dg= +github.com/opencontainers/runc v1.1.4/go.mod h1:1J5XiS+vdZ3wCyZybsuxXZWGrgSr8fFJHLXuG2PsnNg= +github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= +github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI= +github.com/ory/dockertest/v3 v3.9.1 h1:v4dkG+dlu76goxMiTT2j8zV7s4oPPEppKT8K8p2f1kY= +github.com/ory/dockertest/v3 v3.9.1/go.mod h1:42Ir9hmvaAPm0Mgibk6mBPi7SFvTXxEcnztDYOJ//uM= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/seccomp/libseccomp-golang v0.9.2-0.20220502022130-f33da4d89646/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= +github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= +github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= +github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE= +github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU= +github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= +github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo= +github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU= +github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0= +github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ= +github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74= +github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y= +github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220919232410-f2f64ebce3c1 h1:TWZxd/th7FbRSMret2MVQdlI8uT49QEtwZdvJrxjEHU= +golang.org/x/net v0.0.0-20220919232410-f2f64ebce3c1/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191115151921-52ab43148777/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 h1:h+EGohizhe9XlX18rfpa8k8RAc5XyaeamM+0VHRd4lc= +golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= diff --git a/docker-cloud/main.go b/docker-cloud/main.go index 0d67fab80..bd63c1ea2 100644 --- a/docker-cloud/main.go +++ b/docker-cloud/main.go @@ -12,7 +12,7 @@ type Config struct { func main() { config := Config{ - Port: 8090, + Port: 8080, } http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) { diff --git a/docker-cloud/main_test.go b/docker-cloud/main_test.go new file mode 100644 index 000000000..932fc898b --- /dev/null +++ b/docker-cloud/main_test.go @@ -0,0 +1,64 @@ +package main_test + +import ( + "fmt" + "io" + "log" + "net/http" + "os" + "testing" + + "github.com/ory/dockertest/v3" +) + +var pool *dockertest.Pool +var resource *dockertest.Resource + +func TestMain(m *testing.M) { + var err error + pool, err = dockertest.NewPool("") + if err != nil { + log.Fatalf("Could not connect to docker: %s", err) + } + + resource, err = pool.BuildAndRun("docker-cloud-test", "./Dockerfile", nil) + if err != nil { + log.Fatalf("Could not build/run: %s", err) + } + + code := m.Run() + + if err := pool.Purge(resource); err != nil { + log.Fatalf("Could not cleanup: %s", err) + } + + os.Exit(code) +} + +func TestPing(t *testing.T) { + var err error + + var resp *http.Response + err = pool.Retry(func() error { + var err error + t.Log(resource.GetPort("8080/tcp")) + resp, err = http.Get(fmt.Sprint("http://localhost:", resource.GetPort("8080/tcp"), "/ping")) + if err != nil { + t.Log("container not ready, waiting...") + return err + } + return nil + }) + + if err != nil { + t.Fatalf("request: %s", err) + } + body, err := io.ReadAll(resp.Body) + if err != nil { + t.Fatalf("request: %s", err) + } + + if string(body) != "pong" { + t.Fatal("request: response was not pong") + } +} From 8dbbe629c949499b31074802f40eff80da2f1947 Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Tue, 20 Sep 2022 20:18:04 +0100 Subject: [PATCH 04/15] configurable port --- docker-cloud/main.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/docker-cloud/main.go b/docker-cloud/main.go index bd63c1ea2..8b035a64d 100644 --- a/docker-cloud/main.go +++ b/docker-cloud/main.go @@ -4,20 +4,26 @@ import ( "fmt" "log" "net/http" + "os" ) type Config struct { - Port int + Port string } func main() { + port := os.Getenv("HTTP_PORT") + if port == "" { + port = "8080" + } + config := Config{ - Port: 8080, + Port: port, } http.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("pong")) }) - log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)) + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", config.Port), nil)) } From bacd4e1294e9e7304399c4a517fe020f849d3804 Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Tue, 20 Sep 2022 20:28:20 +0100 Subject: [PATCH 05/15] add github action for running tests --- .github/workflows/docker-cloud-test.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .github/workflows/docker-cloud-test.yml diff --git a/.github/workflows/docker-cloud-test.yml b/.github/workflows/docker-cloud-test.yml new file mode 100644 index 000000000..4bff01296 --- /dev/null +++ b/.github/workflows/docker-cloud-test.yml @@ -0,0 +1,20 @@ +name: docker-cloud go tests +on: [push] +jobs: + test: + runs-on: ubuntu-latest + defaults: + run: + working-directory: docker-cloud + steps: + - uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version-file: "docker-cloud/go.mod" + cache-dependency-path: "docker-cloud/go.sum" + cache: true + - name: Build + run: go build -v ./... + - name: Test + run: go test -v ./... From 6de8865b7356db3c0ac035161b6b2a2723f112c4 Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Tue, 20 Sep 2022 20:36:50 +0100 Subject: [PATCH 06/15] instructions on github action --- docker-cloud/IMPLEMENTATION.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docker-cloud/IMPLEMENTATION.md b/docker-cloud/IMPLEMENTATION.md index bf84cbe14..93d6dac3c 100644 --- a/docker-cloud/IMPLEMENTATION.md +++ b/docker-cloud/IMPLEMENTATION.md @@ -95,3 +95,26 @@ go get -u github.com/ory/dockertest/v3 ``` Following [docs here](https://github.com/ory/dockertest) and [example here](https://github.com/olliefr/docker-gs-ping), write some tests. + +## GitHub action to run tests + +Follow [guide on GitHub](https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go) to get GitHub action testing the code. + +Pay attention to: + +```yml +defaults: + run: + working-directory: docker-cloud +``` + +And: + +```yml +- name: Set up Go + uses: actions/setup-go@v3 + with: + go-version-file: "docker-cloud/go.mod" + cache-dependency-path: "docker-cloud/go.sum" + cache: true +``` From 1c02a11dcb7625786ca668bc2d369ce0eb1f266f Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Tue, 20 Sep 2022 20:54:50 +0100 Subject: [PATCH 07/15] add publish image github action --- .github/workflows/docker-cloud-publish.yml | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 .github/workflows/docker-cloud-publish.yml diff --git a/.github/workflows/docker-cloud-publish.yml b/.github/workflows/docker-cloud-publish.yml new file mode 100644 index 000000000..9c95228c3 --- /dev/null +++ b/.github/workflows/docker-cloud-publish.yml @@ -0,0 +1,24 @@ +name: docker-cloud go publish +on: push +jobs: + publish: + runs-on: ubuntu-latest + defaults: + run: + working-directory: docker-cloud + steps: + - uses: actions/checkout@v3 + - name: Login to Amazon ECR Public + id: login-ecr-public + uses: aws-actions/amazon-ecr-login@v1 + with: + registry-type: public + - name: Build, tag, and push docker image to Amazon ECR Public + env: + REGISTRY: ${{ steps.login-ecr-public.outputs.registry }} + REGISTRY_ALIAS: w0k4j6h5 + REPOSITORY: immersive-go-course/docker-cloud + IMAGE_TAG: ${{ github.sha }} + run: | + docker build -t $REGISTRY/$REGISTRY_ALIAS/$REPOSITORY:$IMAGE_TAG . + docker push $REGISTRY/$REGISTRY_ALIAS/$REPOSITORY:$IMAGE_TAG From d3d1abf3fa5d55f2ce3b0601deea2586a5950d36 Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Tue, 20 Sep 2022 21:38:10 +0100 Subject: [PATCH 08/15] configure AWS credentials --- .github/workflows/docker-cloud-publish.yml | 9 ++++++++- docker-cloud/IMPLEMENTATION.md | 13 +++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/.github/workflows/docker-cloud-publish.yml b/.github/workflows/docker-cloud-publish.yml index 9c95228c3..3afe91f77 100644 --- a/.github/workflows/docker-cloud-publish.yml +++ b/.github/workflows/docker-cloud-publish.yml @@ -1,13 +1,20 @@ name: docker-cloud go publish -on: push +on: workflow_dispatch jobs: publish: + permissions: + id-token: write runs-on: ubuntu-latest defaults: run: working-directory: docker-cloud steps: - uses: actions/checkout@v3 + - name: Configure AWS Credentials + uses: aws-actions/configure-aws-credentials@v1 + with: + role-to-assume: arn:aws:iam::464590638146:role/GitHubActionECRPublicPushImage + aws-region: us-east-1 - name: Login to Amazon ECR Public id: login-ecr-public uses: aws-actions/amazon-ecr-login@v1 diff --git a/docker-cloud/IMPLEMENTATION.md b/docker-cloud/IMPLEMENTATION.md index 93d6dac3c..f4a32ae3a 100644 --- a/docker-cloud/IMPLEMENTATION.md +++ b/docker-cloud/IMPLEMENTATION.md @@ -118,3 +118,16 @@ And: cache-dependency-path: "docker-cloud/go.sum" cache: true ``` + +### Action to publish image + +This is complex. + +[Guide here](https://benoitboure.com/securely-access-your-aws-resources-from-github-actions) was very helpful. + +Actions used: + +- https://github.com/aws-actions/configure-aws-credentials +- https://github.com/aws-actions/amazon-ecr-login + +Consider: setting up a CYF public ECR repository with the right permissions. From 870c2d93f41af01be2a090cd16593660b6c95083 Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Tue, 20 Sep 2022 22:41:27 +0100 Subject: [PATCH 09/15] switch to port 80 --- docker-cloud/Dockerfile | 2 +- docker-cloud/main.go | 2 +- docker-cloud/main_test.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docker-cloud/Dockerfile b/docker-cloud/Dockerfile index 0ae75f693..cfb8652cb 100644 --- a/docker-cloud/Dockerfile +++ b/docker-cloud/Dockerfile @@ -23,7 +23,7 @@ WORKDIR / COPY --from=build /out /out -EXPOSE 8080 +EXPOSE 80 USER nonroot:nonroot diff --git a/docker-cloud/main.go b/docker-cloud/main.go index 8b035a64d..60265e652 100644 --- a/docker-cloud/main.go +++ b/docker-cloud/main.go @@ -14,7 +14,7 @@ type Config struct { func main() { port := os.Getenv("HTTP_PORT") if port == "" { - port = "8080" + port = "80" } config := Config{ diff --git a/docker-cloud/main_test.go b/docker-cloud/main_test.go index 932fc898b..2511bca50 100644 --- a/docker-cloud/main_test.go +++ b/docker-cloud/main_test.go @@ -41,8 +41,8 @@ func TestPing(t *testing.T) { var resp *http.Response err = pool.Retry(func() error { var err error - t.Log(resource.GetPort("8080/tcp")) - resp, err = http.Get(fmt.Sprint("http://localhost:", resource.GetPort("8080/tcp"), "/ping")) + t.Log(resource.GetPort("80/tcp")) + resp, err = http.Get(fmt.Sprint("http://localhost:", resource.GetPort("80/tcp"), "/ping")) if err != nil { t.Log("container not ready, waiting...") return err From 3a076d565b89367012dde99713cf9bb1db1fc84c Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Tue, 20 Sep 2022 22:45:08 +0100 Subject: [PATCH 10/15] move to push workflow trigger workflow_dispatch does not work becuase the workflow file is not on the main branch yet. --- .github/workflows/docker-cloud-publish.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker-cloud-publish.yml b/.github/workflows/docker-cloud-publish.yml index 3afe91f77..7cc66a853 100644 --- a/.github/workflows/docker-cloud-publish.yml +++ b/.github/workflows/docker-cloud-publish.yml @@ -1,5 +1,5 @@ name: docker-cloud go publish -on: workflow_dispatch +on: push jobs: publish: permissions: From 9ba14c35a06e4b4c68ac35bc8508b786558eec12 Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Wed, 21 Sep 2022 08:52:34 +0100 Subject: [PATCH 11/15] remove noroot to mount port 80 --- docker-cloud/Dockerfile | 2 -- docker-cloud/IMPLEMENTATION.md | 4 +--- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/docker-cloud/Dockerfile b/docker-cloud/Dockerfile index cfb8652cb..e0c49dad6 100644 --- a/docker-cloud/Dockerfile +++ b/docker-cloud/Dockerfile @@ -25,6 +25,4 @@ COPY --from=build /out /out EXPOSE 80 -USER nonroot:nonroot - ENTRYPOINT ["/out"] \ No newline at end of file diff --git a/docker-cloud/IMPLEMENTATION.md b/docker-cloud/IMPLEMENTATION.md index f4a32ae3a..78f1875b3 100644 --- a/docker-cloud/IMPLEMENTATION.md +++ b/docker-cloud/IMPLEMENTATION.md @@ -67,9 +67,7 @@ WORKDIR / COPY --from=build /out /out -EXPOSE 8080 - -USER nonroot:nonroot +EXPOSE 80 ENTRYPOINT ["/out"] ``` From 6d501ed12aa9b3fdb3cde573b1f90fa6501c2ff0 Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Wed, 21 Sep 2022 09:14:08 +0100 Subject: [PATCH 12/15] shift around plan order and add root handler --- docker-cloud/IMPLEMENTATION.md | 2 +- docker-cloud/main.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/docker-cloud/IMPLEMENTATION.md b/docker-cloud/IMPLEMENTATION.md index 78f1875b3..d3f6775ca 100644 --- a/docker-cloud/IMPLEMENTATION.md +++ b/docker-cloud/IMPLEMENTATION.md @@ -28,9 +28,9 @@ The task will be to bring this all together to run a local application on Elasti - Build a simple Go server - Dockerise it to run locally within a container - Write tests that run against the docker container +- Build GitHub actions automate CI/CD - Push the image to ECR (not Docker Hub) - Launch it in ECS using the UI -- Build GitHub actions automate CI/CD ## Server diff --git a/docker-cloud/main.go b/docker-cloud/main.go index 60265e652..c9ad2bebe 100644 --- a/docker-cloud/main.go +++ b/docker-cloud/main.go @@ -25,5 +25,9 @@ func main() { w.Write([]byte("pong")) }) + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("Hello, world.")) + }) + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", config.Port), nil)) } From 7075719a687d15afc39f9aa5412b8184fef9ae89 Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Thu, 22 Sep 2022 11:53:29 +0100 Subject: [PATCH 13/15] Apply suggestions from code review Co-authored-by: Daniel Wagner-Hall --- docker-cloud/IMPLEMENTATION.md | 15 ++++++++------- docker-cloud/main_test.go | 15 ++++++++------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/docker-cloud/IMPLEMENTATION.md b/docker-cloud/IMPLEMENTATION.md index d3f6775ca..d1ec00be4 100644 --- a/docker-cloud/IMPLEMENTATION.md +++ b/docker-cloud/IMPLEMENTATION.md @@ -1,13 +1,13 @@ # docker-cloud implementation -To get familiar with Docker, complete parts 1, 2 and 3 of [this tutorial](https://docs.docker.com/get-started/): +To get familiar with Docker, complete parts 1, 2 and 3 of [this tutorial](https://docs.docker.com/get-started/), after which you should know about: - Running applications with docker: `docker run -dp 80:80 docker/getting-started` - Containers and images: process & filesystem isolation - `Dockerfile`: a text-based script of instructions that is used to create a container image - Starting, managing processes and images: `docker ps` and `docker rm -f`s -Next work through the [Go Docker tutorial](https://docs.docker.com/language/golang/): +Next work through the [Go Docker tutorial](https://docs.docker.com/language/golang/), after which you should know about: - Dockerising a go application - Starting and stopping containers @@ -15,7 +15,7 @@ Next work through the [Go Docker tutorial](https://docs.docker.com/language/gola - Basics of docker-compose and CockroachDB - GitHub actions for pushing the image to Docker Hub -To get familiar with ECS, run through the [AWS tutorial](https://aws.amazon.com/getting-started/hands-on/deploy-docker-containers/): +To get familiar with ECS, run through the [AWS tutorial](https://aws.amazon.com/getting-started/hands-on/deploy-docker-containers/), after which you should know about: - Container & task: like a blueprint for your application - Service & load balancing: launches and maintains copies of the task definition in your cluster @@ -23,7 +23,7 @@ To get familiar with ECS, run through the [AWS tutorial](https://aws.amazon.com/ --- -The task will be to bring this all together to run a local application on Elastic Container Service: +The task will be to bring this all together to run an application that we've written on Elastic Container Service: - Build a simple Go server - Dockerise it to run locally within a container @@ -34,12 +34,13 @@ The task will be to bring this all together to run a local application on Elasti ## Server +Write a server in Go with this behaviour: + ```console > curl localhost:8090/ping pong -``` -`Dockerfile` inc multi-stage build: +Write a `Dockerfile` including a multi-stage build: ```Dockerfile # syntax=docker/dockerfile:1 @@ -96,7 +97,7 @@ Following [docs here](https://github.com/ory/dockertest) and [example here](http ## GitHub action to run tests -Follow [guide on GitHub](https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go) to get GitHub action testing the code. +Follow [this guide on GitHub](https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go) to get a GitHub action testing the code. Pay attention to: diff --git a/docker-cloud/main_test.go b/docker-cloud/main_test.go index 2511bca50..6b2ee92cb 100644 --- a/docker-cloud/main_test.go +++ b/docker-cloud/main_test.go @@ -18,7 +18,7 @@ func TestMain(m *testing.M) { var err error pool, err = dockertest.NewPool("") if err != nil { - log.Fatalf("Could not connect to docker: %s", err) + log.Fatalf("Could not connect to docker daemon: %s", err) } resource, err = pool.BuildAndRun("docker-cloud-test", "./Dockerfile", nil) @@ -26,13 +26,13 @@ func TestMain(m *testing.M) { log.Fatalf("Could not build/run: %s", err) } - code := m.Run() + exitCode := m.Run() if err := pool.Purge(resource); err != nil { log.Fatalf("Could not cleanup: %s", err) } - os.Exit(code) + os.Exit(exitCode) } func TestPing(t *testing.T) { @@ -51,14 +51,15 @@ func TestPing(t *testing.T) { }) if err != nil { - t.Fatalf("request: %s", err) + t.Fatalf("response error after retries: %s", err) } body, err := io.ReadAll(resp.Body) if err != nil { - t.Fatalf("request: %s", err) + t.Fatalf("error reading response body: %s", err) } - if string(body) != "pong" { - t.Fatal("request: response was not pong") + want := "pong" + if got := string(body); want != got { + t.Fatalf("bad response: want %q got %q", want, got) } } From 6ae4121cdc1b95178cb5f443c0a352c2539aa185 Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Thu, 22 Sep 2022 12:42:54 +0100 Subject: [PATCH 14/15] further implementation docs tweaks --- docker-cloud/Dockerfile | 6 ++---- docker-cloud/IMPLEMENTATION.md | 13 ++++++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/docker-cloud/Dockerfile b/docker-cloud/Dockerfile index e0c49dad6..d2eba39fc 100644 --- a/docker-cloud/Dockerfile +++ b/docker-cloud/Dockerfile @@ -5,8 +5,8 @@ FROM golang:1.19-bullseye as build WORKDIR /app -COPY go.mod . -COPY go.sum . +COPY go.mod ./ +COPY go.sum ./ RUN go mod download @@ -14,8 +14,6 @@ COPY *.go ./ RUN go build -o /out -CMD [ "/out" ] - ## Deploy FROM gcr.io/distroless/base-debian11 diff --git a/docker-cloud/IMPLEMENTATION.md b/docker-cloud/IMPLEMENTATION.md index d1ec00be4..7790ce7ee 100644 --- a/docker-cloud/IMPLEMENTATION.md +++ b/docker-cloud/IMPLEMENTATION.md @@ -39,6 +39,7 @@ Write a server in Go with this behaviour: ```console > curl localhost:8090/ping pong +``` Write a `Dockerfile` including a multi-stage build: @@ -50,8 +51,10 @@ FROM golang:1.19-bullseye as build WORKDIR /app -COPY go.mod . -# COPY go.sum . +COPY go.mod ./ +# This line need to be commented out until such a file exists, once we add a +# dependency on `dockertest`. +# COPY go.sum ./ RUN go mod download @@ -59,8 +62,6 @@ COPY *.go ./ RUN go build -o /out -CMD [ "/out" ] - ## Deploy FROM gcr.io/distroless/base-debian11 @@ -87,7 +88,7 @@ pong ## Tests -[dockertest](https://github.com/ory/dockertest) — principle is to test against real running services, end to end +For writing tests, we'll use [dockertest](https://github.com/ory/dockertest). The principle is to test against real running services, end to end, in containers. ```console go get -u github.com/ory/dockertest/v3 @@ -95,6 +96,8 @@ go get -u github.com/ory/dockertest/v3 Following [docs here](https://github.com/ory/dockertest) and [example here](https://github.com/olliefr/docker-gs-ping), write some tests. +Make sure to uncomment `COPY go.sum ./` from the `Dockerfile`. + ## GitHub action to run tests Follow [this guide on GitHub](https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go) to get a GitHub action testing the code. From 820d4eaa3196e03ee05f1d30e14fdc54ff08f767 Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Thu, 22 Sep 2022 12:44:19 +0100 Subject: [PATCH 15/15] simplify github action definitions --- .github/workflows/docker-cloud-test.yml | 20 --------------- ...ker-cloud-publish.yml => docker-cloud.yml} | 25 +++++++++++++++---- 2 files changed, 20 insertions(+), 25 deletions(-) delete mode 100644 .github/workflows/docker-cloud-test.yml rename .github/workflows/{docker-cloud-publish.yml => docker-cloud.yml} (66%) diff --git a/.github/workflows/docker-cloud-test.yml b/.github/workflows/docker-cloud-test.yml deleted file mode 100644 index 4bff01296..000000000 --- a/.github/workflows/docker-cloud-test.yml +++ /dev/null @@ -1,20 +0,0 @@ -name: docker-cloud go tests -on: [push] -jobs: - test: - runs-on: ubuntu-latest - defaults: - run: - working-directory: docker-cloud - steps: - - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v3 - with: - go-version-file: "docker-cloud/go.mod" - cache-dependency-path: "docker-cloud/go.sum" - cache: true - - name: Build - run: go build -v ./... - - name: Test - run: go test -v ./... diff --git a/.github/workflows/docker-cloud-publish.yml b/.github/workflows/docker-cloud.yml similarity index 66% rename from .github/workflows/docker-cloud-publish.yml rename to .github/workflows/docker-cloud.yml index 7cc66a853..da9f9823d 100644 --- a/.github/workflows/docker-cloud-publish.yml +++ b/.github/workflows/docker-cloud.yml @@ -1,13 +1,28 @@ -name: docker-cloud go publish -on: push +name: docker-cloud go tests +on: [push] +defaults: + run: + working-directory: docker-cloud jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version-file: "docker-cloud/go.mod" + cache-dependency-path: "docker-cloud/go.sum" + cache: true + - name: Build + run: go build -v ./... + - name: Test + run: go test -v ./... publish: + needs: test permissions: id-token: write runs-on: ubuntu-latest - defaults: - run: - working-directory: docker-cloud steps: - uses: actions/checkout@v3 - name: Configure AWS Credentials