docker-compose.yml

This configuration manages multiple services necessary for local development.

version: "3"

services:
  db:
    build: .
    command: ./entrypoint.sh
    image: docker.io/tonistiigi/db
  webapp:
    build:
      context: .
      dockerfile: Dockerfile.webapp
      args:
        buildno: 1

Service Breakdown

  • db: This service builds from the current directory (.) and runs a custom entrypoint script. The image is specified directly to pull from Docker Hub.

  • webapp: This service defines a build context and specifies a Dockerfile (Dockerfile.webapp). A build argument buildno is passed to the build process.

Dockerfile

This file outlines the steps to build the application in a multi-stage manner, leveraging different base images for various tasks.

# syntax=docker/dockerfile:1

ARG GO_VERSION=1.23
ARG XX_VERSION=1.5.0
# other ARG declarations...

FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS golatest
# additional FROM declarations...

FROM golatest AS gobase
COPY --from=xx / /
RUN apk add --no-cache file git
ENV GOFLAGS=-mod=vendor
ENV CGO_ENABLED=0
WORKDIR /src

Key Details

  • Multi-stage Builds: The Dockerfile utilizes multiple stages (FROM clauses) to create a streamlined build environment. Each stage has its intended purpose with specific dependencies.

  • Environment Variables:

    • GOFLAGS=-mod=vendor disables vendoring, ensuring a cleaner build process.
    • CGO_ENABLED=0 instructs Go to avoid cgo, making the final build more portable.

Building with Buildx

The Dockerfile showcases the integration with Buildx for enhanced build capabilities.

FROM gobase AS buildx-version
RUN --mount=type=bind,target=. <<EOT
  set -e
  mkdir /buildx-version
  echo -n "$(./hack/git-meta version)" | tee /buildx-version/version
  echo -n "$(./hack/git-meta revision)" | tee /buildx-version/revision
EOT

Testing

Testing is incorporated into the build flow using dedicated stages.

FROM gobase AS test
ENV SKIP_INTEGRATION_TESTS=1
RUN --mount=type=bind,target=. \
  --mount=type=cache,target=/root/.cache \
  --mount=type=cache,target=/go/pkg/mod \
  go test -v -coverprofile=/tmp/coverage.txt -covermode=atomic ./... && \
  go tool cover -func=/tmp/coverage.txt

Coverage Reporting

This step ensures that the test coverage is calculated, and can be exported for analysis.

Makefile

A Makefile is provided to streamline development tasks, abstracting common commands.

Available functions in Makefile: validate-all, authors, install, docs, mod-outdated, generated-files, test, test-unit, test-driver, release, build, test-integration, vendor, all, shell

Common Operations

  • build: Executes the Docker build process.

  • test: Runs unit tests leveraging the defined stages in the Dockerfile.

  • install: Facilitates the installation of necessary dependencies within the Docker environment.

This configuration maintains a focus on the development lifecycle, ensuring a conducive environment for expert developers looking to leverage Docker’s capabilities without venturing into production specifics.

Information sourced from the provided Dockerfile and docker-compose.yml files.