Sobes.tech
Middle

What is your experience with dynamic parameterization of Docker files?

sobes.tech AI

Answer from AI

Developed template Dockerfiles using environment variables and build arguments.

Applied the following methods:

  1. ARG instructions for build arguments:

    FROM ubuntu:latest
    ARG APP_VERSION
    RUN echo "Building version: $APP_VERSION"
    
  2. ENV instructions for runtime environment variables:

    FROM alpine:latest
    ENV PORT=8080
    CMD ["sh", "-c", "echo Listening on port $PORT"]
    
  3. Passing ARG and ENV via --build-arg and -e during build/run:

    docker build --build-arg APP_VERSION=1.2.0 .
    docker run -e PORT=9000 my_image
    
  4. Using multi-stage builds to pass values between stages:

    # builder stage
    FROM golang:1.20 as builder
    ARG BUILD_TAG
    WORKDIR /app
    RUN go build -ldflags "-X main.version=$BUILD_TAG" -o app .
    
    # final stage
    FROM alpine:latest
    COPY --from=builder /app/app /
    CMD ["/app"]
    
  5. Integration with CI/CD systems (e.g., GitLab CI, Jenkins) for automatic substitution of parameters (application version, environment, API keys) during image build or container deployment.

This approach allows creating more flexible and reusable Docker images, adaptable to various environments and requirements, without modifying the Dockerfile itself.