This page covers building the Docker image from source, including the multi-stage build process, build arguments, and platform-specific builds.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/ragnarok22/telegram-bot-api-docker/llms.txt
Use this file to discover all available pages before exploring further.
Dockerfile Structure
The project uses a two-stage build to minimize the final image size:Stage 1: Build Stage
Compiles the Telegram Bot API server from source:- Uses
--platform=$BUILDPLATFORMfor faster cross-compilation - Shallow clones the upstream repository to reduce build time
- Compiles with
Releaseconfiguration for optimal performance - Installs build dependencies only in this stage
Stage 2: Runtime Stage
Creates the minimal production image:- Only runtime dependencies (libstdc++, libgcc) are installed
- Runs as non-root user
botapifor security - Includes health check for container orchestration
- Exposes ports 8081 (API) and 8082 (stats)
Building Locally
Build Arguments and Customization
The Dockerfile supports Docker’s built-in build arguments for multi-platform builds:Platform-Specific Builds
Build for a specific platform:Multi-Architecture Build
Create a multi-arch manifest (requires Docker Buildx):The
--platform=$BUILDPLATFORM instruction in the build stage ensures cross-compilation uses the host’s native architecture, significantly speeding up builds on Apple Silicon and other ARM64 hosts.Image Layers and Optimization
The two-stage build provides several optimizations:- Reduced image size: Build tools (cmake, gcc, git) are excluded from the final image
- Faster builds: Shallow git clone reduces download time
- Layer caching: Dependencies are installed before source compilation
- Security: Minimal runtime dependencies reduce attack surface
- Build stage: ~500-600 MB
- Final image: ~50-60 MB
Building on Apple Silicon
On Apple Silicon (M1/M2/M3), Docker automatically builds forlinux/arm64/v8:
Understanding the Build Process
The build follows these steps:Build stage initialization
- Pull Alpine 3.23.3 base image for the build platform
- Install C++ toolchain, CMake, and dependencies
Source code retrieval
- Shallow clone telegram-bot-api repository
- Recursively clone submodules (TDLib dependencies)
Compilation
- Configure CMake with Release build type
- Compile telegram-bot-api binary
- Install to
/telegram-bot-api/bin
Runtime image creation
- Start fresh Alpine 3.23.3 image
- Copy only the compiled binary from build stage
- Install minimal runtime libraries
Security hardening
- Create non-root
botapiuser and group - Set file ownership and permissions
- Configure volume mounts and exposed ports