8 ms·
Containerize Go and SQLite with Docker
- latchkey 5y agoAdd -ldflags '-s -w' to go build to strip DWARF, symbol table and debug info. See also: -trimpath
- pstuart 5y agoAnd if you can spare an extra ~150ms startup time: https://blog.filippo.io/shrink-your-go-binaries-with-this-one-weird-trick/ https://blog.filippo.io/shrink-your-go-binaries-with-this-on...
- latchkey 5y agoThat article is about -s -w and goupx. goupx is not necessary after go 1.6. https://github.com/pwaller/goupx#update-20160310 https://github.com/pwaller/goupx#update-20160310
- sugarkjube 5y agogoupx isn't. upx still is.
- deleted 5y ago[deleted]
- pstuart 5y agoIt's actually about upx, which is still relevant. I didn't notice the goupx bit but it doesn't diminish the value of upx, which was my intention in sharing.
- newman314 5y agoSerious question: Is upx worth the tradeoff in 2022? Trying to decide if it's worth working into the containers that I maintain.
- dlor 5y agoProbably not for Go in containers since binaries end up compressed anyway as part of the OCI layers. But Go binaries do get quite large so if you're distributing them other ways, it might be useful.
- mkdirp 5y agoI totally understand removing annotations in the DWARF tables since they're not really needed in a production environment, but why would you want to remove stack traces? They provide invaluable information in a production system, and removing them increases the start up time with negligible difference. In reality you'll tell whatever deployment system to deploy your binary/container, and you'll just wait until it's done. In such cases, 150ms won't make a difference in the grand scheme of things due to other systems such as your CI and other things in your pipeline.
- pstuart 5y agoI must confess to copypasta for this approach. Yes, stack traces are incredibly valuable. The upx compression itself does seem to be a no-brainer with the exception of any situation where that 150ms time actually matters.
- Cthulhu_ 5y agoDoesn't that make crashes much more difficult to debug? Or can you re-attach this information later on if you get a crash report? I mean sure, in theory you shouldn't get any of those in production, but no software is perfect.
- catillac 5y agoThis is really interesting. Copying to the scratch container is powerful and I’ve used it a lot, regularly, but occasionally something comes up where I need to use a more fully featured base to support things. One other downside I’ve encountered with Scratch is no shell so doing docker exec or kubectl exec doesn’t work. Does anyone know a good solution to this problem?
- iampims 5y agoDistroless works well. Still no shell, but comes with “what’s usually missing in scratch”.
- marwatk 5y agoWe drop a statically compiled BusyBox binary on images like that as "sh". If we need more we can symlink to it in /tmp at debug time (or just call it directly). It strikes a good balance between slim and debuggable.
- ed25519FUUU 5y agoDo you do this during build time or debug time?
- neomantra 5y agoBeen using containers and fighting this problem for a long time... I never thought of this simple solution, thank you for sharing it!
- tomohawk 5y agoInstead of a container, just statically build the golang program with sqlite. Single binary deployment.
- ed25519FUUU 5y agoDefault SQLite package requires gcc so you need to use one of the alternatives.
- password4321 5y ago> C libraries are required to interact with SQLite Or: modernc.org/sqlite (plus https://github.com/zombiezen/go-sqlite https://github.com/zombiezen/go-sqlite), "an automatically generated translation of the original C source code of SQLite into Go" as mentioned yesterday: https://news.ycombinator.com/item?id=29959193#29960726 https://news.ycombinator.com/item?id=29959193#29960726
- deleted 5y ago[deleted]
- mariusor 5y agoThis article is just an example of someone that encountered a problem and didn't do their due diligence of finding the best solution for their use case. The discussion of building fully static go binaries for docker has been rehashed a million times on the internet, the modernc.org/sqlite is at version 3+, so nothing in this should be new information for anyone that has an interest in the topic. I strongly dislike this type of content that just repacks basic existing information and uses it as an excuse to click bait people on to a spammy blog.
- stblack 5y agoSuccinct article, with a most excellent TL;DR.
- ttwwmm 5y agoIf you are using Go (which solves most of your dependency problems) and SQLite (which means you don't need to integrate with an external database via service discovery) why do you need Docker at all?
- deleted 5y ago[deleted]
- alexjurkiewicz 5y agoPerhaps because Docker has great stories for deployment. Many of the complexities of deployment are handled for you (writing a systemd unit, managing rollback, etc).
- encryptluks2 5y agoDocker isn't a cache-all for containers. Docker has a pretty terrible deployment story at this point and I forsee in a couple years it will probably be considered legacy compared to Podman/K3d/Local K8s/etc
- jrockway 5y agoI think people use the word Docker to mean "produce an OCI-compliant image that a CRI runtime can run".
- encryptluks2 5y agoPodman does the same thing, as well as other tools. Using Docker to refer to something that doesn't use Docker is like saying that your Android Phone is an iPhone.
- fivea 5y ago> Docker has a pretty terrible deployment story (...) This is the very first time I ever heard such nonsense. In all companies I've been, Docker is a renowned problem solver, not only for production deplyments but also for local testing environments and deployments. It even shines as a stand-alone barebones clustering solution with Docker swarm mode.
- jakearmitage 5y agoI don't see why Docker is needed here.
- sofixa 5y agoEase of deployment?
- daptaq 5y agoSeems more difficult. Without Docker I don't have to bother with a new technology, use new commands and learn new syntax.
- Cthulhu_ 5y agoNo, but you do have to deal with... however you deploy your app right now. If that works for you, then there's no need to change. But if you ever end up in an environment where multiple different applications are deployed, or if your app needs to be deployed a thousand times, that's where e.g. Docker may come in handy.
- DeWilde 5y agoThat is true, learning Docker to just do this would be wasteful. However, many people already know how to use Docker and have flows around it so it makes sense for them to do this.
- syscomet 5y agoThere are `-alpine` variants of the golang docker images, so if you're expecting to deploy to scratch or to alpine, the _builder_ step should be using `golang:1.17-alpine` instead.
- daptaq 5y agoI'm currently being forced to use Docker against my will, with exactly this setup. The CGO overhead every time the container is built is absurd, making developemnt and testing painful. I habe to try this article out, but it doesn't seem like it solves the problem. Does anyone have advice how to solve this without too much Docker magic?
- ramoz 5y agothis makes sense for the scenario- a mock server. Our backend teams rarely do anything with containers, but all apps deploy to K8s (platform team manages). We deploy SQLite… don’t remember any Cgo requirements, however we’re also deploying each SQLite “instance”, or db-files, to a node directly, and for low data gravity on SSDs. Go interacts with database/sql.
- yevpats 5y agoIf using a docker already why not just use postgresql and run go binary without the CGO overhead?
- mariusor 5y agoI'm sure you know this already, but sqlite and postgresql are not equivalent, neither in what they offer a user nor in what is required to set them up.
- lbayes 5y agoIt's hard to believe anyone is still promoting Docker. Has anyone actually used it? If you haven't jumped on the bandwagon yet, don't do it. It's a sucker's game. I got sucked into the Docker hype some years ago and the demos worked well enough in isolation. I pushed it on a small team that didn't need it. One of the biggest regrets of my career. Docker may be useful if an application has enormous reach and needs Google scale, but very few apps do. 1) It is incredibly slow to build 2) It is slow to run 3) It is an unbelievable resource hog 4) It has an entirely new set of problems (file system, networking, etc.) that must be understood to get it working 5) Hard deploy time dependency on Docker's web app?! 6) If you do not believe me on the complexity, just observe how docker arguments always seem to turn into k8s (or other orchestration) arguments. It is hard to reason about, hard to deploy, hard to coordinate. K8s is a total disaster for a small team working on a small project. We lost many months by choosing these tools for a startup that didn't (yet) need them, and the ongoing, neverending pile of problems and distractions they generated was shocking. I still feel bad about it. The company could have run on a couple $400 machines under a desk for years, but instead spent tens of thousands on AWS services it didn't need. Of course, YMMV
- proxysna 5y ago> Docker may be useful if an application has enormous reach and needs Google scale, but very few apps do. > I got sucked into the Docker hype some years ago and the demos worked well enough in isolation. > I pushed it on a small team that didn't need it. One of the biggest regrets of my career. It sounds like (no offence) issues that containerization solved were missed and you had no dedicated operations/build-engineer guy on your team that could've explained/fixed these things for you and steer your infra in a right direction. > I still feel bad about it. The company could have run on a couple $400 machines under a desk for years, but instead spent tens of thousands on AWS services it didn't need. Does not sound like a docker issue. If you actually plan to run a money making business that your livelihood depends on, then running it under your desk is not something that you can reliably do even 10-15 years ago. AWS is a money hog with a lot of things that are a distraction for a small project and it is easy to assume that their products are necessary to run your app effectively, but most often it is not. There is plenty of cloud providers that are not as expensive, and are much easier to manage compared to AWS. > K8s is a total disaster for a small team working on a small project. This is truly a YMMV thing. in my experience managed k8s for a small team can be a godsend for infra management after some level of complexity is reached, while self-hosted bare-metal k8s can utterly decimate the team. But again YMMV. I am currently running several clusters of Nomad for different types of workloads each. CI, HPC, VM's, public facing services you name it. Containerization makes it all possible to run (mostly) single-handedly.
- atmosx 5y agoTesting locally, adding the "-linkmode external -extldflags '-static'" flags doesn't bring down the binary size. The heavy lifting is done by '-s -w'. It is a good idea to drop privileges even if there's a small attack surface. At $work we use the following template. Sharing in case you find it interesting: FROM golang:1.17.1-alpine3.14 as builder RUN apk update && apk add ca-certificates curl git make tzdata RUN adduser -u 5003 --gecos '' --disabled-password --no-create-home mycompany COPY . /app WORKDIR /app RUN make build FROM scratch COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ COPY --from=builder /app/bin/app-3 /bin/app-3 COPY --from=builder /etc/passwd /etc/passwd USER mycompany CMD ["app-3"] And a very simple makefile: help: @echo "Please use 'make <target>' where <target> is one of the following:" @echo " test to run unit tests." @echo " build to build the app as a binary." @echo " build-image to build the app container." @echo " run to run the app with go." run: go run -ldflags="$(govvv -flags -pkg) -w -s" ./cmd/app-3/main.go test: go test -v -coverpkg=./... -coverprofile=profile.cov ./... go tool cover -func profile.cov rm profile.cov build: CGO_ENABLED=0 go build -mod=readonly -a -ldflags="$(govvv -flags -pkg $(go list ./info)) -w -s -linkmode external -extldflags '-static'" -o ./bin/app-3 ./cmd/app-3/* build-image: docker build -t mycompanytown/app-3:latest .
- nerdponx 5y agoWhat do you use the SSL certs in the container for? I always have run Docker behind either a reverse proxy like Nginx or in some kind of cloud platform that handles SSL at the perimeter.
- atmosx 5y agoCerts are used by the client to perform validation when initiating HTTP calls to APIs.