github.com/buildtool/build-tools@v0.2.29-0.20240322150259-6a1d0a553c23/www/docs/quickstart.md (about) 1 # Quickstart 2 3 ## Pre requisites: 4 In order to work with these tools you need: 5 6 - Buildtools [installed](/installation/) (of course) 7 - Docker - read more about options [here](https://www.docker.com/get-started) 8 - Kubernetes - if you're using for example [Docker for Mac](https://docs.docker.com/docker-for-mac/install/) 9 Kubernetes can easily be enabled. 10 11 In this example we will build, push and deploy a sample Go project. 12 13 Create a Git repository and add a single main package with a http server: 14 15 ```shell 16 mkdir quickstart 17 cd quickstart 18 git init 19 ``` 20 21 ```go 22 // main.go 23 package main 24 25 import ( 26 "fmt" 27 "net/http" 28 ) 29 30 func main() { 31 http.HandleFunc("/", HelloServer) 32 http.ListenAndServe(":8080", nil) 33 } 34 35 func HelloServer(w http.ResponseWriter, r *http.Request) { 36 fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:]) 37 } 38 ``` 39 40 Add a Dockerfile describing how to build your code: 41 42 ```dockerfile 43 # Dockerfile 44 FROM golang:1.15 as build 45 WORKDIR /build 46 ADD . /build 47 48 RUN GOOS=linux GOARCH=amd64 go build main.go 49 50 FROM debian:buster-slim 51 COPY --from=build /build/main / 52 CMD ["/main"] 53 ``` 54 55 Add the files to source control and commit: 56 ```sh 57 git add . 58 git commit -m "Init" 59 ``` 60 Build the docker image: 61 62 ```sh 63 build 64 ``` 65 66 After the build completes you should see output like 67 68 ```sh 69 ... 70 Successfully tagged noregistry/quickstart:latest 71 ``` 72 73 Try to run your newly built docker image: 74 ```sh 75 docker run --rm -p 8080:8080 noregistry/quickstart:latest 76 ``` 77 and try to access it: 78 ```sh 79 curl localhost:8080/buildtools 80 ``` 81 You should see a response like: 82 ```sh 83 Hello, buildtools! 84 ``` 85 86 Let's try to deploy it to our local Kubernetes cluster, in order for this to work we need a 87 Kubernetes descriptor file. 88 Create a `k8s` folder and a file `deploy.yaml`: 89 90 ```sh 91 # k8s/deploy.yaml 92 apiVersion: apps/v1 93 kind: Deployment 94 metadata: 95 name: quickstart 96 annotations: 97 kubernetes.io/change-cause: "${TIMESTAMP} Deployed commit id: ${COMMIT}" 98 labels: 99 app: quickstart 100 spec: 101 replicas: 1 102 selector: 103 matchLabels: 104 app: quickstart 105 template: 106 metadata: 107 labels: 108 app: quickstart 109 spec: 110 containers: 111 - name: quickstart 112 imagePullPolicy: IfNotPresent 113 image: ${IMAGE} 114 115 ``` 116 117 This will create [Kubernetes deployment](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/), 118 basically starting your application inside the Kubernetes cluster. 119 120 ```sh 121 deploy --context docker-desktop 122 ``` 123 124 ```sh 125 kubectl --context docker-desktop get pods 126 127 quickstart-b4c5bc467-lqk6r 1/1 Running 0 3s 128 ```