This documentation serves as an in-depth guide for configuring Docker within a development environment using the stefanprodan/timoni
project. This guide assumes that the reader is already familiar with stefanprodan/timoni
and focuses on demonstrating the setup and use of Docker during development without delving into production configurations.
Directory Structure Overview
Before diving into Docker configuration, it is important to understand the role of various files in this project, particularly in relation to Docker.
- Makefile: Used to automate tasks and commands related to building and testing.
- cue.mod: Contains configurations for Kubernetes resources that may include Docker image references.
- test files: Used for setting up a local Docker registry for testing.
Docker Registry Setup for Testing
To facilitate a local development environment, you may need to set up a local Docker registry. This is particularly useful for testing your application against images stored in the registry.
The following code snippet demonstrates how to set up a local Docker registry in both cmd/timoni/main_test.go
and internal/oci/main_test.go
.
func setupRegistryServer(ctx context.Context) error {
config := &configuration.Configuration{}
config.Log.AccessLog.Disabled = true
config.Log.Level = "error"
port, err := freeport.GetFreePort()
if err != nil {
return fmt.Errorf("failed to get free port: %s", err)
}
logrus.SetOutput(io.Discard)
dockerRegistry = fmt.Sprintf("localhost:%d", port)
config.HTTP.Addr = fmt.Sprintf("127.0.0.1:%d", port)
config.HTTP.DrainTimeout = time.Duration(10) * time.Second
config.Storage = map[string]configuration.Parameters{"inmemory": map[string]interface{}{}}
dockerRegistry, err := registry.NewRegistry(ctx, config)
if err != nil {
return fmt.Errorf("failed to create docker registry: %w", err)
}
go dockerRegistry.ListenAndServe()
return nil
}
This function sets up a local Docker registry by defining configuration parameters, acquiring a free port, and then starting the registry.
Makefile Functions Related to Docker
The Makefile
in the stefanprodan/timoni
project automates various tasks. Here are relevant functions that might be involved in Docker configuration during development:
- build: Used to compile the Go code.
- test: Executes tests which may include Docker containerized tests.
- push-redis: Pushes a Redis image to the local or remote registry.
- install-envtest: Installs dependencies necessary for environment testing which often relates to Docker.
Example Usage
To build the project after changing your Docker configuration, run:
make build
To run tests that may involve Docker containers:
make test
You can push a Docker image, for example, to a local registry:
make push-redis
Cue Configuration for Docker Images
In the cue.mod
, configurations regarding Docker images can be defined. Below is a code snippet that demonstrates how to set the image used for Kubernetes pods.
image: timoniv1.#Image & {
repository: *"docker.io/nginx" | string
tag: *"1-alpine" | string
digest: *"" | string
}
This configuration allows setting the container image repository, tag, and optional digest for the images you are using.
Using Docker in Kubernetes Environments
When deploying your application in a Kubernetes environment, it is important to set appropriate image pull secrets. The following defines the required secrets in your deployment configuration:
pod: {
imagePullSecrets?: [...timoniv1.#ObjectReference]
}
This enables Kubernetes to authenticate against Docker registries when pulling images necessary for your application.
Conclusion
The Docker configuration within the stefanprodan/timoni
development environment lays a solid foundation for local testing and builds. Utilizing the provided setup for the Docker registry and Makefile functions, developers can effectively manage the required images and services necessary for their application during development.
For detailed implementation and examples of how these configurations align with the functionality in stefanprodan/timoni
, one should refer to the listed code snippets throughout this documentation. Further iterations on these setups can be customized based on specific development needs.
Source: stefanprodan/timoni.