This documentation provides a step-by-step guide for configuring and using Docker within the development environment of the Slim Toolkit (github.com/slimtoolkit/slim). This guide is aimed at expert developers familiar with Docker and Go.

Dockerfile Overview

The Dockerfile is a key component of the Slim development process. It is responsible for setting up the environment in which the Go application is built and tested. Below is a breakdown of the Dockerfile used in the Slim toolkit.

WORKDIR /go/src

# Set property for golangci-lint version
ARG GOLANGCILINT_VERSION=v1.24.0

# Install golangci-lint
RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin ${GOLANGCILINT_VERSION}

# Install ltag tool
RUN go get -v -u github.com/kunalkushwaha/ltag && rm -rf /go/src/github.com/kunalkushwaha

# Copy project files
COPY . .

Breakdown of the Dockerfile Steps

  1. Setting the Working Directory
    The WORKDIR /go/src command sets the working directory. All subsequent commands will be executed in this directory.

  2. Building Arguments
    The ARG GOLANGCILINT_VERSION line defines a build argument to specify the version of golangci-lint that will be installed.

  3. Installing golangci-lint
    The RUN directive uses curl to fetch the installation script for golangci-lint and executes it. This linter is helpful for maintaining code quality within the project.

  4. Installing ltag Tool
    Another RUN directive installs the ltag tool, which may be used for managing version tags in Go applications. The unnecessary files are removed afterward to keep the image clean.

  5. Copying Project Files
    The COPY . . command copies all files from the current directory into the working directory of the Docker image. This is essential for building the project.

Makefile Functions

The Makefile provides several targets to simplify the development workflow. Here are key commands relevant to Docker:

  • build_in_docker
    This target allows you to build the Go application within a Docker container. You can define it in your Makefile as follows:

    build_in_docker:
        docker build -t slim-toolkit .
    
  • build_dev
    This target can be utilized for building the application for development purposes. Add this target to your Makefile:

    build_dev:
        go build -o slim-toolkit github.com/slimtoolkit/slim
    
  • test
    For running tests, including those with the “e2e” build constraint defined, you might structure your Makefile like so:

    test:
        go test -tags=e2e ./...
    

Example Usage

To effectively leverage Docker within your development process with the Slim Toolkit, follow these steps:

  1. Build the Docker Image
    Use the build_in_docker target from your Makefile.

    make build_in_docker
    
  2. Run Tests in Docker
    Depending on how your tests are structured and if you are utilizing the defined “e2e” constraint, execute:

    make test
    
  3. Cleanup
    Maintain a clean environment by using the clean target, which you can define as:

    clean:
        rm -rf slim-toolkit
    

Conclusion

Docker configuration in the Slim Toolkit development environment streamlines the setup and testing of the application. By employing the Dockerfile and Makefile effectively, expert developers can maintain an efficient development process.

References

  • Source: Dockerfile and Makefile of Slim Toolkit (github.com/slimtoolkit/slim).