Skip to content

Commit

Permalink
feat: add docker muti arch build script
Browse files Browse the repository at this point in the history
  • Loading branch information
rashed091 committed Jun 9, 2024
1 parent 9e247c1 commit a13ebc4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
8 changes: 1 addition & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ VERSION=$(shell git rev-parse HEAD)
PORT=$(shell rg --no-filename --color never 'PORT=*' .env | cut -d '=' -f 2)
DB_CONTAINER_NAME=$(shell rg --no-filename --color never 'DATABASE_CONTAINER_NAME=*' .env | cut -d '=' -f 2)


.PHONY: is_running

setup:
Expand Down Expand Up @@ -47,12 +46,7 @@ push: tag
docker push $(DOCKER_HUB_REPO)/$(APP_NAME):latest

multi:
docker buildx build \
--build-arg APP_NAME=$(APP_NAME) \
--build-arg PORT=$(PORT) \
--platform linux/amd64,linux/arm64 \
--output "type=image,push=true" \
--tag $(DOCKER_HUB_REPO)/$(APP_NAME):latest --push .
./build.sh

testrun:
docker container run --name $(APP_NAME) \
Expand Down
53 changes: 53 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/sh
#-------------------------------------docker multiple architecture build script------------------------------------------
#Note: you cannot export a buildx container image into a local docker instance with multiple architecture manifests so for local testing you have to select just a single architecture.

APP_NAME=$(rg --no-filename --color never 'name = .*' Cargo.toml | cut -d '"' -f 2)
PORT=$(rg --no-filename --color never 'PORT=*' .env | cut -d '=' -f 2)
DB_CONTAINER_NAME=$(rg --no-filename --color never 'DATABASE_CONTAINER_NAME=*' .env | cut -d '=' -f 2)


# Set variables to emulate running in the workflow/pipeline,
# this helps tracking docker image changes for each commit
GIT_REPOSITORY=$(basename `git rev-parse --show-toplevel`)
GIT_BRANCH=$(git branch --show-current)
GIT_COMMIT_SHA=$(git rev-parse HEAD)
GITHUB_WORKFLOW="n/a"
GITHUB_RUN_ID=0
GITHUB_RUN_NUMBER=0
GIT_TAG="latest"

DOCKER_HUB_REPO=mrasheduzzaman
IMAGE_NAME="$DOCKER_HUB_REPO/$APP_NAME:$GIT_TAG"

PLATFORMS="linux/amd64,linux/arm64"

#Create a new builder instance
#https://github.com/docker/buildx/blob/master/docs/reference/buildx_create.md
docker buildx create --name multiarchcontainerrust --use

#Start a build
#https://github.com/docker/buildx/blob/master/docs/reference/buildx_build.md
docker buildx build \
--tag $IMAGE_NAME \
--label "GITHUB_RUN_ID=$GITHUB_RUN_ID" \
--label "IMAGE_NAME=$IMAGE_NAME" \
--build-arg APP_NAME=$(APP_NAME) \
--build-arg PORT=$(PORT) \
--build-arg GIT_REPOSITORY=$GIT_REPOSITORY \
--build-arg GIT_BRANCH=$GIT_BRANCH \
--build-arg GIT_COMMIT=$GIT_COMMIT_SHA \
--build-arg GIT_TAG=$GIT_TAG \
--build-arg GITHUB_WORKFLOW=$GITHUB_WORKFLOW \
--build-arg GITHUB_RUN_ID=$GITHUB_RUN_ID \
--build-arg GITHUB_RUN_NUMBER=$GITHUB_RUN_NUMBER \
--platform $PLATFORMS \
--push \
--output type=docker \
.

#Preview matching images
#https://docs.docker.com/engine/reference/commandline/images/
docker images $GIT_REPOSITORY

docker buildx stop multiarchcontainerrust

0 comments on commit a13ebc4

Please sign in to comment.