A set of scripts/tools packaged in a Docker image to quickly get up-and-writing with Golang.
7.0K
View the canonical documentation at https://github.com/zephinzer/golang-dev
A set of scripts/tools packaged in a Docker image to quickly get up-and-writing with Golang.
go buildgo mod used here)go testgo buildscratch imagego (mod|build|run|test) on the host systemYou don't need Go installed or a
GOPATHfor this 🥂
The quickest way to try this out is to copy the following into a Makefile in your target directory and run make init:
GOLANG_DEV_VERSION=latest
# initialises this directory - use once only
init:
@$(MAKE) _dev ARG="init"
# builds the application - outputs an `app` binary
build:
@$(MAKE) _dev ARG="build"
# runs tests in watch-mode
test: build
@$(MAKE) _dev ARG="test"
# runs tests once
test.once: build
@$(MAKE) _dev ARG="test -coverprofile c.out"
# runs the application on the host network
start: build
@$(MAKE) _dev ARG="start"
# creates a shell in a fresh container generated from the image, usable for development on non-linux machines
shell:
$(MAKE) dev ARG="shell"
# retrieves the latest version we are at
version.get:
@docker run -v "$(CURDIR):/app" zephinzer/vtscripts:latest get-latest -q
# bumps the version by 1: specify VERSION as "patch", "minor", or "major", to be specific about things
version.bump:
@docker run -v "$(CURDIR):/app" zephinzer/vtscripts:latest iterate ${VERSION} -i
# base command to run other scripts (do not use alone)
_dev:
@docker run \
-it \
--network host \
-u $$(id -u) \
-v "$(CURDIR)/.cache/pkg:/go/pkg" \
-v "$(CURDIR):/go/src/app" \
zephinzer/golang-dev:$(GOLANG_DEV_VERSION) ${ARG}
Check out the documentation below on how to bundle your application into a scratch Docker image.
golang-dev ImageIf you already have a Makefile/don't want a Makefile (but why?), running the following in any directory should get you up and running:
docker run -it \
-u ${UID} \
-v "$(pwd)/.cache/pkg:/go/pkg" \
-v "$(pwd):/go/src/app" \
zephinzer/golang-dev:latest init;
The script should ask a series of questions which if you agree to all, will create a Git repository, add an appropriate .gitignore file, initialise go mod, and provision a Dockerfile you can use to build your own image.
Notes:
-u parameter sets the guest user to use your host user ID so that new files aren't created as root, causing problems on your host machine when you attempt to delete them..cache directory allows for faster subsequent dependency processing by go mod.Assuming you've completed the above step, create a main.go if there isn't already one, and run the following to start development:
docker run -it \
-u ${UID} \
--network host \
-v "$(pwd)/.cache/pkg:/go/pkg" \
-v "$(pwd):/go/src/app" \
zephinzer/golang-dev:latest start;
To start the tests, create a new terminal in the smae directory and run:
docker run -it \
-u ${UID} \
--network host \
-v "$(pwd)/.cache/pkg:/go/pkg" \
-v "$(pwd):/go/src/app" \
zephinzer/golang-dev:latest start;
Notes
*.go will result in a live-reloadimport will cause the script to start downloading the new dependencyimport will simply cause the dependency to be removed - simple stuff--network host, specify the flag -p "HOSTPORT":"CONTAINERPORT" in the docker run commandAssuming you have a main.go, building a binary is as simple as running:
docker run -it \
-u ${UID} \
-v "$(pwd)/.cache/pkg:/go/pkg" \
-v "$(pwd):/go/src/app" \
zephinzer/golang-dev:latest build;
The binary will appear in your current directory and be named app.$GOOS.$GOARCH. It will be postfixd with a .exe if $GOOS specifies a Windows build.
Notes:
CGO_ENABLED=0, GOOS=linux, and GOARCH=amd64. To change this, specify your environment variables as part of the docker command. Documentation can be found here under the --env or --env-file flag.If you denied the init script from creating a Dockerfile, you can run it again to get it. Otherwise, the Dockerfile script should be:
FROM zephinzer/golang-dev:latest as development
COPY . /go/src/app
ENTRYPOINT [ "start" ]
FROM development as build
RUN build
ENTRYPOINT [ "/go/src/app/app" ]
FROM scratch as production
COPY --from=build /go/src/app/app /
ENTRYPOINT [ "/app" ]
Copy the above and paste it in a file named Dockerfile in your root directory. After that, you can build your image by running:
docker build -t yourname/yourimage:yourtag .;
docker-compose.ymlAdd the following in your Docker Compose to use this as part of a Docker provisioned environment:
version: "3.5" # or anything you're using
services:
# ...
app:
image: zephinzer/golang-dev:latest
ports: # if needed
- "3000:3000"
user: "${UID}"
entrypoint: ["start"]
volumes:
- "./.cache/pkg:/go/pkg"
- "./:/go/src/app"
# ...
# ...
This project is licensed under the MIT license. See the LICENSE file for the full text.
golang-dev Came About$GOPATH is a pain-in-the-filesystem. Developing via a Docker environment abstracts this away. Also, you no longer have to have Go installed on your system since managing different Go versions is another pain-in-the-filesystem.go mod which accomodates previous package managers such as glide and go dep.I use this project for information-systems type of work. This may not accomodate your use cases perfectly if you're developing scripts for say big data processing or artificial intelligence works, but it may. Let me know if you have any suggestions via an issue!
If you're interested in working on this, read on!
The contribution mechanism is roughly the same as other open-source projects:
masterThe main logic of how this works is written as shell scripts in the /scripts directory.
The Dockerfile simply copies the /scripts in and adds it to the $PATH.
Tests are contained in the ./test directory but you can run it from the root using make test.
To build the Docker image, run make build.
To bump a patch version, run make version.bump.
To bump a minor versoin, run make version.bump VERSION=minor.
To bump a major versoin, run make version.bump VERSION=major.
To publish the Docker image, run make publish.
This publishes two images - one with the version as recorded by the Git tags, another with the version of Golang, and a last one with the latest tag.
Leave me a 🌟 or watch this repository to indicate your interest in my sustained development on this. It'll help me decide whether or not I should deprecate this once my own use case for this is over.
Content type
Image
Digest
Size
177.3 MB
Last updated
over 7 years ago
docker pull zephinzer/golang-dev