Skip to content

Commit

Permalink
Dockerize original template
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom-Notch committed Sep 4, 2024
1 parent ba0938b commit 41138a4
Show file tree
Hide file tree
Showing 17 changed files with 2,028 additions and 3 deletions.
16 changes: 16 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# .env
XSOCK=/tmp/.X11-unix
XAUTH=/tmp/.docker.xauth
AVAILABLE_CORES=$(($(nproc) - 1))

DOCKER_USER=todo-docker-user
IMAGE_NAME=todo-image-name
IMAGE_TAG=latest

CONTAINER_NAME=${IMAGE_NAME}
CONTAINER_HOME_FOLDER=/root
CODE_FOLDER=${IMAGE_NAME}

HOST_UID=$(id -u)
HOST_GID=$(id -g)
HOSTNAME=${HOSTNAME}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ celerybeat.pid
*.sage.py

# Environments
.env
# .env
.venv
env/
venv/
Expand Down
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# New Repository
# New Docker Repository

[![pre-commit](https://github.com/Tom-Notch/New-Repository/actions/workflows/pre-commit.yml/badge.svg)](https://github.com/Tom-Notch/New-Repository/actions/workflows/pre-commit.yml)
[![pre-commit](https://github.com/Tom-Notch/New-Docker-Repository-Template/actions/workflows/pre-commit.yml/badge.svg)](https://github.com/Tom-Notch/New-Docker-Repository-Template/actions/workflows/pre-commit.yml)

## Dependencies

- [Docker](https://docs.docker.com/get-docker/)

## Usage Guidelines

Expand All @@ -10,6 +14,20 @@

1. Modify/add GitHub workflow status badges in [README.md](README.md)

1. Modify **DOCKER_USER**, **IMAGE_NAME** in [.env](.env)

1. Modify the service name from **default** to your service name in [docker-compose.yml](docker-compose.yml)

1. Update [Dockerfile](docker/latest/Dockerfile)

1. [build.sh](scripts/build.sh) to build and test the image locally in your machine's architecture

1. [push.sh](scripts/push.sh) to push the multi-arch image to the registry

## Developer Quick Start

- Run [scripts/dev-setup.sh](scripts/dev-setup.sh) to setup the development environment

## Note

- This template currently only supports docker image for amd64 and arm64, if you want to support other architectures, please modify the [build.sh](scripts/build.sh) script and [docker-compose.yml](docker-compose.yml) accordingly
41 changes: 41 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
version: "3"

services:
default:
build:
dockerfile: ./docker/${IMAGE_TAG}/Dockerfile
x-bake:
contexts:
home-folder-config: ./docker/build-context/home-folder-config
platforms:
- linux/amd64 # for PC
- linux/arm64 # for arm64 platforms
privileged: true
tags:
- ${DOCKER_USER}/${IMAGE_NAME}:${IMAGE_TAG}
image: ${DOCKER_USER}/${IMAGE_NAME}:${IMAGE_TAG}
pull_policy: missing
container_name: ${CONTAINER_NAME}
privileged: true
hostname: ${HOSTNAME}
network_mode: host
ipc: host
pid: host
ulimits:
core: -1
group_add:
- video
- audio
environment: # -e
DISPLAY: ${DISPLAY}
QT_X11_NO_MITSHM: 1
XAUTHORITY: ${XAUTH}
volumes: # -v
- /var/lib/systemd/coredump/:/cores
- /tmp/.X11-unix/:/tmp/.X11-unix/:rw
- ${XAUTH}:${XAUTH}
- .:${CONTAINER_HOME_FOLDER}/${CODE_FOLDER}
working_dir: ${CONTAINER_HOME_FOLDER}/${CODE_FOLDER}
stdin_open: true # for -it
tty: true # for -it
# command: /bin/zsh
1,660 changes: 1,660 additions & 0 deletions docker/build-context/home-folder-config/.p10k.zsh

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docker/build-context/home-folder-config/.screenrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
termcapinfo xterm* ti@:te@
1 change: 1 addition & 0 deletions docker/build-context/home-folder-config/.tmux.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set -g mouse on
18 changes: 18 additions & 0 deletions docker/build-context/home-folder-config/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh
set -e

# Check if the HOST_UID and HOST_GID are specified
if [ -n "${HOST_UID+x}" ] && [ -n "${HOST_GID+x}" ]; then
# Change 'myuser' UID and GID to match the host user's UID and GID
sudo usermod -u "$HOST_UID" "$(whoami)"
sudo groupmod -g "$HOST_GID" "$(whoami)"
# Ensure ownership of home directory is reset to 'myuser' after UID and GID change
chown -R "$(whoami)":"$(whoami)" /home/"$(whoami)"
fi

# Switch to 'myuser'
su -c "$(whoami)"

# Run the main command with exec to replace the current process (bash) with the given command
# This is important because of how signals are handled (https://hynek.me/articles/docker-signals/)
exec "$@"
8 changes: 8 additions & 0 deletions docker/build-context/home-folder-config/fixuid-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
user: camera-driver
group: camera-driver
paths:
- /home/camera-driver/.oh-my-zsh
- /home/camera-driver/.zshrc
- /home/camera-driver/.bashrc
- /tmp
82 changes: 82 additions & 0 deletions docker/latest/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Do not add --platform=linux/blabla since this is intended for multiplatform builds
FROM tomnotch/bipvrobotics-base-image:latest
ENV HOME_FOLDER=/root
WORKDIR $HOME_FOLDER/

# Fix apt install stuck problem
ENV DEBIAN_FRONTEND=noninteractive
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections

# update all obsolete packages to latest, install sudo, and cleanup
RUN apt update -o Acquire::Check-Valid-Until=false -o Acquire::AllowInsecureRepositories=true -o Acquire::AllowDowngradeToInsecureRepositories=true && \
apt full-upgrade -y && \
apt install -y sudo ca-certificates && \
apt autoremove -y && \
apt autoclean -y

# # Add a new group and user
# RUN addgroup --gid 1000 $USER && \
# adduser --uid 1000 --ingroup $USER --home $HOME_FOLDER --shell /bin/zsh --disabled-password --gecos "" $USER && \
# echo "$USER ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers

# # Fix UID/GID when mounting from host using this: https://github.com/boxboat/fixuid
# RUN curl -SsL https://github.com/boxboat/fixuid/releases/download/v0.6.0/fixuid-0.6.0-linux-amd64.tar.gz | tar -C /usr/local/bin -xzf - && \
# chown root:root /usr/local/bin/fixuid && \
# chmod 4755 /usr/local/bin/fixuid && \
# mkdir -p /etc/fixuid

# # Switch to the new user
# USER $USER:$USER

#! Install OpenCV 4.2.0 with QUIRC support from source
ENV OPENCV_VERSION=4.2.0
RUN pip3 uninstall -y opencv && \
apt install -y --no-install-recommends libavcodec-dev libavformat-dev libswscale-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libgtk-3-dev libpng-dev libjpeg-dev && \
git clone --depth 1 --recursive https://github.com/opencv/opencv.git $HOME_FOLDER/opencv -b $OPENCV_VERSION && \
# follow the error information,replace all the “ipcp-unit-growth” with “ipa-cp-unit-growth” in 3rdparty/carotene/CMakeLists.txt and 3rdparty/carotene/hal/CMakeLists.txt
perl -pi -e 's/ipcp-unit-growth/ipa-cp-unit-growth/g' $HOME_FOLDER/opencv/3rdparty/carotene/CMakeLists.txt $HOME_FOLDER/opencv/3rdparty/carotene/hal/CMakeLists.txt && \
git clone --depth 1 --recursive https://github.com/opencv/opencv_contrib.git $HOME_FOLDER/opencv_contrib -b $OPENCV_VERSION && \
mkdir -p $HOME_FOLDER/opencv/build && \
cd $HOME_FOLDER/opencv/build && \
cmake \
-D CMAKE_CXX_STANDARD=20 \
-D EIGEN_INCLUDE_PATH=/usr/include/eigen3 \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D BUILD_opencv_python3=ON \
-D OPENCV_PYTHON3_INSTALL_PATH=/usr/local/lib/python3.8/dist-packages \
# -D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
# -D WITH_LAPACK=ON \
-D WITH_GTK=ON \
-D WITH_TBB=ON \
# -D WITH_QUIRC=ON \
-D WITH_GSTREAMER=ON \
-D WITH_V4L=ON \
# -D WITH_OPENGL=ON \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_EXAMPLES=OFF \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local .. && \
make install -j$(($(nproc)-1)) && \
echo "export OpenCV_DIR=/usr/local/lib/cmake/opencv4/" >> ${HOME_FOLDER}/.zshrc && \
echo "export OpenCV_DIR=/usr/local/lib/cmake/opencv4/" >> ${HOME_FOLDER}/.bashrc && \
rm -rf $HOME_FOLDER/opencv && \
rm -rf $HOME_FOLDER/opencv_contrib

# end of sudo apt installs
RUN apt full-upgrade -y && \
apt autoremove -y && \
apt autoclean -y && \
apt clean -y && \
rm -rf /var/lib/apt/lists/*

# Set the default shell to zsh
SHELL [ "/bin/zsh", "-c" ]

# # move fixuid config
# RUN mv $HOME_FOLDER/fixuid-config.yml /etc/fixuid/config.yml

# Entrypoint command
# ENTRYPOINT [ "/bin/sh" , "-c", "fixuid; /bin/zsh" ]
ENTRYPOINT [ "/bin/zsh" ]
38 changes: 38 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash
#
# Created on Wed Sep 04 2024 18:04:23
# Author: Mukai (Tom Notch) Yu
# Email: [email protected]
# Affiliation: Carnegie Mellon University, Robotics Institute
#
# Copyright Ⓒ 2024 Mukai (Tom Notch) Yu
#

. "$(dirname "$0")"/variables.sh

# Determine the local platform
if [[ "$(uname -m)" == "x86_64" ]]; then
LOCAL_PLATFORM="linux/amd64"
elif [[ "$(uname -m)" == "arm64" ]]; then
LOCAL_PLATFORM="linux/arm64"
else
echo "Unsupported local platform: $(uname -m)"
exit 1
fi

# Desired builder name
BUILDER_NAME="multi-platform"

# Check if the builder already exists
if ! docker buildx ls | grep -q "^${BUILDER_NAME} "; then
echo "Creating Docker Buildx builder '${BUILDER_NAME}'..."
docker buildx create \
--name "${BUILDER_NAME}" \
--use --platform linux/amd64,linux/arm64 \
--driver docker-container
else
echo "Docker Buildx builder '${BUILDER_NAME}' already exists."
fi

# Build the Docker image for only the local platform, push.sh will push multi-platform version
docker buildx bake --file "$(dirname "$0")"/../docker-compose.yml --load --set "*.platform=$LOCAL_PLATFORM"
15 changes: 15 additions & 0 deletions scripts/kill.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
#
# Created on Wed Sep 04 2024 18:04:42
# Author: Mukai (Tom Notch) Yu
# Email: [email protected]
# Affiliation: Carnegie Mellon University, Robotics Institute
#
# Copyright Ⓒ 2024 Mukai (Tom Notch) Yu
#

. "$(dirname "$0")"/variables.sh

docker exec --privileged -it "$CONTAINER_NAME" pkill -f zsh

docker rm -f "$CONTAINER_NAME"
20 changes: 20 additions & 0 deletions scripts/kill_all_container.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
#
# Created on Wed Sep 04 2024 18:04:36
# Author: Mukai (Tom Notch) Yu
# Email: [email protected]
# Affiliation: Carnegie Mellon University, Robotics Institute
#
# Copyright Ⓒ 2024 Mukai (Tom Notch) Yu
#

. "$(dirname "$0")"/variables.sh

echo "Removing all containers"

for id in $(docker ps -aq); do
echo "Removing container $id"
docker rm -f "$id"
done

echo "Done"
13 changes: 13 additions & 0 deletions scripts/pull.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
#
# Created on Wed Sep 04 2024 18:04:49
# Author: Mukai (Tom Notch) Yu
# Email: [email protected]
# Affiliation: Carnegie Mellon University, Robotics Institute
#
# Copyright Ⓒ 2024 Mukai (Tom Notch) Yu
#

. "$(dirname "$0")"/variables.sh

docker pull "$DOCKER_USER"/"$IMAGE_NAME":"$IMAGE_TAG"
15 changes: 15 additions & 0 deletions scripts/push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
#
# Created on Wed Sep 04 2024 18:04:56
# Author: Mukai (Tom Notch) Yu
# Email: [email protected]
# Affiliation: Carnegie Mellon University, Robotics Institute
#
# Copyright Ⓒ 2024 Mukai (Tom Notch) Yu
#

. "$(dirname "$0")"/variables.sh

# docker push "$DOCKER_USER"/"$IMAGE_NAME":"$IMAGE_TAG"

docker buildx bake --file "$(dirname "$0")"/../docker-compose.yml --push
52 changes: 52 additions & 0 deletions scripts/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env bash
#
# Created on Wed Sep 04 2024 18:05:06
# Author: Mukai (Tom Notch) Yu
# Email: [email protected]
# Affiliation: Carnegie Mellon University, Robotics Institute
#
# Copyright Ⓒ 2024 Mukai (Tom Notch) Yu
#

. "$(dirname "$0")"/variables.sh

xhost +local:*

if [ ! -f "$XAUTH" ]; then
touch "$XAUTH"
xauth_list=$(xauth nlist "$DISPLAY" | sed -e 's/^..../ffff/')
if [ -n "$xauth_list" ]; then
echo "$xauth_list" | xauth -f "$XAUTH" nmerge -
fi
chmod a+r "$XAUTH"
fi

if [ "$(docker ps -a -q -f name="$CONTAINER_NAME")" ]; then
echo "A container with name ""$CONTAINER_NAME"" is running, force removing it"
docker rm -f "$CONTAINER_NAME"
echo "Done"
fi

docker run \
--name "$CONTAINER_NAME" \
--hostname "$(hostname)" \
--privileged \
--cpus "$AVAILABLE_CORES" \
--network host \
--ipc host \
--pid host \
--ulimit core=-1 \
--group-add audio \
--group-add video \
--volume=":" \
-e DISPLAY="$DISPLAY" \
-e XAUTHORITY="$XAUTH" \
-e QT_X11_NO_MITSHM=1 \
-v /var/lib/systemd/coredump/:/cores \
-v "$XSOCK":"$XSOCK" \
-v "$XAUTH":"$XAUTH" \
-v "$HOME"/.Xauthority:"$CONTAINER_HOME_FOLDER"/.Xauthority:rw \
-v "$(dirname "$0")"/..:"$CONTAINER_HOME_FOLDER"/"$CODE_FOLDER" \
-w "$CONTAINER_HOME_FOLDER"/"$CODE_FOLDER" \
--rm \
-itd "$DOCKER_USER"/"$IMAGE_NAME":"$IMAGE_TAG"
Loading

0 comments on commit 41138a4

Please sign in to comment.