Write a Dockerfile to create haproxy image of version 2.8.10 with openssl and cjson lua…
- User
Write a Dockerfile to create haproxy image of version 2.8.10 with openssl and cjson lua libraries
Assistant
Okay, here is a Dockerfile that builds HAProxy version 2.8.10 from source on Alpine Linux, enabling OpenSSL support and including the
cjsonLua library. This uses a multi-stage build to keep the final image small.1.
Dockerfile:# ---- Builder Stage ---- FROM alpine:3.19 AS builder # Arguments for versions ARG HAPROXY_VERSION=2.8.10 ARG ALPINE_VERSION=3.19 # Set HAProxy source URL (adjust mirror if needed) ENV HAPROXY_SOURCE_URL="https://www.haproxy.org/download/${HAPROXY_VERSION%.*}/src/haproxy-${HAPROXY_VERSION}.tar.gz" ENV HAPROXY_INSTALL_DIR="/usr/local/sbin/haproxy" # Install build dependencies # - build-base: Essential build tools (make, gcc, etc.) # - linux-headers: Kernel headers for building # - pcre-dev: For PCRE regex support (often needed by HAProxy) # - openssl-dev: For OpenSSL library support # - lua, lua-dev: For Lua runtime and development headers/libraries # - wget, tar, gzip: For downloading and extracting source RUN apk add --no-cache --virtual .build-deps \ build-base \ linux-headers \ pcre-dev \ openssl-dev \ lua \ lua-dev \ wget \ tar \ gzip # Download HAProxy source code RUN wget --progress=bar:force ${HAPROXY_SOURCE_URL} -O haproxy.tar.gz && \ echo "Downloaded HAProxy source" # Extract HAProxy source code RUN tar -xzf haproxy.tar.gz && \ echo "Extracted HAProxy source" # Move source to a standard location RUN mv haproxy-${HAPROXY_VERSION} /usr/local/src/haproxy-${HAPROXY_VERSION} && \ echo "Moved HAProxy source to /usr/local/src" # Compile HAProxy with desired options # TARGET=linux-musl: Use musl C library (standard for Alpine) # USE_OPENSSL=1: Enable OpenSSL support # USE_PCRE_JIT=1: Enable PCRE JIT support for better regex performance # USE_LUA=1: Enable Lua support RUN cd /usr/local/src/haproxy-${HAPROXY_VERSION} && \ make TARGET=linux-musl USE_OPENSSL=1 USE_PCRE_JIT=1 USE_LUA=1 && \ echo "Compiled HAProxy" # Install HAProxy binary RUN cd /usr/local/src/haproxy-${HAPROXY_VERSION} && \ make install && \ echo "Installed HAProxy binary" # Clean up build source and tarball RUN cd /usr/local/src && rm -rf haproxy-${HAPROXY_VERSION} haproxy.tar.gz && \ echo "Cleaned up build artifacts" # Remove build dependencies to reduce layer size RUN apk del .build-deps && \ echo "Removed build dependencies" # ---- Runtime Stage ---- FROM alpine:${ALPINE_VERSION} # Install runtime dependencies # - openssl: Required runtime libraries for SSL # - lua: Required Lua runtime # - lua-cjson: The cjson library for Lua (installed via apk) # - pcre: Required runtime libraries for PCRE regex # - ca-certificates: For verifying SSL certificates RUN apk add --no-cache \ openssl \ lua \ lua-cjson \ pcre \ ca-certificates # Create HAProxy user and group (non-root) RUN addgroup -S haproxy && adduser -S -G haproxy haproxy # Copy the compiled HAProxy binary from the builder stage COPY --from=builder /usr/local/sbin/haproxy /usr/local/sbin/haproxy # Create configuration directory RUN mkdir -p /etc/haproxy # Copy a default HAProxy configuration file (optional, create haproxy.cfg locally) # If you don't have one, HAProxy will start but likely fail unless configured otherwise. COPY haproxy.cfg /etc/haproxy/haproxy.cfg # Ensure the configuration directory and file are owned by root, # but HAProxy runs as the 'haproxy' user. RUN chown -R root:root /etc/haproxy # Switch to the non-root HAProxy user USER haproxy # Expose standard HAProxy ports (adjust as needed) EXPOSE 80 EXPOSE 443 # Set the entrypoint to run HAProxy with the configuration file # Using "$@" allows passing additional arguments like -db, -dp, -cc, etc. at runtime ENTRYPOINT ["/usr/local/sbin/haproxy", "-f", "/etc/haproxy/haproxy.cfg"] # Default command (optional) - runs HAProxy in the foreground without extra args CMD []2. Sample
haproxy.cfg(Place this file in the same directory as the Dockerfile):global log stdout format raw local0 maxconn 2000 daemon defaults mode http timeout client 10s timeout server 10s timeout connect 5s log global frontend http_front bind *:80 # Example: Use Lua script for custom logic (e.g., JSON processing) # lua-load /etc/haproxy/lua/my_script.lua # http-request use-lua lua.my_http_handler # You'll need to create and copy a Lua script like my_script.lua # and ensure cjson is available to it. # Example using cjson directly (less common, usually called from script): # http-request set-header X-Lua-Cjson-Loaded lua-cjson.version() if { hdr(Host) -i example.com } default_backend http_back backend http_back balance roundrobin server http_srv1 127.0.0.1:8080 check # Replace with your backend serverExplanation:
-
Builder Stage (
AS builder):- Starts with a minimal
alpine:3.19image. - Installs necessary build tools (
build-base,linux-headers,pcre-dev,openssl-dev,lua,lua-dev,wget, etc.). We use--virtual .build-depsto group them for easy removal later. - Downloads the HAProxy 2.8.10 source tarball using
wget. - Extracts the source code.
- Compiles HAProxy using
makewith specific flags:TARGET=linux-musl: Optimized for Alpine's C library.USE_OPENSSL=1: Enables HTTPS and SSL features.USE_PCRE_JIT=1: Enables Just-In-Time compilation for PCRE regex, improving performance.USE_LUA=1: Enables Lua scripting support.
- Installs the compiled binary to
/usr/local/sbin/haproxy. - Cleans up the downloaded tarball and extracted source directories.
- Removes the build dependencies using
apk del .build-deps.
- Starts with a minimal
-
Runtime Stage:
- Starts fresh from
alpine:3.19. - Installs only the runtime dependencies:
openssl,lua,lua-cjson,pcre, andca-certificates.lua-cjsonis installed directly viaapk, which is the easiest way on Alpine. - Creates a dedicated, non-root
haproxyuser and group for better security. - Copies the compiled HAProxy binary from the
builderstage usingCOPY --from=builder. - Creates the
/etc/haproxydirectory for the configuration. - Copies a sample
haproxy.cfgfile into the container. You should customize this file. - Sets the
USERtohaproxy. - Exposes ports 80 and 443 (common defaults).
- Sets the
ENTRYPOINTto/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg, ensuring HAProxy starts with the correct config file when the container launches. - The
CMD []is empty, allowing you to pass runtime arguments to HAProxy (like-dbfor debug mode) when starting the container (docker run ... haproxy -db).
- Starts fresh from
To build the image:
- Save the Dockerfile contents as
Dockerfile. - Save the sample configuration as
haproxy.cfgin the same directory. - Run the build command:
docker build -t my-haproxy:2.8.10 .
To run the container:
docker run -d -p 8080:80 --name haproxy_instance my-haproxy:2.8.10 # Or run with debug flags: # docker run -it --rm my-haproxy:2.8.10 -dbThis setup provides a secure, efficient HAProxy image with the requested features. Remember to tailor the
haproxy.cfgto your specific load balancing needs.-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.