Skip to content

Commit

Permalink
Add dockerfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
caitlinelfring committed Aug 21, 2020
1 parent 155f650 commit 3e30282
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.DS_Store
bin/*
.git
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.DS_Store
bin/*
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM golang:1.14 as builder

ARG BUILD_TIME=0
ARG BUILD_VERSION=0

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . ./

# Make it runnable on alpine
RUN CGO_ENABLED=0 GOOS=linux go build \
-ldflags="-X 'main.BuildTime=${BUILD_TIME}' -X 'main.BuildVersion=${BUILD_VERSION}'" \
-a -installsuffix cgo -o woke .

######################

FROM alpine:latest
COPY --from=builder /app/woke /woke
COPY default.yaml /default.yaml
ENTRYPOINT [ "/woke" ]
13 changes: 13 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

FROM golang:1.14

RUN apt-get update && apt-get install -y inotify-tools

ENV ROOT_PATH /go/src/github.com/caitlinelfring/woke
WORKDIR $ROOT_PATH
COPY . $ROOT_PATH

RUN go build ./...

ENTRYPOINT ["./autoreload.sh"]
CMD ["woke"]
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

build:
docker build -t woke -f Dockerfile.dev .

run:
docker run --rm -it \
-v `pwd`:/go/src/github.com/caitlinelfring/woke \
woke bin/woke "./*.go ./**/*.go ./**/**/*.go"

.PHONY: build run
54 changes: 54 additions & 0 deletions autoreload.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/bash
# Autoreloads go application on file changes
# From https://github.com/caitlinelfring/go_autoreload

TARGET=${1:-"app"}
FILES_TO_WATCH=${2:-"./*.go"}

wait_for_changes() {
echo "Waiting for changes"
if [ "$(uname)" == "Darwin" ]; then
fswatch -e ".*" -i "\\.go$" -x -r -1 .
else
# FILES_TO_WATCH can be something like: ./*.go ./**/*.go ./**/**/*.go
# depending on how long the folder structure is.
# unfortunately, inotifywait can't recursively watch specific file types
inotifywait -e modify -e create -e delete -e move $FILES_TO_WATCH
fi
}

build() {
echo "Rebuilding..."
rm -vf $TARGET
go build -v -o $TARGET
}

kill_server() {
if [[ -n $SERVER_PID ]]; then
echo
echo "Stopping server (PID: $SERVER_PID)"
kill $SERVER_PID
fi
}

serve() {
echo "Starting server"
./$TARGET &
SERVER_PID=$!
}

# Exit on ctrl-c (without this, ctrl-c would go to inotifywait, causing it to
# reload instead of exit):
trap "exit 0" SIGINT SIGTERM
trap kill_server "EXIT"

build
serve
while wait_for_changes; do
kill_server
if ! build; then
echo "Error building server"
continue
fi
serve
done

0 comments on commit 3e30282

Please sign in to comment.