github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/Documentation/getting-started-guide.md (about) 1 # Getting Started with rkt 2 3 The following guide will show you how to build and run a self-contained Go app using rkt, the reference implementation of the [App Container Specification][appc-spec]. 4 If you're not on Linux, you should do all of this inside [the rkt Vagrant][rkt-vagrant]. 5 6 For a more complex example, please check the [build container examples](examples/build-container/README.md). 7 8 ## Create a hello go application 9 10 ```go 11 package main 12 13 import ( 14 "log" 15 "net/http" 16 ) 17 18 func main() { 19 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 20 log.Printf("request from %v\n", r.RemoteAddr) 21 w.Write([]byte("hello\n")) 22 }) 23 log.Fatal(http.ListenAndServe(":5000", nil)) 24 } 25 ``` 26 27 ### Build a statically linked Go binary 28 29 Next we need to build our application. 30 We are going to statically link our app so we can ship an App Container Image with no external dependencies. 31 32 With Go 1.9: 33 34 ``` 35 $ CGO_ENABLED=0 go build -ldflags '-extldflags "-static"' 36 ``` 37 38 Note that if you use [gccgo][gcc-go], the command is instead: 39 40 ``` 41 $ go build -compiler gccgo -gccgoflags '-static' 42 ``` 43 44 Before proceeding, verify that the produced binary is statically linked: 45 46 ``` 47 $ file hello 48 hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped 49 $ ldd hello 50 not a dynamic executable 51 ``` 52 53 ## Create the image 54 55 To create the image, we can use [`acbuild`][acbuild], which can be downloaded via one of the [releases in the containers/build repository][rkt-releases]. 56 57 The following commands will create an ACI containing our application and important metadata. 58 59 ```bash 60 acbuild begin 61 acbuild set-name example.com/hello 62 acbuild copy hello /bin/hello 63 acbuild set-exec /bin/hello 64 acbuild port add www tcp 5000 65 acbuild label add version 0.0.1 66 acbuild label add arch amd64 67 acbuild label add os linux 68 acbuild annotation add authors "Carly Container <carly@example.com>" 69 acbuild write hello-0.0.1-linux-amd64.aci 70 acbuild end 71 ``` 72 73 ## Run 74 75 ### Launch a local application image 76 77 ``` 78 # rkt --insecure-options=image run hello-0.0.1-linux-amd64.aci 79 ``` 80 81 Note that `--insecure-options=image` is required because, by default, rkt expects our images to be signed. 82 See the [Signing and Verification Guide][signing-guide] for more details. 83 84 At this point our hello app is running and ready to handle HTTP requests. 85 86 To stop the container, pass three escape characters (`^]^]^]`), which is generated by `Ctrl-]` on a US keyboard. You can also [run rkt as a daemon][rkt-daemon]. 87 88 ### Test with curl 89 90 By default, rkt will assign the running container an IP address. Use `rkt list` to discover what it is: 91 92 ``` 93 # rkt list 94 UUID APP IMAGE NAME STATE NETWORKS 95 885876b0 hello example.com/hello:0.0.1 running default:ip4=172.16.28.2 96 ``` 97 98 Then you can `curl` that IP on port 5000: 99 100 ``` 101 $ curl 172.16.28.2:5000 102 hello 103 ``` 104 105 [acbuild]: https://github.com/containers/build 106 [appc-spec]: https://github.com/appc/spec 107 [rkt-daemon]: subcommands/run.md#run-rkt-as-a-daemon 108 [rkt-releases]: https://github.com/containers/build/releases 109 [rkt-vagrant]: https://github.com/rkt/rkt/blob/master/Documentation/trying-out-rkt.md#rkt-using-vagrant 110 [signing-guide]: signing-and-verification-guide.md 111 [gcc-go]: https://golang.org/doc/install/gccgo