Dockerfile for Alpine+Golang

大眼铅笔 · · 694 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

Dockerfile for Alpine+Golang

在开发环境,测试环境下使用Golang的Docker镜像。
目前使用两个Dockerfile,一个基于Alpine,一个基于CentOS。

使用时请注意修改掉私人有关的内容。

Dockerfile for Alpine+Golang

FROM alpine:latest

RUN apk add --no-cache \
        ca-certificates

# set up nsswitch.conf for Go's "netgo" implementation
# - https://github.com/golang/go/blob/go1.9.1/src/net/conf.go#L194-L275
# - docker run --rm debian:stretch grep '^hosts:' /etc/nsswitch.conf
RUN [ ! -e /etc/nsswitch.conf ] && echo 'hosts: files dns' > /etc/nsswitch.conf

ENV GOLANG_VERSION 1.12.3

RUN set -eux; \
    apk add --no-cache --virtual .build-deps bash gcc musl-dev openssl gcc musl-dev go git\
    ; \
    export \
# set GOROOT_BOOTSTRAP such that we can actually build Go
        GOROOT_BOOTSTRAP="$(go env GOROOT)" \
# ... and set "cross-building" related vars to the installed system's values so that we create a build targeting the proper arch
# (for example, if our build host is GOARCH=amd64, but our build env/image is GOARCH=386, our build needs GOARCH=386)
        GOOS="$(go env GOOS)" \
        GOARCH="$(go env GOARCH)" \
        GOHOSTOS="$(go env GOHOSTOS)" \
        GOHOSTARCH="$(go env GOHOSTARCH)" \
    ; \
# also explicitly set GO386 and GOARM if appropriate
# https://github.com/docker-library/golang/issues/184
    apkArch="$(apk --print-arch)"; \
    case "$apkArch" in \
        armhf) export GOARM='6' ;; \
        x86) export GO386='387' ;; \
    esac; \
    \
    wget -O go.tgz "https://golang.org/dl/go$GOLANG_VERSION.src.tar.gz"; \
    # echo 'bc1ef02bb1668835db1390a2e478dcbccb5dd16911691af9d75184bbe5aa943e *go.tgz' | sha256sum -c -; \
    tar -C /usr/local -xzf go.tgz; \
    rm go.tgz; \
    \
    cd /usr/local/go/src; \
    ./make.bash; \
    \
    rm -rf \
# https://github.com/golang/go/blob/0b30cf534a03618162d3015c8705dd2231e34703/src/cmd/dist/buildtool.go#L121-L125
        /usr/local/go/pkg/bootstrap \
# https://golang.org/cl/82095
# https://github.com/golang/build/blob/e3fe1605c30f6a3fd136b561569933312ede8782/cmd/release/releaselet.go#L56
        /usr/local/go/pkg/obj \
    ; \
    export PATH="/usr/local/go/bin:/usr/bin:$PATH"; \
    go version; \
    git version
# apk del .build-deps
ENV GO15VENDOREXPERIMENT 1
ENV GO111MODULE on
ENV GOROOT /usr/local/go
ENV GOPATH /data/boyosoft/goBillProcess
# ENV DPPATH $GOPATH/src/vendor/github.com/cpfc/dp
ENV PATH $GOPATH/bin:$GOROOT/bin:$PATH

# ENV GOPATH /go
# ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH

# RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && \
#   chmod -R 777 "$GOPATH"  &&  \
RUN mkdir -p /root/.ssh
COPY authorized_keys /root/.ssh/authorized_keys
COPY id_rsa_github /root/.ssh/id_rsa_github
# COPY id_rsa_bigdata_prod /root/.ssh/id_rsa_bigdata_prod
COPY config /root/.ssh/config
COPY known_hosts /root/.ssh/known_hosts
RUN cd /root/.ssh; \
    chmod 400  *; \
    cd ..;  \
    chmod 700 .ssh; \
    git config --global user.name "devuser";    \
    git config --global user.email "pythoner@icloud.com"
#
WORKDIR $GOPATH/src
VOLUME ["/data/boyosoft"]
EXPOSE 8080 8081 3000 3001 9000 9001 9090

Dockerfile for CentOS+Golang

# Version: 0.0.1
FROM centos:latest
MAINTAINER xiehuanang "xiehuanang@boyosoft.net"
# RUN yum install -y wget
#================================================================
# 创建构建或初始化容器时的若干中文件
RUN mkdir /dockerdata
WORKDIR /dockerdata
ADD go1.12.3.linux-amd64.tar.gz /dockerdata
# 升级到centos-7
# COPY CentOS6-Base-163.repo /dockerdata/CentOS-Base.repo
# COPY geekery.repo /dockerdata/geekery.repo
# RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup &&  \
#       cp /dockerdata/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo   &&  \
#       cp /dockerdata/geekery.repo /etc/yum.repos.d/geekery.repo
RUN yum clean all &&    \
        yum makecache   &&  \
        yum -y update   &&  \
        yum install -y gcc git
# RUN yum install -y curl   &&  \

#   yum install -y readline-devel   &&  \
#   yum install -y libXt-devel  &&  \
#   yum install -y make
# screen ftp
# RUN yum install -y gcc-gfortran

# gcc-c++
# RUN yum install -y readline-devel
# RUN yum install -y libXt-devel
# RUN yum install -y make
# git

#================================================================

# RUN yum install -y vixie-cron
# RUN yum install -y crontabs
# RUN chkconfig --level 35 crond on
# RUN yum install -y bzip2recover
# RUN yum install -y bzip2

# install R packages
# RUN yum install -y epel-release
# RUN yum install -y R
#================================================================
# RUN yum install -y mysql-community-server
# RUN yum install tomcat6 tomcat6-webapps tomcat6-admin-webapps
#================================================================
# RUN yum install -y redis-server
# RUN yum install -y redis-tools
#================================================================RUN eval "TZ='Asia/Shanghai'; export TZ"
RUN echo "export TZ=\"Asia/Shanghai\"" >> /root/.bashrc &&  \
    echo "export TS=\"`date -d today +"%-Y%-m%-d%k%M%S"`\"" >> /root/.bashrc    &&  \
    echo "export VERSION=\"`date -d today +"v2.4.0.%-Y%-m%-d%-k%M%S"`\"" >> /root/.bashrc   &&  \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#@todo: 争取把跨平台编译的脚本保存在Docker容器中
# RUN echo `for GOOS in windows;do for GOARCH in 386 amd64; do go build -v -ldflags "-s -w -X main._VERSION_="$VERSION  -o BillProcess-$GOOS-$GOARCH-v$TS.exe; done; done`

# RUN rm /etc/localtime3

#================================================================
# 此处纯粹是为配合腾讯的习惯,腾讯虚拟服务器的工作卷名字叫data
# RUN export http_proxy="10.251.233.145:8888"
# RUN export https_prxy="10.251.233.145:8888"
ENV LANG en_US.UTF-8
ENV LC_ALL en_US.UTF-8

#================================================================

#================================================================

RUN echo "47.92.84.245\tboyosoft" >> /dockerdata/ex_hosts &&  \
        echo "47.92.84.245\tboyosoft.net" >> /dockerdata/ex_hosts &&  \
        echo "47.92.84.245\twww.boyosoft.net" >> /dockerdata/ex_hosts &&  \
        touch /dockerdata/update_hosts  &&  \
        echo "cat /dockerdata/ex_hosts >> /etc/hosts" >> /dockerdata/update_hosts &&  \
        echo "/etc/init.d/netfs restart" >> /dockerdata/update_hosts    &&  \
        chmod 700 /dockerdata/update_hosts  &&  \
        rm -rf /dockerdata/ex_hosts &&  \
        rm -rf /usr/lib/golang  &&  \
    cp -rf /dockerdata/go /usr/lib/golang   &&  \
        rm -rf /dockerdata/go  &&  \
    mkdir -p /root/.ssh &&  \
    chmod 700 /root/.ssh

ENV GO15VENDOREXPERIMENT 1
ENV GO111MODULE on
ENV GOROOT /usr/lib/golang
ENV GOPATH /data/boyosoft/goBillProcess
# ENV DPPATH $GOPATH/src/vendor/github.com/cpfc/dp
ENV PATH $GOPATH/bin:$GOROOT/bin:$PATH

#================================================================
# ADD getlatest.sh /dockerdata/getlatest.sh

RUN mkdir -p /root/.ssh
COPY authorized_keys /root/.ssh/authorized_keys
COPY id_rsa_github /root/.ssh/id_rsa_github
# COPY id_rsa_bigdata_prod /root/.ssh/id_rsa_bigdata_prod
COPY config /root/.ssh/config
COPY known_hosts /root/.ssh/known_hosts
RUN cd /root/.ssh   &&  \
    chmod 400 *    &&  \
    cd ..   &&  \
    chmod 700 .ssh

# RUN git clone git@git.18.tl:xiehuanang/processtest.git gittest
# RUN git clone http://xiehuanang:Pocketbbs@www.taolch.com:10080/git/goBillProcess.git

# RUN chmod 700 /dockerdata/getlatest.sh
#RUN echo "#!/bin/sh" >> /dockerdata/build-$APPNAME.sh; \
#    echo "cd $APPAPIPATH" >> /dockerdata/build-$APPNAME.sh; \
#    echo "go build -o $APPNAME main.go" >> /dockerdata/build-$APPNAME.sh; \
#   echo "scp $APPNAME 192.168.200.244:~/$APPNAME" >> /dockerdata/build-$APPNAME.sh; \
#    chmod 700 /dockerdata/build-$APPNAME.sh

# RUN echo "#!/bin/sh" >> /dockerdata/build-$APPNAME.sh; \
#       echo "cd $APPAPIPATH" >> /dockerdata/build-$APPNAME.sh; \
#       echo "go build -o $APPNAME $APPNAME.go" >> /dockerdata/build-$APPNAME.sh; \
#       echo "scp $APPNAME 10.104.19.122:~/data/boyosoft/$APPNAME" >> /dockerdata/build-$APPNAME.sh; \
#       chmod 700 /dockerdata/build-$APPNAME.sh

# RUN git config --global user.email "xiehuanang@boyosoft.net"
# RUN git config --global user.name "xiehuanang"
# RUN git config --global push.default matching
RUN git config --global user.name "devuser"
RUN git config --global user.email "pythoner@icloud.com"

###############################################################################
# 初始化root用户的环境变量
RUN echo "export GOPATH=$GOPATH" >> /root/.bashrc   &&  \
    echo "export GOROOT=$GOROOT" >> /root/.bashrc   &&  \
    echo "export GO15VENDOREXPERIMENT=$GO15VENDOREXPERIMENT" >> /root/.bashrc   &&  \
    echo "export GO111MODULE=$GO111MODULE" >> /root/.bashrc &&  \
    echo "export JDALPHASERVERPATH=$GOPATH/src/github.com/boyosoft/jdalphaserver" >> /root/.bashrc  &&  \
    echo "export PATH=$PATH" >> /root/.bashrc   &&  \
    cat /root/.bashrc   &&  \
    rm -rf /var/cache/yum
# RUN source /root/.profile
# RUN touch /usr/local/bin/q &&  \
#     chmod +x /usr/local/bin/q  &&  \
#     echo "#!/bin/bash" >> /usr/local/bin/q    &&  \
#     echo "cd $JDALPHASERVERPATH" >> /usr/local/bin/q  &&  \
#     echo "rm -rf jdalphaserver" >> /usr/local/bin/q   &&  \
#     echo "go build" >> /usr/local/bin/q    &&  \
#     echo "chmod +x jdalphaserver" >> /usr/local/bin/q && \
#     # echo "./jdalphaserver -syncdb" >> /usr/local/bin/q    &&  \
#     echo "./jdalphaserver" >> /usr/local/bin/q
###############################################################################

#RUN mkdir -p /dockerdata/www/html/static
# RUN git commit -a -m "update when compile the dockerfile"
# RUN git push
# goBillProcess内嵌的Web服务器占用的端口号
WORKDIR $GOPATH/src
VOLUME ["/data/boyosoft"]
EXPOSE 8080 8081 3000 3001 9000 9001 9090

# export VERSION=`date -d today +"v2.4.0.%-Y%-m%-d%-k%M%S"`
# export TS=`date -d today +"%-Y%-m%-d%k%M%S"`
# for GOOS in windows;do for GOARCH in 386 amd64; do go build -v -ldflags "-s -w -X main._VERSION_="$VERSION  -o BillProcess-$GOOS-$GOARCH-v$TS.exe; done; done

# ENTRYPOINT ["/root/config_crond.sh"]
# CMD ["/dockerdata/build-$APPNAME.sh"]

有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:大眼铅笔

查看原文:Dockerfile for Alpine+Golang

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

694 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传