Skip to content

Commit

Permalink
Refactor Docker image (#50)
Browse files Browse the repository at this point in the history
* Refactor Docker image

* Update Makefile to be used by Dockerfile

* Update Dockerfile to use Makefile

* Minor update to re-trigger GHAs
  • Loading branch information
gab-arrobo authored Mar 4, 2024
1 parent 2b59a12 commit 6ee0652
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 19 deletions.
32 changes: 21 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
# Copyright 2019-present Open Networking Foundation
# Copyright 2024-present Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#

FROM golang:1.22.0-bookworm AS sim
FROM golang:1.22.0-bookworm AS builder

LABEL maintainer="ONF <omec-dev@opennetworking.org>"

RUN apt-get update && apt-get -y install vim
RUN cd $GOPATH/src && mkdir -p simapp
COPY . $GOPATH/src/simapp
RUN cd $GOPATH/src/simapp && CGO_ENABLED=0 go install
RUN apt-get update && \
apt-get -y install --no-install-recommends \
vim && \
apt-get clean

WORKDIR $GOPATH/src/simapp
COPY . .
RUN make all

FROM alpine:3.19 AS simapp

#RUN apk update && apk add -U libc6-compat vim strace net-tools curl netcat-openbsd bind-tools bash
RUN apk update && apk add -U gcompat vim strace net-tools curl netcat-openbsd bind-tools bash
LABEL description="Aether open source 5G Core Network" \
version="Stage 3"

ARG DEBUG_TOOLS

# Install debug tools ~ 50MB (if DEBUG_TOOLS is set to true)
RUN if [ "$DEBUG_TOOLS" = "true" ]; then \
apk update && apk add --no-cache -U gcompat vim strace net-tools curl netcat-openbsd bind-tools bash; \
fi

WORKDIR /simapp
RUN mkdir -p /simapp/bin
WORKDIR /simapp/bin

# Copy executable
COPY --from=sim /go/bin/* /simapp/bin/
WORKDIR /simapp
COPY --from=builder /go/src/simapp/bin/* .
75 changes: 67 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,46 +1,105 @@
# Copyright 2019-present Open Networking Foundation
# Copyright 2024-present Intel Corporation
#
# SPDX-License-Identifier: Apache-2.0
#

PROJECT_NAME := simapp
VERSION ?= $(shell cat ./VERSION)
DOCKER_VERSION ?= $(shell cat ./VERSION)

## Docker related
DOCKER_REGISTRY ?=
DOCKER_REPOSITORY ?=
DOCKER_TAG ?= ${VERSION}
DOCKER_TAG ?= ${DOCKER_VERSION}
DOCKER_IMAGENAME := ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}${PROJECT_NAME}:${DOCKER_TAG}
DOCKER_BUILDKIT ?= 1
DOCKER_BUILD_ARGS ?=

## Docker labels. Only set ref and commit date if committed
#DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
#DOCKER_LABEL_VCS_REF ?= $(shell git diff-index --quiet HEAD -- && git rev-parse HEAD || echo "unknown")
#DOCKER_LABEL_COMMIT_DATE ?= $(shell git diff-index --quiet HEAD -- && git show -s --format=%cd --date=iso-strict HEAD || echo "unknown" )
DOCKER_LABEL_VCS_URL ?= $(shell git remote get-url $(shell git remote))
DOCKER_LABEL_VCS_REF ?= $(shell git diff-index --quiet HEAD -- && git rev-parse HEAD || echo "unknown")
DOCKER_LABEL_COMMIT_DATE ?= $(shell git diff-index --quiet HEAD -- && git show -s --format=%cd --date=iso-strict HEAD || echo "unknown" )
DOCKER_LABEL_BUILD_DATE ?= $(shell date -u "+%Y-%m-%dT%H:%M:%SZ")

DOCKER_TARGETS ?= simapp

# https://docs.docker.com/engine/reference/commandline/build/#specifying-target-build-stage---target
GO_BIN_PATH = bin
GO_SRC_PATH = ./
C_BUILD_PATH = build
ROOT_PATH = $(shell pwd)

NF = $(GO_NF)
GO_NF = simapp

NF_GO_FILES = $(shell find $(GO_SRC_PATH)/$(%) -name "*.go" ! -name "*_test.go")

VERSION = $(shell git describe --tags)
BUILD_TIME = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
COMMIT_HASH = $(shell git submodule status | grep $(GO_SRC_PATH)/$(@F) | awk '{print $$(1)}' | cut -c1-8)
COMMIT_TIME = $(shell cd $(GO_SRC_PATH) && git log --pretty="%ai" -1 | awk '{time=$$(1)"T"$$(2)"Z"; print time}')

.PHONY: $(NF) clean docker-build docker-push

.DEFAULT_GOAL: nfs

nfs: $(NF)

all: $(NF)

$(GO_NF): % : $(GO_BIN_PATH)/%

$(GO_BIN_PATH)/%: %.go $(NF_GO_FILES)
# $(@F): The file-within-directory part of the file name of the target.
@echo "Start building $(@F)...."
cd $(GO_SRC_PATH)/ && \
CGO_ENABLED=0 go build -o $(ROOT_PATH)/$@ $(@F).go

vpath %.go $(addprefix $(GO_SRC_PATH)/, $(GO_NF))

clean:
rm -rf $(addprefix $(GO_BIN_PATH)/, $(GO_NF))
rm -rf $(addprefix $(GO_SRC_PATH)/, $(addsuffix /$(C_BUILD_PATH), $(C_NF)))

docker-build:
@go mod vendor
for target in $(DOCKER_TARGETS); do \
DOCKER_BUILDKIT=$(DOCKER_BUILDKIT) docker build $(DOCKER_BUILD_ARGS) \
--target $$target \
--tag ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}$$target:${DOCKER_TAG} \
--build-arg org_label_schema_version="${VERSION}" \
--build-arg org_label_schema_version="${DOCKER_VERSION}" \
--build-arg org_label_schema_vcs_url="${DOCKER_LABEL_VCS_URL}" \
--build-arg org_label_schema_vcs_ref="${DOCKER_LABEL_VCS_REF}" \
--build-arg org_label_schema_build_date="${DOCKER_LABEL_BUILD_DATE}" \
--build-arg org_opencord_vcs_commit_date="${DOCKER_LABEL_COMMIT_DATE}" \
. \
|| exit 1; \
done
rm -rf vendor

docker-push:
for target in $(DOCKER_TARGETS); do \
docker push ${DOCKER_REGISTRY}${DOCKER_REPOSITORY}$$target:${DOCKER_TAG}; \
done

.coverage:
rm -rf $(CURDIR)/.coverage
mkdir -p $(CURDIR)/.coverage

test: .coverage
docker run --rm -v $(CURDIR):/simapp -w /simapp golang:latest \
go test \
-race \
-failfast \
-coverprofile=.coverage/coverage-unit.txt \
-covermode=atomic \
-v \
./ ./...

fmt:
@go fmt ./...

golint:
@docker run --rm -v $(CURDIR):/app -w /app golangci/golangci-lint:latest golangci-lint run -v --config /app/.golangci.yml

.PHONY: docker-build docker-push
check-reuse:
@docker run --rm -v $(CURDIR):/simapp -w /simapp omecproject/reuse-verify:latest reuse lint

0 comments on commit 6ee0652

Please sign in to comment.