Skip to content

Commit 974c27c

Browse files
committed
Add a make target to build a static bin
Signed-off-by: Albin Kerouanton <albinker@gmail.com>
1 parent b801b14 commit 974c27c

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*
2+
3+
!go.mod
4+
!go.sum
5+
!*.go

Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM golang:1.20-bookworm AS build
2+
3+
RUN apt-get update && \
4+
apt-get install -y --no-install-recommends bison flex && \
5+
rm -r /var/lib/apt/*
6+
7+
WORKDIR /build
8+
9+
ENV LIBPCAP_VERSION=1.10.4
10+
RUN wget http://www.tcpdump.org/release/libpcap-${LIBPCAP_VERSION}.tar.gz && \
11+
tar xvf libpcap-${LIBPCAP_VERSION}.tar.gz && \
12+
cd libpcap-${LIBPCAP_VERSION} && \
13+
./configure && \
14+
make
15+
16+
COPY . /build
17+
18+
ENV LD_LIBRARY_PATH="-L/build/libpcap-${LIBPCAP_VERSION}" \
19+
CGO_LDFLAGS="-L/build/libpcap-${LIBPCAP_VERSION}" \
20+
CGO_CPPFLAGS="-I/build/libpcap-${LIBPCAP_VERSION}"
21+
RUN GOOS=linux GOARCH=amd64 go build -ldflags "-linkmode 'external' -extldflags '-static' -s -w" -o bin/iptables-tracer .
22+
23+
####################
24+
25+
FROM scratch AS binary
26+
WORKDIR /bin
27+
COPY --from=build /build/bin/iptables-tracer /bin/

Makefile

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
.PHONY: build
2-
build:
1+
IMAGE_TAG = akerouanton/iptables-tracer:latest
2+
BUILD_OPTS =
3+
4+
ifdef BUILDER
5+
BUILD_OPTS := --builder=${BUILDER}
6+
endif
7+
8+
.PHONY: binary
9+
binary:
310
if [ ! -d bin/ ]; then mkdir bin; fi
4-
go build -o bin/iptables-tracer ./
11+
docker build ${BUILD_OPTS} -t ${IMAGE_TAG} --target=binary .
12+
undock --include=/bin docker-daemon://${IMAGE_TAG} .
513

614
install:
715
sudo cp bin/iptables-tracer /usr/local/sbin/iptables-tracer

0 commit comments

Comments
 (0)