Skip to content

Instantly share code, notes, and snippets.

@uudashr
Last active July 15, 2022 00:52
Show Gist options
  • Save uudashr/fc93dec7e0acd7611810d45927975b6c to your computer and use it in GitHub Desktop.
Save uudashr/fc93dec7e0acd7611810d45927975b6c to your computer and use it in GitHub Desktop.
Docker and entrypoint
# == Builder ==
FROM golang:1.13.7-alpine3.11 as builder
RUN apk add --no-cache \
bash=5.0.11-r1 \
git=2.24.1-r0 \
mercurial=5.2.1-r0 \
openssh-client=8.1_p1-r0
ARG SSH_PRIVATE_KEY
RUN mkdir /root/.ssh/
RUN echo "${SSH_PRIVATE_KEY}" > /root/.ssh/id_rsa
RUN chmod 600 /root/.ssh/id_rsa
RUN git config --global url."git@github.com:mycompany".insteadOf \
"https://github.com/mycompany"
RUN touch /root/.ssh/known_hosts
RUN ssh-keyscan github.com >> /root/.ssh/known_hosts
WORKDIR /app
# Copy dependencies definition
COPY go.mod .
COPY go.sum .
# Download dependencies
RUN GOPRIVATE=github.com/mycompany go mod download
# Copy remaining source code
COPY . .
# Build binary
RUN CGO_ENABLED=0 go build -a -ldflags "-X main.revision=$(git rev-list -1 HEAD)" -installsuffix cgo -o /app/api-up ./*.go
# == Runner ==
FROM alpine:3.11.3
RUN apk add --no-cache \
tini=0.18.0-r0
# Copy binary from builder
COPY --from=builder /app/api-up /app/api-up
COPY docker-entrypoint.sh /bin/
ENTRYPOINT ["tini", "--", "docker-entrypoint.sh"]
CMD ["/app/api-up"]
#!/bin/sh
if [ "$1" = '/app/api-up' ]; then
if [ -n "$PORT" ]; then
set -- -port="$PORT" "$@"
else
set -- -port="8080"
fi
if [ -n "$MAX_CONN" ]; then
set -- "$@" -max-conn="$MAX_CONN"
fi
exec /app/api-up "$@"
fi
exec "$@"
SSH_PRIVATE_KEY_FILE ?= ~/.ssh/id_rsa
SSH_PRIVATE_KEY ?= `cat $(SSH_PRIVATE_KEY_FILE)`
# Build docker image
.PHONY: docker-image
docker-image:
@echo Build docker image
@docker build -t api --build-arg SSH_PRIVATE_KEY="$(SSH_PRIVATE_KEY)" .
#!/bin/sh
docker run --rm -it -v $(pwd)/logs:/app/logs \
-e MAX_CONN=10 \
-e PORT=9090 \
logsim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment