Development Environment Setup

The following sections detail the configuration and setup for a Docker-based development environment using docker/cli. Follow the steps closely to ensure a functional local setup.

1. Defining docker-compose.yml

The docker-compose.yml file orchestrates your Docker services. Below is an example of a configuration suitable for a development environment.

version: "2"
services:
  server:
    build:
      context: .
      dockerfile: server.Dockerfile
    networks:
      - mdb
      - sig
    ports:
      - "8080"
      - "4443:4443"
    entrypoint: /usr/bin/env sh
    command: -c "./migrations/migrate.sh && notary-server -config=fixtures/server-config.json"
    depends_on:
      - mysql
      - signer

  signer:
    build:
      context: .
      dockerfile: signer.Dockerfile
    networks:
      mdb:
      sig:
        aliases:
          - notarysigner
    entrypoint: /usr/bin/env sh
    command: -c "./migrations/migrate.sh && notary-signer -config=fixtures/signer-config.json"
    depends_on:
      - mysql

  mysql:
    networks:
      - mdb
    volumes:
      - ./notarysql/mysql-initdb.d:/docker-entrypoint-initdb.d
      - notary_data:/var/lib/mysql
    image: mariadb:10.4
    environment:
      - TERM=dumb
      - MYSQL_ALLOW_EMPTY_PASSWORD="true"
    command: mysqld --innodb_file_per_table

volumes:
  notary_data:
    external: false

networks:
  mdb:
    external: false
  sig:
    external: false

This docker-compose.yml defines three services: server, signer, and mysql. Each service has its build context and Dockerfile. They are connected through dedicated networks and expose specific ports for local development.

2. Creating the Dockerfile

The next step is to create a Dockerfile for building the application images. Below is an outlined example based on the provided configurations.

# syntax=docker/dockerfile:1

ARG BASE_VARIANT=alpine
ARG ALPINE_VERSION=3.20
ARG BASE_DEBIAN_DISTRO=bookworm

ARG GO_VERSION=1.23.3
ARG XX_VERSION=1.5.0
ARG GOVERSIONINFO_VERSION=v1.4.1
ARG GOTESTSUM_VERSION=v1.10.0
ARG BUILDX_VERSION=0.18.0
ARG COMPOSE_VERSION=v2.30.3

FROM --platform=$BUILDPLATFORM tonistiigi/xx:${XX_VERSION} AS xx

FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine${ALPINE_VERSION} AS build-base-alpine
ENV GOTOOLCHAIN=local
COPY --link --from=xx / /
RUN apk add --no-cache bash clang lld llvm file git
WORKDIR /go/src/github.com/docker/cli

FROM build-base-alpine AS build-alpine
ARG TARGETPLATFORM
RUN xx-apk add --no-cache musl-dev gcc

FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-${BASE_DEBIAN_DISTRO} AS build-base-debian
ENV GOTOOLCHAIN=local
COPY --link --from=xx / /
RUN apt-get update && apt-get install --no-install-recommends -y bash clang lld llvm file
WORKDIR /go/src/github.com/docker/cli

FROM build-base-debian AS build-debian
ARG TARGETPLATFORM
RUN xx-apt-get install --no-install-recommends -y libc6-dev libgcc-12-dev pkgconf

FROM build-base-${BASE_VARIANT} AS build
ARG GO_LINKMODE=static
ARG GO_BUILDTAGS
ARG GO_STRIP
ARG CGO_ENABLED
ARG VERSION
COPY --link --from=goversioninfo /out/goversioninfo /usr/bin/goversioninfo
RUN --mount=type=bind,target=.,ro \
    --mount=type=cache,target=/root/.cache \
    --mount=type=tmpfs,target=cli/winresources \
    xx-go --wrap && \
    TARGET=/out ./scripts/build/binary && \
    xx-verify $([ "$GO_LINKMODE" = "static" ] && echo "--static") /out/docker

This Dockerfile implements a multi-stage build process, ensuring the necessary tools and dependencies are available without bloating the final image. The relevant build arguments like GO_VERSION and BASE_VARIANT are configured at the top.

3. Makefile Commands

The included Makefile can assist in automating various tasks such as testing, building, and linting:

Available functions in Makefile: yamldocs, mddocs, test-unit, help, validate-vendor, clean, binary, all, shell, dynbinary, shellcheck, vendor, authors, lint, dev, plugins, test, manpages, mod-outdated, test-coverage, fmt, completion, /etc/bash_completion.d/docker 

For example, to build the application, the following command can be used:

make binary

Utilizing the Makefile allows for a streamlined development workflow, reducing manual commands and potential errors.

4. Building and Running

After ensuring your docker-compose.yml, Dockerfiles, and Makefiles are correctly configured, you can build and run your entire development environment with:

docker-compose up --build

This command pulls the necessary images, builds the defined services, and starts the containers as defined in your Docker Compose file, enabling a complete local development setup.

5. Testing the Build

Once the containers are up, it is important to ensure everything works as expected. Run the tests using:

make test

This command executes the defined unit tests in the Makefile, verifying the integrity of the code within your Docker environment.

Conclusion

Utilizing the above configurations and commands allows expert developers to maintain an efficient Docker-based development environment. Follow the outlined steps precisely to ensure a sustainable setup and troubleshoot or optimize as needed for specific project requirements.

Source

  • Docker CLI Setup and Configuration Source: github.com/docker/cli/docs/generate (for Go module specific details).