This documentation provides a comprehensive overview of how Docker is utilized within the development environment of the Thanos project, specifically for expert developers familiar with the codebase.

Overview

Docker plays a significant role in the Thanos development environment by allowing developers to build, test, and package the Thanos application within containerized environments. This approach simplifies dependencies management and ensures consistency across different development setups.

Setting Up the Development Environment with Docker

1. Dockerfile

The core Docker image used during development is specified in the Dockerfile. It is responsible for building the Thanos application into a Docker image.

# By default, we pin to amd64 sha. Use make docker to automatically adjust for arm64 versions.
ARG BASE_DOCKER_SHA="14d68ca3d69fceaa6224250c83d81d935c053fb13594c811038c461194599973"
FROM quay.io/prometheus/busybox@sha256:${BASE_DOCKER_SHA}
LABEL maintainer="The Thanos Authors"

COPY /thanos_tmp_for_docker /bin/thanos

RUN adduser \
    -D `#Don't assign a password` \
    -H `#Don't create home directory` \
    -u 1001 `#User id` \
    thanos && \
    chown thanos /bin/thanos
USER 1001
ENTRYPOINT [ "/bin/thanos" ]

This Dockerfile defines the base image to be used from Prometheus, sets up user permissions, and specifies the entry point for the Docker container.

2. Multi-Stage Docker Build

In certain cases, the multi-stage Docker build process is utilized to optimize image size and build time. The below example demonstrates how to effectively use this technique in the Dockerfile.multi-stage file.

# By default, we pin to amd64 sha. Use make docker to automatically adjust for arm64 versions.
ARG BASE_DOCKER_SHA="14d68ca3d69fceaa6224250c83d81d935c053fb13594c811038c461194599973"
FROM golang:1.21-alpine3.18 as builder

WORKDIR $GOPATH/src/github.com/thanos-io/thanos
RUN apk update && apk add --no-cache alpine-sdk
COPY . $GOPATH/src/github.com/thanos-io/thanos

RUN make build

FROM quay.io/prometheus/busybox@sha256:${BASE_DOCKER_SHA}
LABEL maintainer="The Thanos Authors"

COPY --from=builder /go/bin/thanos /bin/thanos

RUN adduser \
    -D `#Don't assign a password` \
    -H `#Don't create home directory` \
    -u 1001 `#User id` \
    thanos && \
    chown thanos /bin/thanos
USER 1001
ENTRYPOINT [ "/bin/thanos" ]

The above example illustrates a two-stage build where the first stage compiles the Thanos binary, and the second stage creates a smaller final image.

3. Building the Docker Image

To build the Docker image for Thanos, the Makefile includes a convenient command that developers can utilize:

docker-build:
    docker build -t thanos:latest .

This command prompts Docker to build the image based on the current context and Dockerfile.

4. Testing the Docker Image

Running tests in the development environment requires consideration of build constraints present in certain files. A command to execute tests could look like this:

docker-test:
    docker run --rm thanos:latest go test -tags '!linux,!stringlabels' ./...

This command runs the tests while respecting the specified build constraints.

5. Continuous Integration with Docker

In the CI environment, the .circleci/config.yml configuration file demonstrates how Docker is leveraged to facilitate various build and push steps.

publish_main:
    executor: golang
    steps:
      - git-shallow-clone/checkout
      - go/mod-download-cached
      - setup_remote_docker:
          version: 20.10.12
      - attach_workspace:
          at: .
      - run: make docker-build
      - run: make docker-test
      - run: echo "${DOCKERHUB_PASSWORD}" | docker login -u="${DOCKERHUB_USERNAME}" --password-stdin
      - run: make docker-push DOCKER_IMAGE_REPO=thanosio/thanos

This setup includes critical steps for building and testing the image in a CI/CD pipeline, which integrates with Docker.

6. Development Containers

The .devcontainer/devcontainer.json file can be used to configure the development environment, ensuring all necessary tools and configurations are in place.

{
  "name": "Codespaces for Thanos",
  "build": {
    "context": "..",
    "dockerfile": "Dockerfile"
  },
  "workspaceFolder": "/go/src/github.com/thanos-io/thanos",
  "workspaceMount": "source=${localWorkspaceFolder},target=/go/src/github.com/thanos-io/thanos,type=bind,consistency=cached",
  "features": {
    "ghcr.io/devcontainers/features/docker-in-docker:2": {},
    "ghcr.io/devcontainers/features/node:1": {},
    "ghcr.io/devcontainers/features/kubectl-helm-minikube:1": {}
  },
  "onCreateCommand": "make build",
  "postAttachCommand": {
    "Run quickstart": "bash scripts/quickstart.sh"
  }
}

This configuration allows a streamlined setup of the development environment, enhancing developer productivity with the Thanos codebase.

Conclusion

The Thanos project integrates Docker within its development environment to streamline the building, testing, and package distribution processes. By utilizing well-defined Docker configurations and commands, developers can work efficiently in a consistent environment.

References

  • Source files: Dockerfile, Dockerfile.multi-stage, Makefile, and .circleci/config.yml.
  • Official Thanos documentation and internal configuration details.