Skip to content

Build Redis using Dockerfile

Purpose

This document provides instructions for building, tagging, pushing, and running the Redis Docker image. It also outlines the required file structure

Dockerfile

Use the following Dockerfile:

# Dockerfile
FROM alpine:3.23

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added

RUN addgroup -S -g 1000 oneuser && adduser -S -G oneuser -u 1000 oneuser

# runtime dependencies
RUN set -eux; \
    apk add --no-cache \
        tzdata \
        setpriv \
    ;

ARG REDIS_DOWNLOAD_URL=https://github.com/redis/redis/archive/refs/tags/8.6.2.tar.gz
ARG REDIS_DOWNLOAD_SHA=cef021615ec4aef355a824cf933702be5def36e37ca1e9b99ab1cc5599f1748f

RUN set -eux; \
    \
    apk add --no-cache --virtual .build-deps \
        coreutils \
        dpkg-dev dpkg \
        gcc \
        linux-headers \
        make \
        musl-dev \
        openssl-dev \
        g++; \
    \
    arch="$(dpkg --print-architecture | awk -F- '{ print $NF }')"; \
    case "$arch" in \
        'amd64') export BUILD_WITH_MODULES=yes; export INSTALL_RUST_TOOLCHAIN=yes; export DISABLE_WERRORS=yes ;; \
        'arm64') export BUILD_WITH_MODULES=yes; export INSTALL_RUST_TOOLCHAIN=yes; export DISABLE_WERRORS=yes ;; \
        *) echo >&2 "Modules are NOT supported! unsupported architecture: '$arch'"; export BUILD_WITH_MODULES=no ;; \
    esac; \
    if [ "$BUILD_WITH_MODULES" = "yes" ]; then \
    apk add --no-cache --virtual .module-build-deps \
        autoconf \
        automake \
        bash \
        bsd-compat-headers \
        build-base \
        cargo \
        clang21 \
        clang21-static \
        clang21-libclang \
        cmake \
        curl \
        g++ \
        git \
        libffi-dev \
        libgcc \
        libtool \
        llvm21-dev \
        ncurses-dev \
        openssh \
        openssl  \
        py-virtualenv \
        py3-cryptography \
        py3-pip \
        py3-virtualenv \
        python3 \
        python3-dev \
        rsync \
        tar \
        unzip \
        which \
        xsimd \
        xz; \
    fi; \
    \
    # install required python packages for RedisJSON module
    pip install -q --upgrade setuptools && pip install -q --upgrade pip && PIP_BREAK_SYSTEM_PACKAGES=1 pip install -q addict toml jinja2 ramp-packer; \
    wget -O redis.tar.gz "$REDIS_DOWNLOAD_URL"; \
    echo "$REDIS_DOWNLOAD_SHA *redis.tar.gz" | sha256sum -c -; \
    mkdir -p /usr/src/redis; \
    tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1; \
    rm redis.tar.gz; \
    \
    # disable Redis protected mode
    grep -E '^ *createBoolConfig[(]"protected-mode",.*, *1 *,.*[)],$' /usr/src/redis/src/config.c; \
    sed -ri 's!^( *createBoolConfig[(]"protected-mode",.*, *)1( *,.*[)],)$!\10\2!' /usr/src/redis/src/config.c; \
    grep -E '^ *createBoolConfig[(]"protected-mode",.*, *0 *,.*[)],$' /usr/src/redis/src/config.c; \
    \
    gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; \
    extraJemallocConfigureFlags="--build=$gnuArch"; \
    dpkgArch="$(dpkg --print-architecture)"; \
    case "${dpkgArch##*-}" in \
        amd64 | i386 | x32) extraJemallocConfigureFlags="$extraJemallocConfigureFlags --with-lg-page=12" ;; \
        *) extraJemallocConfigureFlags="$extraJemallocConfigureFlags --with-lg-page=16" ;; \
    esac; \
    extraJemallocConfigureFlags="$extraJemallocConfigureFlags --with-lg-hugepage=21"; \
    grep -F 'cd jemalloc && ./configure ' /usr/src/redis/deps/Makefile; \
    sed -ri 's!cd jemalloc && ./configure !&'"$extraJemallocConfigureFlags"' !' /usr/src/redis/deps/Makefile; \
    grep -F "cd jemalloc && ./configure $extraJemallocConfigureFlags " /usr/src/redis/deps/Makefile; \
    \
    # Disable static linking the C runtime for RediSearch's rust submodule
    export RUST_DYN_CRT=1; \
    export PATH="/usr/lib/llvm21/bin:$PATH"; \
    export BUILD_TLS=yes; \
    if [ "$BUILD_WITH_MODULES" = "yes" ]; then \
        make -C /usr/src/redis/modules/redisjson get_source; \
        sed -i 's/^RUST_FLAGS=$/RUST_FLAGS += -C target-feature=-crt-static/' /usr/src/redis/modules/redisjson/src/Makefile ; \
        grep -E 'RUST_FLAGS' /usr/src/redis/modules/redisjson/src/Makefile; \
    fi; \
    make -C /usr/src/redis -j "$(nproc)" all; \
    make -C /usr/src/redis install; \
    \
    serverMd5="$(md5sum /usr/local/bin/redis-server | cut -d' ' -f1)"; export serverMd5; \
    find /usr/local/bin/redis* -maxdepth 0 \
        -type f -not -name redis-server \
        -exec sh -eux -c ' \
            md5="$(md5sum "$1" | cut -d" " -f1)"; \
            test "$md5" = "$serverMd5"; \
        ' -- '{}' ';' \
        -exec ln -svfT 'redis-server' '{}' ';' \
    ; \
    \
    make -C /usr/src/redis distclean; \
    rm -r /usr/src/redis; \
    \
    runDeps="$( \
        scanelf --needed --nobanner --format '%n#p' --recursive /usr/local \
            | tr ',' '\n' \
            | sort -u \
            | awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
    )"; \
    apk add --no-network --virtual .redis-rundeps $runDeps; \
    if [ "$BUILD_WITH_MODULES" = "yes" ]; then \
        apk del --no-network .module-build-deps; \
    fi; \
    apk del --no-network .build-deps; \
    rm -rf ~/.cache ~/.gitconfig; \
    \
    redis-cli --version; \
    redis-server --version;

RUN mkdir /data && chgrp -R 0 /data && chmod -R g=u /data

WORKDIR /data

COPY entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/entrypoint.sh

USER oneuser

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

EXPOSE 6379

entrypoint.sh

#!/bin/sh
set -e
redis-server /etc/redis/redis.conf

Build and Push to Container Registry

docker build -t redis <registry>/redis:8.6.2

docker login <registry>
docker push <registry>/redis:8.6.2