Skip to content

Instantly share code, notes, and snippets.

@lesomnus
Last active July 30, 2019 16:48
Show Gist options
  • Select an option

  • Save lesomnus/1b0a79f110095cead1213f001e039395 to your computer and use it in GitHub Desktop.

Select an option

Save lesomnus/1b0a79f110095cead1213f001e039395 to your computer and use it in GitHub Desktop.
Build Docker go app image using local "GOPATH/src" outside of the build context without "go get".
#!/usr/bin/env bash
# <- means just comment.
### <- means that you might need to modify following line.
#
# YOU MUST RUN THIS SCRIPT IN THE SAME FOLDER AS DOCKERFILE
# AND I ASSUMES MAIN PACKAGE ALSO IN THE SAME FOLDER.
#
# expected tree
# /go/src/your/package
# ├ internal/package
# │ └ foo.go
# ├ bar.go
# ├ main.go
# ├ Dockerfile
# └ build.sh
#
# it will catch most of dependencies as far as i know... perhaps...
# if you see the errors such as -> cannot find package "..." in any of: ...
# add word to catch after vertical bar('|').
graper='github\.\|google\.\|golang\.\|gopkg\.'
### specify your dokcer image name with tag
your_image=image:latest
### specify your package path in the GOPATH
your_pkg='your/package'
### some dependencies of the internal package are not uses in the surface package.
### specify your internal package path in the GOPATH.
### if you dont, delete below line and line:44 and see Dockerfile line:12.
your_internal_pkg='your/package/internal/package'
# sometimes GOPATH is not declared in environment variables.
# i dont know why. please give me a feedback.
go_path=$(go env | grep -m1 GOPATH= | cut -d "\"" -f 2)
### these variables are your package path
your_pkg_path=$go_path/src/$your_pkg
your_internal_pkg_path=$go_path/src/$your_internal_pkg
# find dependencies using "go list" command.
deps=$(go list -f '{{ join .Deps "\n" }}' $your_pkg_path | grep $graper)
### remove following line if you dont have internal pkage
### if not, add more lines
deps+=" "$(go list -f '{{ join .Deps "\n" }}' $your_internal_pkg_path | grep $graper)
# replace ' ' to ','
deps=$(echo $deps | sed 's/ /,/g')
# it iterates dependencies of your packages.
# i want to use single rsync command but i dont know how to resolve it. please give me a feedback.
IFS=',' read -r -a deps <<< "$deps"
for element in "${deps[@]}"
do
mkdir -p ./temp/$element/
rsync -a $go_path/src/$element/ ./temp/$element/
echo $element" done"
done
# perhaps the dependencies of your package will be out of Docker build context.
# this is the reason why you come to here.
# it moves your package into Docker build context.
mkdir -p ./temp/$your_pkg
rsync -a $loco_metro_cli_path/ ./temp/$your_pkg
docker build \
-t $your_image .
# delete temp folder.
rm -rf ./temp/
# delete dangling image (see Dokcerfile).
docker rmi $(docker images -f dangling=true -q)
# please point out any inconvenient grammar or awkward sentence at any time.
FROM golang:latest
# <- means just comment.
### <- means that you might need to modify following line.
### your package path
RUN mkdir -p /go/src/your/package \
### if you dont have internal packages, delete this line.
### if you have more internal packages, however, add more lines.
&& mkdir -p /go/src/your/package/internal/package
WORKDIR /go/src/your/package
# COPY . .
# will copies all files including build.sh and Dockerfile etc.
# if you dont care this situation, delete line:22,26 and uncomment line:17
# copy dependencies
COPY ./temp/. /go/src/.
### if you dont need to copy main.go, delete this line.
### if you need to copy more .go files, however, add more lines.
COPY ./main.go ./main.go
###
### if you dont have main package, delete all below lines
###
# create a static binary in go and put it in a from scratch docker container.
# thank you PurpleBooth!
# https://gist.github.com/PurpleBooth/ec81bad0a7b56ac767e0da09840f835a
RUN go build -ldflags "-linkmode external -extldflags -static" -a main.go
FROM scratch
### your main package path
COPY --from=0 /go/src/your/package/main /main
ENTRYPOINT [ "/metro" ]
# please point out any inconvenient grammar or awkward sentence at any time.
@napoliano

Copy link
Copy Markdown

unbelievable code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment