-
-
Notifications
You must be signed in to change notification settings - Fork 108
Implementation of docker-cloud project #39
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
Open
tgvashworth
wants to merge
15
commits into
main
Choose a base branch
from
impl/docker-cloud
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ee04e78
Initial commit of IMPLEMENTATION.md
tgvashworth c2145a2
add basic server and Dockerfile
tgvashworth 6034d49
Add a basic test with dockertest
tgvashworth 8dbbe62
configurable port
tgvashworth bacd4e1
add github action for running tests
tgvashworth 6de8865
instructions on github action
tgvashworth 1c02a11
add publish image github action
tgvashworth d3d1abf
configure AWS credentials
tgvashworth 870c2d9
switch to port 80
tgvashworth 3a076d5
move to push workflow trigger
tgvashworth 9ba14c3
remove noroot to mount port 80
tgvashworth 6d501ed
shift around plan order and add root handler
tgvashworth 7075719
Apply suggestions from code review
tgvashworth 6ae4121
further implementation docs tweaks
tgvashworth 820d4ea
simplify github action definitions
tgvashworth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.