-
Notifications
You must be signed in to change notification settings - Fork 1
/
container_cp.sh
executable file
·51 lines (41 loc) · 1.05 KB
/
container_cp.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
#
# Extract a file from a Docker container image
#
set -euo pipefail
#######################################################
# Self test
#######################################################
err() {
echo -e "$*"
exit 1
}
self_test() {
# Check sha256sum
command -v sha256sum &>/dev/null || \
err "sha256sum not found. Please install coreutils first"
# Check Docker daemon
docker info &>/dev/null || \
err "Make sure docker daemon is running"
}
do_extract() {
if [ "$#" -ne 3 ]; then
echo "Usage: $0 REPOSITORY:TAG FILE_SRC FILE_TO"
echo "Example: $0 myimage:latest /app/myapp ./myapp"
exit 1
fi
set -x
local container_id
local image="$1"
local from="$2"
local to="$3"
local sum
container_id=$(docker create "$image" --entrypoint="null")
docker cp -L "$container_id:$from" "$to"
docker rm "$container_id" >/dev/null
echo "Successfully copied $from in container $image to $to"
sum="$(sha256sum "$to" | cut -f1 -d' ')"
echo "$(basename "$to")(sha256:$sum)"
}
self_test
do_extract "$@"