Skip to content

Commit

Permalink
Docker images for cross-building Beats
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkroh committed Jun 21, 2018
0 parents commit f047f0d
Show file tree
Hide file tree
Showing 39 changed files with 698 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# See: http://editorconfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.yml]
indent_style = space
indent_size = 2

[Dockerfile]
indent_style = space
indent_size = 2

[Makefile]
indent_style = tab
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.iml
*.swp
*.o
.idea
.vagrant
_obj
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
build:
@make -C go1.10 $@
@make -C go1.10 -f Makefile.debian7 $@
@make -C fpm $@

# Requires login at https://docker.elastic.co:7000/.
push:
@make -C go1.10 $@
@make -C go1.10 -f Makefile.debian7 $@
@make -C fpm $@

.PHONY: build push
13 changes: 13 additions & 0 deletions Makefile.common
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
REPOSITORY := docker.elastic.co/beats-dev
VCS_REF := $(shell git rev-parse HEAD)
VCS_URL := https://github.com/elastic/golang-crossbuild
BUILD_DATE := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
.DEFAULT_GOAL := build

# Requires login at https://docker.elastic.co:7000/.
push:
echo ">> Pushing $(REPOSITORY)/$(NAME):$(VERSION)$(SUFFIX)$(TAG_EXTENSION)"
@docker tag "$(REPOSITORY)/$(NAME):$(VERSION)$(SUFFIX)$(TAG_EXTENSION)" "push.$(REPOSITORY)/$(NAME):$(VERSION)$(SUFFIX)$(TAG_EXTENSION)"
@docker push "push.$(REPOSITORY)/$(NAME):$(VERSION)$(SUFFIX)$(TAG_EXTENSION)"

.PHONY: push
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# golang-crossbuild

golang-crossbuild is a set of Docker images containing the requisite
cross-compilers for cross compiling Go applications. The cross-compilers are
needed when the application uses [cgo](https://golang.org/cmd/cgo/).

The base image used is Debian 9 (stretch) unless otherwise specified.

## Docker Repo

`docker.elastic.co/beats-dev/golang-crossbuild:[TAG]`

## Build Tags

- `1.10.3-main` - linux/{amd64,386} and windows/{amd64,386}
- `1.10.3-arm` - linux/{armv5,armv6,armv7,arm64}
- `1.10.3-darwin` - darwin/{amd64,386}
- `1.10.3-ppc` - linux/{ppc64,ppc64le}
- `1.10.3-mips` - linux/{mips,mipsle,mips64,mips64le}
- `1.10.3-s390x` - linux/s390x
- `1.10.3-main-debian7` - linux/{amd64,386} and windows/{amd64,386} (Debian 7
uses glibc 2.13 so the resulting binaries (if dynamically linked) have greater
compatibility.)

## Usage Example

```sh
docker run -it --rm \
-v $GOPATH/src/github.com/user/go-project:/go/src/github.com/user/go-project \
-w /go/src/github.com/user/go-project \
-env CGO_ENABLED=1 \
docker.elastic.co/beats-dev/golang-crossbuild:1.10.3-arm \
--build-cmd "make build" \
-p "linux/armv7"
```

This will execute your projects `make build` target. While executing the build
command the following variables with be added to the environment: GOOS, GOARCH,
GOARM, PLATFORM_ID, CC, and CXX.
1 change: 1 addition & 0 deletions fpm/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Makefile
40 changes: 40 additions & 0 deletions fpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
FROM debian:stable
LABEL maintainer="Elastic Beats Team"

RUN \
apt-get update && \
apt-get install -y --no-install-recommends \
autoconf build-essential libffi-dev ruby-dev rpm zip dos2unix libgmp3-dev \
curl make gcc \
&& rm -rf /var/lib/apt/lists/*

ARG SUEXEC_VERSION=0.2
ARG SUEXEC_DOWNLOAD_URL=https://github.com/ncopa/su-exec/archive/v${SUEXEC_VERSION}.tar.gz
ARG SUEXEC_DOWNLOAD_SHA256=ec4acbd8cde6ceeb2be67eda1f46c709758af6db35cacbcde41baac349855e25

RUN \
curl -fsSL "$SUEXEC_DOWNLOAD_URL" -o suexec.tar.gz \
&& echo "$SUEXEC_DOWNLOAD_SHA256 suexec.tar.gz" | sha256sum -c - \
&& mkdir /suexec \
&& tar -C /suexec --strip-components=1 -xzf suexec.tar.gz \
&& make -C /suexec \
&& mv /suexec/su-exec /usr/bin/ \
&& rm -rf /suexec suexec.tar.gz

ARG FPM_VERSION
RUN gem install fpm -v "$FPM_VERSION"

COPY rootfs /

ENTRYPOINT ["/entrypoint.sh"]

# Build-time metadata as defined at http://label-schema.org.
ARG BUILD_DATE
ARG IMAGE
ARG VCS_REF
ARG VCS_URL
LABEL org.label-schema.build-date=$BUILD_DATE \
org.label-schema.name=$IMAGE \
org.label-schema.vcs-ref=$VCS_REF \
org.label-schema.vcs-url=$VCS_URL \
org.label-schema.schema-version="1.0"
16 changes: 16 additions & 0 deletions fpm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
include ../Makefile.common

NAME := fpm
VERSION := 1.10.0

build:
@echo ">> Building $(REPOSITORY)/$(NAME):$(VERSION)$(SUFFIX)"
@docker build -t "$(REPOSITORY)/$(NAME):$(VERSION)$(SUFFIX)" \
--build-arg FPM_VERSION=$(VERSION) \
--build-arg IMAGE="$(REPOSITORY)/$(NAME):$(VERSION)$(SUFFIX)$(TAG_EXTENSION)" \
--build-arg VCS_REF="$(VCS_REF)" \
--build-arg VCS_URL="$(VCS_URL)" \
--build-arg BUILD_DATE="$(BUILD_DATE)" \
.

.PHONY: build
10 changes: 10 additions & 0 deletions fpm/rootfs/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env sh

if [ -z ${EXEC_UID+x} ] || [ -z ${EXEC_GID+x} ]; then
exec "$@"
fi

groupadd -f -g "$EXEC_GID" fpm
useradd -N -m -c 'FPM User' -u "$EXEC_UID" -g "$EXEC_GID" fpm

su-exec fpm "$@"
10 changes: 10 additions & 0 deletions go1.10/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
IMAGES := base main darwin arm mips ppc s390x

build:
@$(foreach var,$(IMAGES),$(MAKE) -C $(var) build || exit 1;)

# Requires login at https://docker.elastic.co:7000/.
push:
@$(foreach var,$(IMAGES),$(MAKE) -C $(var) push || exit 1;)

.PHONY: build push
23 changes: 23 additions & 0 deletions go1.10/Makefile.common
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST)))
include $(SELF_DIR)/../Makefile.common

NAME := golang-crossbuild
VERSION := 1.10.3
DEBIAN_VERSION ?= 9
SUFFIX := -$(shell basename $(CURDIR))
TAG_EXTENSION ?=

build:
@echo ">> Building $(REPOSITORY)/$(NAME):$(VERSION)$(SUFFIX)$(TAG_EXTENSION)"
@docker build -t "$(REPOSITORY)/$(NAME):$(VERSION)$(SUFFIX)$(TAG_EXTENSION)" \
--build-arg REPOSITORY=$(REPOSITORY) \
--build-arg VERSION=$(VERSION) \
--build-arg DEBIAN_VERSION=$(DEBIAN_VERSION) \
--build-arg TAG_EXTENSION=$(TAG_EXTENSION) \
--build-arg IMAGE="$(REPOSITORY)/$(NAME):$(VERSION)$(SUFFIX)$(TAG_EXTENSION)" \
--build-arg VCS_REF="$(VCS_REF)" \
--build-arg VCS_URL="$(VCS_URL)" \
--build-arg BUILD_DATE="$(BUILD_DATE)" \
.

.PHONY: build
14 changes: 14 additions & 0 deletions go1.10/Makefile.debian7
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
IMAGES := base main
DEBIAN_VERSION := 7
TAG_EXTENSION := -debian7

export DEBIAN_VERSION TAG_EXTENSION

build:
@$(foreach var,$(IMAGES),$(MAKE) -C $(var) build || exit 1;)

# Requires login at https://docker.elastic.co:7000/.
push:
@$(foreach var,$(IMAGES),$(MAKE) -C $(var) push || exit 1;)

.PHONY: build push
1 change: 1 addition & 0 deletions go1.10/arm/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Makefile
26 changes: 26 additions & 0 deletions go1.10/arm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
ARG REPOSITORY
ARG VERSION
FROM ${REPOSITORY}/golang-crossbuild:${VERSION}-base

RUN \
apt-get update && apt-get install -y --no-install-recommends \
crossbuild-essential-arm64 \
crossbuild-essential-armel \
crossbuild-essential-armhf \
linux-libc-dev-arm64-cross \
linux-libc-dev-armel-cross \
linux-libc-dev-armhf-cross \
&& rm -rf /var/lib/apt/lists/*

COPY rootfs /

# Build-time metadata as defined at http://label-schema.org.
ARG BUILD_DATE
ARG IMAGE
ARG VCS_REF
ARG VCS_URL
LABEL org.label-schema.build-date=$BUILD_DATE \
org.label-schema.name=$IMAGE \
org.label-schema.vcs-ref=$VCS_REF \
org.label-schema.vcs-url=$VCS_URL \
org.label-schema.schema-version="1.0"
1 change: 1 addition & 0 deletions go1.10/arm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../Makefile.common
17 changes: 17 additions & 0 deletions go1.10/arm/rootfs/compilers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---

linux:
armv7:
CC: arm-linux-gnueabihf-gcc
CXX: arm-linux-gnueabihf-g++
armv6:
# Using debian armel for ARMv6.
# This is the same as ARMv5 so it will use softfp instead of hardfp.
CC: arm-linux-gnueabi-gcc
CXX: arm-linux-gnueabi-g++
armv5:
CC: arm-linux-gnueabi-gcc
CXX: arm-linux-gnueabi-g++
arm64:
CC: aarch64-linux-gnu-gcc
CXX: aarch64-linux-gnu-g++
1 change: 1 addition & 0 deletions go1.10/base/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Makefile
55 changes: 55 additions & 0 deletions go1.10/base/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
ARG DEBIAN_VERSION
FROM debian:${DEBIAN_VERSION}

RUN \
apt-get update \
&& apt-get dist-upgrade -y \
&& apt-get install -y --no-install-recommends \
build-essential \
ca-certificates \
curl \
git \
gnupg \
make \
file \
flex \
bison \
&& rm -rf /var/lib/apt/lists/*

ARG GOLANG_VERSION=1.10.3
ARG GOLANG_DOWNLOAD_URL=https://golang.org/dl/go$GOLANG_VERSION.linux-amd64.tar.gz
ARG GOLANG_DOWNLOAD_SHA256=fa1b0e45d3b647c252f51f5e1204aba049cde4af177ef9f2181f43004f901035

RUN curl -fsSL "$GOLANG_DOWNLOAD_URL" -o golang.tar.gz \
&& echo "$GOLANG_DOWNLOAD_SHA256 golang.tar.gz" | sha256sum -c - \
&& tar -C /usr/local -xzf golang.tar.gz \
&& rm golang.tar.gz

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

RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" && chmod -R 777 "$GOPATH"
WORKDIR $GOPATH

ARG MAGE_VERSION=2.1
ARG MAGE_DOWNLOAD_URL=https://github.com/magefile/mage/releases/download/${MAGE_VERSION}/mage_${MAGE_VERSION}_Linux-64bit.tar.gz
ARG MAGE_DOWNLOAD_SHA256=44f4a34fcf7bd17c11c6ed2a7b495900c771289c580fdef517a950fdccd15e02

RUN curl -fsSL "$MAGE_DOWNLOAD_URL" -o mage.tar.gz \
&& echo "$MAGE_DOWNLOAD_SHA256 mage.tar.gz" | sha256sum -c - \
&& mkdir /usr/local/mage \
&& tar -C /usr/local/mage -xzf mage.tar.gz \
&& rm mage.tar.gz
ENV PATH /usr/local/mage:$PATH

COPY rootfs /

WORKDIR /
RUN go get -d . \
&& go build -o /crossbuild /entrypoint.go \
&& rm -rf /go/* /root/.cache/* /entrypoint.go

ENV GOLANG_CROSSBUILD=1
VOLUME /app
WORKDIR /app
ENTRYPOINT ["/crossbuild"]
1 change: 1 addition & 0 deletions go1.10/base/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../Makefile.common
Loading

0 comments on commit f047f0d

Please sign in to comment.