Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker image hardening and one dockerfile for all services #215

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

marcus-sva
Copy link
Contributor

@marcus-sva marcus-sva commented Jan 20, 2025

What this PR does / why we need it:

Hardening docker image build by

  • using build stages with two stages
    • build stage to build go binary
    • runtime stage will run built go binary as dedicated user
  • not using latest images
  • not running build go binary as root

Other changes

  • adapt Makefile
  • unified Dockerfile for services
  • smaller go binary in ci/cd via -ldflags="-s -w" causing smaller docker images
  • use buildkit for multi-platform docker builds
  • github workflow for pr which builds go binary using go build
  • github workflow for release which does docker multi-platform builds

Makefile

:> make help

Usage:
  make docker-setup
  make docker-build [SERVICES=service1 service2] [IMAGE_TAG=tag] [IMAGE_REGISTRY=registry] [PLATFORMS=os/arch,os/arch] [PUSH=true]
  make docker-push [SERVICES=service1 service2] [IMAGE_TAG=tag] [IMAGE_REGISTRY=registry] [PLATFORMS=os/arch,os/arch]
  make generate-client
  make generate-protos
  make list-services
  make help

Targets:
  docker-setup       Sets up and configures Docker Buildx for the first time
  docker-build       Build the docker images with multi-platform support
  docker-push        Build and push the docker images with multi-platform support
  generate-client    Generate kubernetes glue code for go
  generate-protos    Generate go code from proto files
  list-services      Prints the available services for building
  help               Prints help

Options:
  SERVICES           List of services to build (default: all detected)
  IMAGE_TAG          Tag for the built images (default: latest)
  IMAGE_REGISTRY     Registry for images (default: hobbyfarm)
  PLATFORMS          Comma-separated list of target platforms (default: linux/amd64)
  PUSH               If set to true, push images to the registry (default: false)

Examples

  • to setup docker buildx for the first time run make docker-setup
  • build the images like:
make docker-build SERVICES="costsvc vmsetsvc" PLATFORMS="linux/amd64,linux/arm64" IMAGE_REGISTRY="my.registry/test" IMAGE_TAG="v1.2.3"
  • build and push the images like:
make docker-push SERVICES="costsvc vmsetsvc" PLATFORMS="linux/amd64,linux/arm64" IMAGE_REGISTRY="my.registry/test" IMAGE_TAG="v1.2.3"

Build service docker image

cd $ROOT_OF_PROJECT
docker build --build-arg SERVICE_NAME=costsvc -f v3/Dockerfile -t costsvc:latest .
docker build --build-arg SERVICE_NAME=usersvc -f v3/Dockerfile -t usersvc:latest .
docker build --build-arg SERVICE_NAME=sessionsvc -f v3/Dockerfile -t sessionsvc:latest .
...

Build gargantua

cd $ROOT_OF_PROJECT
docker build -f Dockerfile -t gargantua:latest .

TODO

  • is docker-compose.yaml can be removed
  • adapt ci/cd pipeline
  • add github variable for DOCKER_REGISTRY with value hobbyfarm

Which issue(s) this PR fixes:

Fixes hobbyfarm/hobbyfarm#487

@marcus-sva marcus-sva force-pushed the docker-hardening branch 3 times, most recently from e68cb8e to 1e14633 Compare January 21, 2025 12:44
@marcus-sva marcus-sva force-pushed the docker-hardening branch 3 times, most recently from 3a2d901 to 13a8cd8 Compare January 31, 2025 14:37
@marcus-sva marcus-sva force-pushed the docker-hardening branch 4 times, most recently from 848b300 to 79e636d Compare February 4, 2025 16:02
@marcus-sva marcus-sva force-pushed the docker-hardening branch 2 times, most recently from 2d8b937 to 5c4a56f Compare February 6, 2025 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Build containers should follow docker best practice
1 participant