From 1d3ca46de29a1a4d5f014cf7927d2cbd4f3a60c0 Mon Sep 17 00:00:00 2001 From: Spencer Heywood Date: Thu, 24 Apr 2025 21:46:10 -0600 Subject: [PATCH] added multistage docker builds with docker instructions in readme --- README.md | 26 +++++++++++++++ extra/docker/Dockerfile | 34 ++++++++++++++++++++ extra/docker/Dockerfile.alpine | 31 ------------------ extra/docker/Dockerfile.ubuntu | 58 +++++++++++++++++----------------- 4 files changed, 89 insertions(+), 60 deletions(-) create mode 100644 extra/docker/Dockerfile delete mode 100644 extra/docker/Dockerfile.alpine diff --git a/README.md b/README.md index d4521d55..dbfe4652 100755 --- a/README.md +++ b/README.md @@ -137,6 +137,32 @@ See __--enable-jnat__ below The toolset is build upon the autotools framework. Run `./autogen.sh` first. Afterwards `./configure` `make` and `make install` should do the trick. +### Docker + +> These commands assume the current working directory is the root of the nfdump repository + +To build and run the `nfcapd` target (runs `nfcapd` by default): + +``` +docker build -t nfcapd --target nfcapd -f extra/docker/Dockerfile . +mkdir -p /tmp/flows +docker run -it --rm --name=nfcapd -p 9995:9995/udp -v /tmp/flows:/data nfcapd +``` + +Desired arguments to `nfcapd` can be appended to the `docker run` command above. + +To build the `nfdump` target (drops you into an interactive shell by default): + +``` +docker build -t nfdump --target nfdump -f extra/docker/Dockerfile . +mkdir -p /tmp/flows +docker run -it --rm --name=nfdump -v /tmp/flows:/data nfdump +``` + +For reference, there is also an Ubuntu Dockerfile at _extra/docker/Dockerfile.ubuntu_ with similar `nfcapd` and `nfdump` targets. + +### Older Linux-distribution notes + For various older Linuxes need a more modern compiler: #### CentOS 7.x: diff --git a/extra/docker/Dockerfile b/extra/docker/Dockerfile new file mode 100644 index 00000000..978e5195 --- /dev/null +++ b/extra/docker/Dockerfile @@ -0,0 +1,34 @@ +ARG ALPINE_VERSION=3 +FROM alpine:${ALPINE_VERSION} AS builder + +# Assume context is root of nfdump repository +COPY . /app +RUN apk add --no-cache build-base gcc abuild binutils make \ + libtool bzip2-dev libpcap-dev flex bison \ + autoconf automake m4 pkgconfig + +WORKDIR /app + +RUN ./autogen.sh \ + && ./configure --enable-maxmind --enable-nfpcapd --enable-sflow=yes --with-lz4path=/usr --with-zstdpath=/usr --with-bz2path=/usr \ + && make && make install + +FROM alpine:${ALPINE_VERSION} AS base + +VOLUME /data +RUN apk add --no-cache bzip2-dev libpcap-dev lz4-libs zstd \ + && mkdir -p /data + +COPY --from=builder /usr/local /usr/local + +FROM base AS nfcapd + +EXPOSE 9995/udp + +ENTRYPOINT ["/usr/local/bin/nfcapd"] + +CMD ["-w", "/data", "-S", "1", "-y", "-p", "9995"] + +FROM base AS nfdump + +ENTRYPOINT ["/bin/ash"] diff --git a/extra/docker/Dockerfile.alpine b/extra/docker/Dockerfile.alpine deleted file mode 100644 index dd6fa843..00000000 --- a/extra/docker/Dockerfile.alpine +++ /dev/null @@ -1,31 +0,0 @@ -# -# Example Alpine Dockerfile -# - -FROM alpine:3 - -ARG NFDUMP_VERSION=1.7.3 - -WORKDIR /tmp - -ADD https://github.com/phaag/nfdump/archive/refs/tags/v$NFDUMP_VERSION.tar.gz /tmp -RUN \ - apk add --no-cache build-base gcc abuild binutils make && - apk add --no-cache libtool bzip2-dev libpcap-dev flex bison && - apk add --no-cache autoconf automake m4 pkgconfig - -RUN \ - tar zxf v$NFDUMP_VERSION.tar.gz && - cd /tmp/nfdump-$NFDUMP_VERSION && - ./autogen.sh && - ./configure --enable-maxmind --enable-nfpcapd --enable-sflow=yes && - make && - cd /tmp/nfdump-$NFDUMP_VERSION && make install && - cd .. && - rm -rf nfdump-v$NFDUMP_VERSION && - rm /tmp/v$NFDUMP_VERSION.tar.gz && - apk del build-base gcc flex bison autoconf automake m4 pkgconfig - -VOLUME /data - -CMD ["nfcapd", "-w", "/data", "-S", "1", "-y", "-p", "9999"] diff --git a/extra/docker/Dockerfile.ubuntu b/extra/docker/Dockerfile.ubuntu index 4ee89029..01f0c491 100644 --- a/extra/docker/Dockerfile.ubuntu +++ b/extra/docker/Dockerfile.ubuntu @@ -1,16 +1,14 @@ # -# Example Ubuntu Dockerfile +# Reference Ubuntu Dockerfile # -# Pull base image. -FROM ubuntu:latest +ARG UBUNTU_VERSION=24.04 +FROM ubuntu:${UBUNTU_VERSION} AS builder -ARG NFDUMP_VERSION=1.7.3 +# Assume context is root of nfdump repository +COPY . /app -#Expose netflow port -EXPOSE 9995/udp - -# Install. +# Install dependencies RUN apt-get update && apt-get install -y \ wget \ unzip \ @@ -26,30 +24,32 @@ RUN apt-get update && apt-get install -y \ flex \ make \ libpcap-dev \ - libbz2-dev && - rm -rf /var/lib/apt/lists/* + libbz2-dev \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /app +RUN ./autogen.sh \ + && ./configure --enable-nfpcapd --enable-maxmind --enable-sflow \ + && make && make install \ + && ldconfig + +FROM ubuntu:${UBUNTU_VERSION} AS base -RUN cd /usr/src && - wget https://github.com/phaag/nfdump/archive/refs/tags/v$NFDUMP_VERSION.tar.gz && - tar xfz v$NFDUMP_VERSION.tar.gz && - cd nfdump-$NFDUMP_VERSION && - ./autogen.sh && - ./configure --enable-nfpcapd --enable-maxmind --enable-sflow && - make && - make install +COPY --from=builder /usr/local /usr/local +RUN apt-get update && apt-get install -y libbz2-dev \ + && rm -rf /var/lib/apt/lists/* -RUN ldconfig +VOLUME /data +RUN mkdir -p /data + +FROM base AS nfcapd + +EXPOSE 9995/udp -# Add files. -#ADD root/.bashrc /root/.bashrc -#ADD root/.gitconfig /root/.gitconfig -#ADD root/.scripts /root/.scripts +ENTRYPOINT ["/usr/local/bin/nfcapd"] -# Set environment variables. -#ENV HOME /root +CMD ["-w", "/data", "-S", "1", "-y", "-p", "9995"] -# Define working directory. -WORKDIR /usr/src +FROM base AS nfdump -# Define default command. -CMD ["bash"] +ENTRYPOINT ["/bin/bash"]