Skip to content

Commit

Permalink
Merge pull request #408 from mmgordon82/feature/dockerfile-refactor
Browse files Browse the repository at this point in the history
Optimize Dockerfiles: Reduce Image Sizes and Improve Build Process
  • Loading branch information
genivia-inc authored Jul 2, 2024
2 parents d8a1257 + f639a37 commit 2f961aa
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 59 deletions.
49 changes: 32 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
# step 1: create a debian or ubuntu container for ugrep named "ugrep"
# docker -D build --no-cache -t ugrep .
#
# step 2: run bash in the container, e.g. to run ugrep from the command line
# docker run -it ugrep bash
# or
# docker run -it --mount type=bind,source=$PWD,target=/mnt ugrep bash
# Dockerfile to build and run ugrep in a container (development environment).
#
# step 3: run ugrep in the container, for example:
# ugrep -r -n -tjava Hello ugrep/tests/
# This file creates the environment to build and run ugrep in a container. If
# you just want to use ugrep in a minimal container, use the minimized Dockerfile.
#
# Build the ugrep image:
# $ docker build -t ugrep .
#
# Execute ugrep in the container (The / of the host is accessible in /mnt directory in the container):
# $ docker run -v /:/mnt -it ugrep --help
# or run bash in the container instead:
# $ docker run -v /:/mnt --entrypoint /bin/bash -it ugrep
# $ ugrep --help

# debian or ubuntu
FROM ubuntu

RUN apt-get update

RUN apt-get install -y \
RUN apt-get install -y --no-install-recommends \
autoconf \
automake \
build-essential \
ca-certificates \
pkg-config \
make \
vim \
git \
clang \
wget \
unzip \
libpcre2-dev \
libz-dev \
Expand All @@ -29,9 +35,18 @@ RUN apt-get install -y \
libzstd-dev \
libbrotli-dev

RUN cd / && \
git clone https://github.com/Genivia/ugrep
WORKDIR /ugrep

# Clone ugrep from GitHub
RUN git clone --single-branch --depth=1 https://github.com/Genivia/ugrep /ugrep

# Local build of ugrep
# If you want to build ugrep from a local source, uncomment the following line:
# ADD . /ugrep

RUN autoreconf -fi
RUN ./build.sh
RUN make install
RUN ugrep --version

RUN cd ugrep && \
./build.sh && \
make install
ENTRYPOINT [ "ugrep" ]
118 changes: 76 additions & 42 deletions Dockerfile-minimized
Original file line number Diff line number Diff line change
@@ -1,44 +1,78 @@
# step 1: create a debian or ubuntu minimized container for ugrep named "ugrep"
# docker -D build --no-cache -t ugrep .
#
# step 2: run bash in the container, e.g. to run ugrep from the command line
# docker run -it ugrep bash
# or
# docker run -it --mount type=bind,source=$PWD,target=/mnt ugrep bash
# Dockerfile to build and run ugrep in a container (production/minimized environment).
#
# step 3: run ugrep in the container

# debian or ubuntu
FROM ubuntu

RUN apt-get update && \
apt-get install --no-install-recommends -y \
make \
git \
clang \
ca-certificates \
libpcre2-dev \
libz-dev \
libbz2-dev \
liblzma-dev \
liblz4-dev \
libzstd-dev \
libbrotli-dev && \
git clone --depth=1 https://github.com/Genivia/ugrep && \
cd ugrep && \
./build.sh && \
make install && \
ugrep -V && \
cd ../ && \
rm -rf ugrep && \
apt-get remove -y \
make \
git \
libpcre2-dev \
libz-dev \
libbz2-dev \
liblzma-dev \
liblz4-dev \
libzstd-dev \
libbrotli-dev && \
rm -rf /var/lib/apt/lists/*
# This file creates the bare essentials to run ugrep in a container, with nothing else.
# It's a minimized environment, with no development tools, no git, no make, etc.
# It's perfect for running ugrep in a compact container.
#
# Build the ugrep image:
# $ docker build -f Dockerfile-minimized -t ugrep .
#
# Execute ugrep in the container (The / of the host is accessible in /mnt directory in the container):
# $ docker run -v /:/mnt -it ugrep --help
# or run bash in the container instead:
# $ docker run -v /:/mnt --entrypoint /bin/bash -it ugrep
# $ ugrep --help

FROM ubuntu AS builder

RUN apt-get update

RUN apt-get install -y --no-install-recommends \
autoconf \
automake \
build-essential \
ca-certificates \
pkg-config \
make \
vim \
git \
clang \
wget \
unzip \
libpcre2-dev \
libz-dev \
libbz2-dev \
liblzma-dev \
liblz4-dev \
libzstd-dev \
libbrotli-dev

WORKDIR /ugrep

# Clone ugrep from GitHub
RUN git clone --single-branch --depth=1 https://github.com/Genivia/ugrep /ugrep

# Local build of ugrep
# If you want to build ugrep from a local source, uncomment the following line:
# ADD . /ugrep

RUN autoreconf -fi
RUN ./build.sh



# Create a tarball with the necessary libraries to run ugrep in a minimized environment
# Some large libraries are excluded to keep the image size small
RUN tar -czvf /libs.tar.gz \
--exclude=/lib/x86_64-linux-gnu/libclang* \
--exclude=/lib/x86_64-linux-gnu/libicudata* \
--exclude=/lib/x86_64-linux-gnu/libtsan* \
/lib/x86_64-linux-gnu/*.so* /lib64/*

# Use clean new image (alpine is small and compact compared to ubuntu)
FROM alpine AS production


# Copy libraries from the builder image
COPY --from=builder /libs.tar.gz /
RUN tar -C / -xzvf /libs.tar.gz
RUN rm /libs.tar.gz

# TODO: find a way to make `make install` work instead of calling install manually
WORKDIR /ugrep
COPY --from=builder /ugrep/bin/ug* ./
RUN install -d /usr/local/bin/ && install -D /ugrep/* /usr/local/bin/
RUN ugrep --version

ENTRYPOINT [ "ugrep" ]

0 comments on commit 2f961aa

Please sign in to comment.