Skip to content

Commit

Permalink
Clean up redundant goals in Makefile, add annotations to docker-bake.…
Browse files Browse the repository at this point in the history
…hcl (#308)
  • Loading branch information
dovvla authored Jan 24, 2025
1 parent eb97959 commit c915b1b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 45 deletions.
36 changes: 5 additions & 31 deletions docker/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ IMAGE=ghcr.io/memristor/mep3

.PHONY: all

all: destroy run setup-default
all: destroy run

colors:
$(eval NC=\033[1;0m)
Expand All @@ -35,32 +35,17 @@ multiple:
$(eval FLAVOR=multiple)
true

## Test if the provided command exists
exists/cmd/%:
@hash $(*) > /dev/null 2>&1 || (echo "ERROR: '$(*)' must be installed"; exit 1)

## Test if the provided environment variable exists
exists/env/%:
@if [ -z '$($(*))' ]; then echo "ERROR: environment variable '$*' not set" && exit 1; fi


# Docker since version 23.0 has been including buildx by default, current live version is 27.2.1 as of 2024-09-16
build: | exists/cmd/docker colors
if [ -n "${BUILDX_INSTALLED}" ]; then \
[ ${FLAVOR} == 'multiple' ] && docker buildx bake && exit 0; \
[ ${FLAVOR} == 'base' ] && docker buildx bake mep3 && exit 0; \
docker buildx bake mep3-${FLAVOR} && exit 0; \
else \
printf '%b\n' "${RED}Docker buildx is not present, it is highly recommended to install newer version of docker\n${NC}" || \
[ ${FLAVOR} = 'multiple' ] && \
DOCKER_BUILDKIT=1 docker build ${DOCKER_DIR} -f ${DOCKER_DIR}/Dockerfile.base -t mep3 ${DOCKER_ARGS} --build-arg UID=${UID} && \
DOCKER_BUILDKIT=1 docker build ${DOCKER_DIR} -f ${DOCKER_DIR}/Dockerfile.vnc -t mep3-vnc ${DOCKER_ARGS} && \
DOCKER_BUILDKIT=1 docker build ${DOCKER_DIR} -f ${DOCKER_DIR}/Dockerfile.deploy -t mep3-deploy ${DOCKER_ARGS} && exit 0; \
[ ${FLAVOR} = 'base' ] && \
DOCKER_BUILDKIT=1 docker build ${DOCKER_DIR} -f ${DOCKER_DIR}/Dockerfile.${FLAVOR} -t mep3 ${DOCKER_ARGS} --build-arg UID=${UID} && exit 0; \
[ ${FLAVOR} != 'base' ] && \
DOCKER_BUILDKIT=1 docker build ${DOCKER_DIR} -f ${DOCKER_DIR}/Dockerfile.${FLAVOR} -t mep3-${FLAVOR} ${DOCKER_ARGS} && exit 0; \
fi
[ ${FLAVOR} == 'multiple' ] && docker buildx bake && exit 0; \
[ ${FLAVOR} == 'base' ] && docker buildx bake mep3 && exit 0; \
docker buildx bake mep3-${FLAVOR} && exit 0;

run: | exists/cmd/docker test-nvidia
docker run \
Expand All @@ -78,6 +63,7 @@ run: | exists/cmd/docker test-nvidia
-v /dev:/dev:rw \
-v ${PROJECT_DIR}:/memristor/ros2_ws/src/mep3:rw \
-d -it ${IMAGE} 1>/dev/null
echo "Container started, you can run 'make exec' to exec into the container and continue your workflow."

test-nvidia: | exists/cmd/docker
lspci | grep -qi nvidia && base64 --decode massage | unxz || true
Expand All @@ -88,25 +74,13 @@ test-nvidia: | exists/cmd/docker
printf '%b\n' "${RED}Detected NVIDIA GPU in system, but missing packets, look up NVIDIA GPU section in README!\n${NC}" || \
true

start-code-server: | exists/cmd/docker
docker exec -d -it mep3-${FLAVOR} bash -c 'pgrep code-server || code-server /memristor/ros2_ws/src/mep3' && \
xdg-open 'localhost:31415?folder=/memristor/ros2_ws/src/mep3'

stop-code-server: | exists/cmd/docker
docker exec -it mep3-${FLAVOR} pkill -f code-server

exec: | exists/cmd/docker
docker exec -it mep3-${FLAVOR} bash

destroy: | exists/cmd/docker
docker container kill mep3-${FLAVOR} 1>/dev/null || true
docker container rm -f mep3-${FLAVOR} 1>/dev/null || true

setup-default: | exists/cmd/docker colors
docker exec -it mep3-${FLAVOR} sh -c '/usr/bin/setup.sh --default'
printf '%b\n%b\n' "${GREEN}Default setup complete!${NC}" \
"Run ${BOLD}make exec${NC} or ${BOLD}docker exec -it mep3-${FLAVOR}${NC} to access the container"

setup-interactive: | exists/cmd/docker colors
docker exec -it mep3-${FLAVOR} sh -c '/usr/bin/setup.sh --interactive'
printf '%b\n%b\n' "${GREEN}Interactive setup complete!${NC}" \
Expand Down
16 changes: 2 additions & 14 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
cd ./mep3/docker
make all
```
If you want to build image localy you can use command:
If you want to build the image locally, you can use the command:
```sh
make build destroy run exec IMAGE=mep3
```
Expand All @@ -42,22 +42,10 @@

6. Acces the environment from any terminal window
```sh
docker exec -it mep3-devel bash
docker exec -it mep3-${FLAVOR} bash
```
Graphical applications started inside this terminal will use your existing Xorg session to display.

## Code Server

If you prefer to use browser based VS Code, you can start it in the container and then access it locally through your browser at `localhost:31415`

```sh
# This will start the VS code server with your mep3 repo
make start-code-server

# To stop the VS code server
make stop-code-server
```

## Remote development environment (VNC)

1. Follow steps in [Local development environment](#local-development-environment), but add `vnc` after
Expand Down
8 changes: 8 additions & 0 deletions docker/docker-bake.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ target "default" {

# Contexts for dependent images, as defined in CONTEXTS_MAPPING graph, i.e. mep3-vnc builds atop mep3
contexts = lookup(CONTEXTS_MAPPING, tgt, {})

annotations = [
format("org.opencontainers.image.revision=%s", notequal("", COMMIT_SHA) ? COMMIT_SHA : ""),
format("org.opencontainers.image.source=%s", notequal("", GITHUB_REPO) ? GITHUB_REPO : ""),
format("org.opencontainers.image.created=%s", timestamp()),
format("org.opencontainers.image.version=%s", notequal("", COMMIT_SHA) ? COMMIT_SHA : ""),
"org.opencontainers.image.authors=memristor <[email protected]>"
]
}

0 comments on commit c915b1b

Please sign in to comment.