Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ruanbekker/golang-build-small-images
Demo of Building Small Golang Images
https://github.com/ruanbekker/golang-build-small-images
Last synced: about 2 months ago
JSON representation
Demo of Building Small Golang Images
- Host: GitHub
- URL: https://github.com/ruanbekker/golang-build-small-images
- Owner: ruanbekker
- Created: 2019-04-03T10:23:04.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-04-03T12:21:26.000Z (over 5 years ago)
- Last Synced: 2024-07-08T16:40:05.719Z (2 months ago)
- Language: Go
- Size: 4.88 KB
- Stars: 2
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# golang-build-small-images
Demo of Building Small Golang Images![](https://user-images.githubusercontent.com/567298/55478306-aabb0600-561b-11e9-9cc6-730fadb4beeb.png)
## About
So we thinking lets go with alpine right? Yeah sure lets build a small, lekke, but useless app running on go with alpine.Let's package our app to an image:
```
❯ docker build -t mygolangapp:using-alpine .
```Inspect the size of our image, as you can see it being 310MB
```
❯ docker images "mygolangapp:*"
REPOSITORY TAG IMAGE ID CREATED SIZE
mygolangapp using-alpine eea1d7bde218 About a minute ago 310MB
```Just make sure it actually works:
```
❯ docker run mygolangapp:using-alpine
Number of words: 16
Selected index number: 11
Selected word is: mountain lion
```But for something just returning random selected text, 310MB is a bit crazy.
## Multi Stage Builds
As Go binaries are self-contained, we can make use of docker's multi stage builds, where we can build our application on alpine and use the binary on a scratch image:
Build it:
```
❯ docker build -t mygolangapp:using-multistage -f Dockerfile.multi .
```Notice that the image is only 2.01MB, say w000t!
```
❯ docker images "mygolangapp:*"
REPOSITORY TAG IMAGE ID CREATED SIZE
mygolangapp using-multistage 31474c61ba5b 15 seconds ago 2.01MB
mygolangapp using-alpine eea1d7bde218 2 minutes ago 310MB
```Run the app:
```
❯ docker run mygolangapp:using-multistage
Number of words: 16
Selected index number: 5
Selected word is: spider
```