Run a docker container
One script reply has been approved by the moderators Verified

Runs the docker container of your choice

Created by yassinsiouda74165 920 days ago Picked 12 times
Submitted by yassinsiouda74165 Bash
Verified 223 days ago
1
image="${1:-alpine:latest}"
2
port_host="${2:-8080}"
3
port_container="${3:-8080}"
4
name="${4:-my_container}"
5
gpu="${5:-true}"
6

7
# if gpu is true, then add the gpu flag
8
if [ "$gpu" = true ] ; then
9
  gpu_flag="--gpus all"
10
else
11
  gpu_flag=""
12
fi
13

14
# Stop the container if it is running
15
docker stop "$name" 2>/dev/null || true
16

17
# Run the container
18
docker run -d \
19
  --rm \
20
  --name "$name" \
21
  -p "$port_host:$port_container" \
22
  $gpu_flag \
23
  "$image"
24

25