https://github.com/nathan-osman/bettergo
Docker container for building Go applications without running as root
https://github.com/nathan-osman/bettergo
docker golang
Last synced: 3 months ago
JSON representation
Docker container for building Go applications without running as root
- Host: GitHub
- URL: https://github.com/nathan-osman/bettergo
- Owner: nathan-osman
- Created: 2017-07-17T23:52:03.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-09-08T06:56:15.000Z (almost 9 years ago)
- Last Synced: 2025-05-25T01:43:51.682Z (about 1 year ago)
- Topics: docker, golang
- Language: Shell
- Homepage:
- Size: 3.91 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## bettergo
The [`golang` repository](https://hub.docker.com/_/golang/) on Docker Hub is useful for building Go applications in a container without requiring the host to have the Go toolchain installed. Typical usage would look something like this:
mkdir dist
docker run \
--rm \
-v `pwd`/dist:/go/bin \
golang \
go get github.com/hectane/hectane
If successful, the `dist/` directory will contain an executable named `hectane` when the command completes.
One small problem: _`dist/hectane` is owned by root_.
You can still remove the directory with `rm` but even that will fail if there are subdirectories.
That's where bettergo comes in. Instead of running commands in the container as root, bettergo takes a completely different approach: create a user in the container that matches the current user on the host. What does this look like?
mkdir dist
docker run \
--rm \
-v `pwd`/dist:/go/bin \
-e UID=`id -u` \
nathanosman/bettergo \
go get github.com/hectane/hectane
The only differences are the repository name and the addition of the environment variable `UID`, which is initialized with the current user's ID. The container will then create the appropriate entries in `/etc/passwd` and `/etc/shadow` for you and then run the `go get` command as the new user.
Once the command completes, everything inside `dist` is owned by... you! That's right, you are the owner of the files inside the directory once again.