Skip to content

Instantly share code, notes, and snippets.

@inhere
Created July 2, 2018 07:31
Show Gist options
  • Save inhere/d66c1e6575bbf873375070221e60a019 to your computer and use it in GitHub Desktop.
Save inhere/d66c1e6575bbf873375070221e60a019 to your computer and use it in GitHub Desktop.
dockerfile for build golang application
#
# @build-example build . -f Dockerfile -t myapp:test
#
################################################################################
### builder image
################################################################################
FROM golang:1.10-alpine as Builder
# Recompile the standard library without CGO
#RUN CGO_ENABLED=0 go install -a std
ENV APP_DIR $GOPATH/src/{{!your_app_path!}}
RUN mkdir -p $APP_DIR
ADD . $APP_DIR
# Compile the binary and statically link
# -ldflags '-w -s'
# -s: 去掉符号表
# -w: 去掉调试信息,不能gdb调试了
# RUN cd $APP_DIR && CGO_ENABLED=0 go build -ldflags '-d -w -s' -o /tmp/app-server
RUN go version && cd $APP_DIR && go build -ldflags '-w -s' -o /tmp/app-server
# RUN cd $APP_DIR && go build -o /tmp/app-server
################################################################################
### target image
################################################################################
FROM alpine:3.7
LABEL maintainer="inhere <[email protected]>" version="1.0"
##
# ---------- env settings ----------
##
ARG timezone
# prod audit test dev. --build-arg app_env=dev
ARG app_env=dev
ARG app_port
ENV APP_ENV=${app_env:-"dev"} \
APP_PORT=${app_port:-19430} \
TIMEZONE=${timezone:-"Asia/Shanghai"}
##
# ---------- some config, clear work ----------
##
RUN set -ex \
&& sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/' /etc/apk/repositories \
# install some tools
&& apk update && apk add --no-cache tzdata ca-certificates \
# clear caches
&& rm -rf /var/cache/apk/* \
# - config timezone
&& ln -sf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
&& echo "${TIMEZONE}" > /etc/timezone \
&& date -R \
# - create logs, caches dir
&& mkdir -p /data/logs /var/www \
# && chown -R www:www /data/logs \
&& echo -e "\033[42;97m Build Completed :).\033[0m\n"
EXPOSE ${APP_PORT}
WORKDIR "/var/www"
COPY --from=Builder /tmp/app-server app-server
COPY conf conf
# COPY static static
# COPY resources resources
CMD ./app-server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment