|
| 1 | +# |
| 2 | +# @build-example docker build . -f Dockerfile -t inherelab/httprr |
| 3 | +# |
| 4 | + |
| 5 | +################################################################################ |
| 6 | +### builder image |
| 7 | +################################################################################ |
| 8 | +FROM golang:1.14-alpine as Builder |
| 9 | + |
| 10 | +# Recompile the standard library without CGO |
| 11 | +#RUN CGO_ENABLED=0 go install -a std |
| 12 | + |
| 13 | +ENV BUILd_DIR=/go/project \ |
| 14 | + GO111MODULE=on \ |
| 15 | + GOPROXY=https://goproxy.io |
| 16 | + |
| 17 | +RUN mkdir -p $BUILd_DIR |
| 18 | + |
| 19 | +COPY . $BUILd_DIR |
| 20 | + |
| 21 | +# Compile the binary and statically link |
| 22 | +# -ldflags '-w -s' |
| 23 | +# -s: 去掉符号表 |
| 24 | +# -w: 去掉调试信息,不能gdb调试了 |
| 25 | +# RUN cd $BUILd_DIR && CGO_ENABLED=0 go build -ldflags '-d -w -s' -o /tmp/app |
| 26 | +RUN go version && cd $BUILd_DIR && go build -ldflags '-w -s' -o $BUILd_DIR/app |
| 27 | +# RUN cd $BUILd_DIR && go build -o /tmp/app |
| 28 | + |
| 29 | +################################################################################ |
| 30 | +### target image |
| 31 | +################################################################################ |
| 32 | +FROM alpine:3.12 |
| 33 | +LABEL maintainer= "inhere <[email protected]>" version= "1.0" |
| 34 | + |
| 35 | +## |
| 36 | +# ---------- env settings ---------- |
| 37 | +## |
| 38 | +ARG timezone |
| 39 | +# env: prod pre test dev. --build-arg app_env=dev |
| 40 | +ARG app_env=dev |
| 41 | +ARG app_port |
| 42 | + |
| 43 | +ENV APP_ENV=${app_env:-"dev"} \ |
| 44 | + APP_PORT=${app_port:-8080} \ |
| 45 | + BUILd_DIR=/go/project \ |
| 46 | + TIMEZONE=${timezone:-"Asia/Shanghai"} |
| 47 | + |
| 48 | +EXPOSE ${APP_PORT} |
| 49 | +WORKDIR /data/www |
| 50 | + |
| 51 | +COPY --from=Builder $BUILd_DIR/* ./ |
| 52 | +#COPY --from=Builder $BUILd_DIR/static static |
| 53 | +#COPY --from=Builder $BUILd_DIR/resource resource |
| 54 | +#COPY --from=Builder $BUILd_DIR/app app |
| 55 | + |
| 56 | +## |
| 57 | +# ---------- some config, clear work ---------- |
| 58 | +## |
| 59 | +RUN set -ex; \ |
| 60 | + sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/' /etc/apk/repositories; \ |
| 61 | + # install some tools |
| 62 | + apk update && apk add --no-cache tzdata ca-certificates; \ |
| 63 | + # clear caches |
| 64 | + rm -rf /var/cache/apk/*; \ |
| 65 | + # - config timezone |
| 66 | + ln -sf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime; \ |
| 67 | + echo "${TIMEZONE}" > /etc/timezone; \ |
| 68 | + # - create logs, caches dir |
| 69 | + mkdir -p /data/logs /data/www; \ |
| 70 | +# chown -R worker:worker /data/www; \ |
| 71 | + chown -R www:www /data/www; \ |
| 72 | + chmod a+x /data/www/app; \ |
| 73 | + # && chown -R www:www /data/logs \ |
| 74 | + echo -e "\033[42;37m Build Completed :).\033[0m\n" |
| 75 | + |
| 76 | +CMD ./app |
0 commit comments