-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathContainerfile.base
More file actions
149 lines (129 loc) · 5.94 KB
/
Copy pathContainerfile.base
File metadata and controls
149 lines (129 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Codex CLI Base Image
#
# Multi-stage build that compiles Codex CLI from source (Apache 2.0)
# and produces a clean UBI 9 minimal runtime image. This is an alternative
# to the npm-based Containerfile for supply chain auditable builds.
#
# The builder uses cross-compilation so it runs natively on the host
# (e.g., ARM64 on Apple Silicon) while producing an x86_64 binary.
# This avoids QEMU emulation which crashes rustc with SIGSEGV.
# Cross-compiling requires 32GB+ RAM in the podman VM.
#
# Build:
# podman build --platform linux/amd64 \
# -t codex-base:latest \
# -f Containerfile.base .
#
# Build with specific version (git tag):
# podman build --platform linux/amd64 \
# --build-arg CODEX_VERSION=rust-v0.144.0 \
# -t codex-base:0.144.0 \
# -f Containerfile.base .
#
# License: Apache-2.0 (Codex CLI), Apache-2.0 (this file)
# =============================================================================
# Stage 1: Builder — cross-compile Codex from source
# =============================================================================
# $BUILDPLATFORM ensures this stage runs natively (no QEMU).
# We cross-compile to the $TARGETPLATFORM using Rust's --target flag.
FROM --platform=$BUILDPLATFORM docker.io/library/rust:1.87.0-bookworm AS builder
ARG CODEX_VERSION=rust-v0.144.0
ARG TARGETARCH
# Cross-compilation environment for C/C++ dependencies (openssl, v8, lzma, etc.)
ENV CC_x86_64_unknown_linux_gnu=x86_64-linux-gnu-gcc \
CXX_x86_64_unknown_linux_gnu=x86_64-linux-gnu-g++ \
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc \
PKG_CONFIG_ALLOW_CROSS=1 \
PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig \
OPENSSL_DIR=/usr \
OPENSSL_INCLUDE_DIR=/usr/include/x86_64-linux-gnu \
OPENSSL_LIB_DIR=/usr/lib/x86_64-linux-gnu
# Install system build deps + cross-compilation toolchain
# Enable amd64 as a foreign architecture for cross-compilation headers
RUN dpkg --add-architecture amd64 \
&& apt-get update && apt-get install -y --no-install-recommends \
git pkg-config make cmake \
libssl-dev \
# x86_64 cross-compilation toolchain
gcc-x86-64-linux-gnu g++-x86-64-linux-gnu \
libc6-dev-amd64-cross \
# x86_64 headers for cross-compiling native dependencies
libssl-dev:amd64 \
liblzma-dev:amd64 \
libcap-dev:amd64 \
&& rm -rf /var/lib/apt/lists/*
# Clone source first so we can read its rust-toolchain.toml
RUN git clone --depth 1 --branch "${CODEX_VERSION}" \
https://github.com/openai/codex.git /src
# The repo pins a specific Rust version via rust-toolchain.toml (e.g., 1.95.0).
# cargo auto-installs this toolchain but only for the native arch.
# We must add the x86_64 target to the PROJECT's pinned toolchain, not the
# image default, otherwise cargo can't find the x86_64 standard library.
RUN cd /src/codex-rs \
&& PINNED=$(sed -n 's/^channel *= *"\(.*\)"/\1/p' rust-toolchain.toml) \
&& echo "Project pins Rust ${PINNED}" \
&& rustup toolchain install "${PINNED}" \
&& rustup target add x86_64-unknown-linux-gnu --toolchain "${PINNED}" \
&& mkdir -p /root/.cargo \
&& printf '[target.x86_64-unknown-linux-gnu]\nlinker = "x86_64-linux-gnu-gcc"\n' \
>> /root/.cargo/config.toml
WORKDIR /src/codex-rs
# Build
# The upstream release profile uses thin LTO which requires 20GB+ RAM
# during cross-compilation linking. Disable it for cross builds —
# the binary is functionally identical, just slightly larger.
RUN CARGO_PROFILE_RELEASE_LTO=false \
cargo build --release --target x86_64-unknown-linux-gnu \
--bin codex --bin bwrap --bin codex-code-mode-host \
&& cp target/x86_64-unknown-linux-gnu/release/codex /opt/codex \
&& cp target/x86_64-unknown-linux-gnu/release/bwrap /opt/bwrap \
&& cp target/x86_64-unknown-linux-gnu/release/codex-code-mode-host /opt/codex-code-mode-host \
&& chmod +x /opt/codex /opt/bwrap /opt/codex-code-mode-host \
&& rm -rf target /src /usr/local/cargo/registry
# =============================================================================
# Stage 2: Runtime — clean UBI 9 minimal
# =============================================================================
FROM registry.access.redhat.com/ubi9/ubi-minimal@sha256:8201445bebcb5bd4fe23fcc2a76cd5fec029ab401d270926a1563c03b36f0137
ARG CODEX_VERSION=rust-v0.144.0
ARG USER_NAME=codex-agent
ARG USER_UID=1001
ARG USER_GID=0
LABEL name="codex-base" \
version="${CODEX_VERSION}" \
vendor="Red Hat" \
summary="Codex CLI base image for OpenShift AI" \
description="OpenAI Codex CLI (Apache 2.0) compiled from source on UBI 9 minimal" \
license="Apache-2.0" \
io.openshift.tags="codex,agent,cli" \
io.k8s.description="Codex CLI coding agent base image"
# System dependencies
# - git: Version control (agent clones repos, makes commits)
# - jq: JSON processor (config manipulation)
# - tar: Required by oc cp for file retrieval
# - nodejs + npm: Runtime for npx-based MCP servers
# Note: curl-minimal is pre-installed in ubi-minimal; full curl conflicts
RUN microdnf install -y --nodocs \
git \
jq \
tar \
nodejs \
npm \
&& microdnf clean all \
&& rm -rf /var/cache/yum
# Copy the compiled binaries from the builder
COPY --from=builder /opt/codex /usr/local/bin/codex
COPY --from=builder /opt/bwrap /usr/local/bin/bwrap
COPY --from=builder /opt/codex-code-mode-host /usr/local/bin/codex-code-mode-host
# Create non-root user (OpenShift restricted-v2 SCC compatible)
RUN useradd -u ${USER_UID} -g ${USER_GID} -d /home/${USER_NAME} -m -s /bin/bash ${USER_NAME}
# Workspace and config directories
RUN mkdir -p /workspace/projects /home/${USER_NAME}/.codex \
&& chown -R ${USER_UID}:${USER_GID} /workspace /home/${USER_NAME} \
&& chmod -R 775 /workspace /home/${USER_NAME}
ENV HOME="/home/${USER_NAME}" \
PATH="/home/${USER_NAME}/.local/bin:${PATH}"
# Verify
RUN codex --version
USER ${USER_UID}
WORKDIR /workspace/projects
ENTRYPOINT ["codex"]