This commit is contained in:
2026-08-04 17:36:05 +08:00
parent d4e023696d
commit 74b3c89e1e
16 changed files with 487 additions and 34 deletions
+34
View File
@@ -0,0 +1,34 @@
#!/usr/bin/env bash
# scripts/shell.sh —— 进入容器开发终端(交互式)
#
# 使用:
# bash scripts/shell.sh (WSL / Git Bash / Linux / macOS)
#
# 容器长跑命令 = `tail -f /dev/null`,跟 docker-compose 等价。
set -eo pipefail
CONTAINER="${CONTAINER:-ros2_dev}"
# 容器已跑 → 直接 exec
if docker ps --format '{{.Names}}' | grep -qx "${CONTAINER}"; then
exec docker exec -it "${CONTAINER}" bash -lc \
'source /opt/ros/humble/setup.bash; source /root/ros2_ws/install/setup.bash; cd /root/ros2_ws; exec bash'
fi
# 容器没跑 → 尝试 start,不行就 run(用宿主当前目录挂进去)
if docker ps -a --format '{{.Names}}' | grep -qx "${CONTAINER}"; then
docker start "${CONTAINER}" >/dev/null
exec docker exec -it "${CONTAINER}" bash -lc \
'source /opt/ros/humble/setup.bash; source /root/ros2_ws/install/setup.bash; cd /root/ros2_ws; exec bash'
fi
echo "[shell.sh] 容器 ${CONTAINER} 不存在,自动用本目录启动..."
docker run -d -it \
--name "${CONTAINER}" \
-v "${PWD}:/root/ros2_ws" \
--network ros2_net \
ros2-humble-dev:latest \
bash >/dev/null
exec docker exec -it "${CONTAINER}" bash -lc \
'source /opt/ros/humble/setup.bash; source /root/ros2_ws/install/setup.bash; cd /root/ros2_ws; exec bash'