Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of docker-cloud project #39

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .github/workflows/docker-cloud.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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
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
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
26 changes: 26 additions & 0 deletions docker-cloud/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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

## Deploy
FROM gcr.io/distroless/base-debian11

WORKDIR /

COPY --from=build /out /out

EXPOSE 80

ENTRYPOINT ["/out"]
135 changes: 135 additions & 0 deletions docker-cloud/IMPLEMENTATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# docker-cloud implementation

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/), after which you should know about:

- 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/), 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
- Cluster: compute resources used to run the service & load balancing

---

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
- 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

## Server

Write a server in Go with this behaviour:

```console
> curl localhost:8090/ping
pong
```

Write a `Dockerfile` including a multi-stage build:

```Dockerfile
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same questions apply as above

# syntax=docker/dockerfile:1

## Build
FROM golang:1.19-bullseye as build

WORKDIR /app

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

COPY *.go ./

RUN go build -o /out

## Deploy
FROM gcr.io/distroless/base-debian11

WORKDIR /

COPY --from=build /out /out

EXPOSE 80

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
```

## Tests

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
```

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.

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
```

### 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.
32 changes: 32 additions & 0 deletions docker-cloud/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +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
)
Loading