-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_container.sh
executable file
·51 lines (45 loc) · 1.78 KB
/
run_container.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env bash
set -e
if [ -z "${DOCKER_IMAGE}" ]; then
echo "Please source the container env first"
echo "For example:"
echo " source containers/ros2-jazzy/env.sh"
echo " source containers/ros2-rolling/env.sh"
exit 1
fi
CONTAINER_NAME=${DOCKER_IMAGE}-container
if [ ! "$(docker images -q ${DOCKER_IMAGE})" ]; then
echo "${DOCKER_IMAGE} does not exist. Creating..."
docker build --no-cache \
--build-arg UID=`id -u` \
--build-arg GID=`id -g` \
--build-arg USERNAME=${USER} \
-f ${DOCKER_FILE} -t ${DOCKER_IMAGE} .
fi
if [[ -z "${DISPLAY}" ]]; then
echo "The terminal doesn't support GUI."
else
xhost +local:
fi
# Check the status of the container (We need to ignore errors here)
set +e
CONTAINER_STATUS=$(docker inspect --format='{{.State.Status}}' $CONTAINER_NAME 2>/dev/null)
set -e
if [ "$CONTAINER_STATUS" == "exited" ]; then
echo "Container '$CONTAINER_NAME' is stopped. Starting the container..."
docker start $CONTAINER_NAME
docker exec -it ${CONTAINER_NAME} zsh
elif [ "$CONTAINER_STATUS" == "running" ]; then
echo "Container '$CONTAINER_NAME' is already running. Attaching to the container..."
docker exec -it ${CONTAINER_NAME} zsh
elif [ "$CONTAINER_STATUS" == "" ]; then
echo "Container '$CONTAINER_NAME' does not exist. Creating and running the container..."
docker run -it --network host --privileged --name ${CONTAINER_NAME} \
-v $(pwd):/workspaces/ros2_build_env --workdir /workspaces/ros2_build_env \
-e QT_X11_NO_MITSHM=1 -e DISPLAY=${DISPLAY} \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-v /etc/localtime:/etc/localtime:ro \
${DOCKER_IMAGE}
else
echo "Unhandled state '$CONTAINER_STATUS' for container '$CONTAINER_NAME'."
fi