-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-enter.sh
More file actions
executable file
·69 lines (61 loc) · 1.77 KB
/
docker-enter.sh
File metadata and controls
executable file
·69 lines (61 loc) · 1.77 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
# bash script for running a docker image in a nice-ish way
# version 0.5
#
# CHANGELOG
#
# 0.5 warn the user about xhost
# 0.4 ask whether to create container
# 0.3 use variables instead of magic words
# 0.2 add feature to open a new shell when container is already running
if [[ $# != 1 ]]; then
echo Usage: $0 CONTAINER
exit 1
fi
IMAGE=
CONTAINER="$1"
# check if the container already exists and get its state
state=$(docker inspect --format "{{.State.Running}}" $CONTAINER 2> /dev/null)
if [ $? != 0 ]; then
state="not-found"
fi
# check that an image exists
if [[ "$state" == "not-found" ]] ; then
echo "There is no docker container named $CONTAINER"
mapfile -t images < <(docker image ls --format {{.Repository}}:{{.Tag}} | grep $CONTAINER)
if [[ ${#images[@]} == 1 ]]; then
read -r -p "Would you like to create a container from the image ${images[0]}? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]; then
IMAGE=${images[0]}
else
exit 1
fi
fi
fi
# we assume the user wants to use X programs
# so we check the status of xhost for them
if (xhost | grep -q enabled); then
echo Warning: xhost access control is enabled
echo 'If you want X access you might want to run `xhost +` on the host'
fi
if [[ "$state" == "not-found" ]]; then
# if it doesn't exist create the container
# Run docker with all the right settings!
docker run -it \
-v /etc/localtime:/etc/localtime:ro \
--device /dev/dri \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-e DISPLAY=unix$DISPLAY \
--name $CONTAINER \
--hostname $CONTAINER \
$IMAGE
elif [[ "$state" == "true" ]]; then
# if it exists and is running
# create a new bash session
docker exec -it \
$CONTAINER \
bash
elif [[ "$state" == "false" ]]; then
# start the container
docker start -i $CONTAINER
fi