@cryptotaxi247 / kubo / commits / 63b002566

feat(docker): /container-init.d for advanced initialization (#6577)

* Add initialization directory support to Docker image * Add sharness test, fix bugs in init script Fixed in init script: - Added some missing quotes around expansions - Fixed INIT_ARGS to not pass any args if IPFS_PROFILE isn't specified - Use printf instead of "echo -e" - Only run scripts in top-level of init dir - Handle filenames correctly when finding init scripts (by using find + xargs) * chore: docker cleanup cleans up containers and images (useful when run on developer machine) * remove container init documentation from README There is already IPFS Docker documentation where this should live: https://docs.ipfs.io/how-to/run-ipfs-inside-docker/ Co-authored-by: Caian <caian@ggaunicamp.com> Co-authored-by: Marcin Rataj <lidel@lidel.org> Co-authored-by: Gus Eggert <gus@gus.dev>

Caian Benedicto committed Apr 12, 2022 at 14:44 UTC 63b00256642c6ddcb35eecd95ea4e23a6bba21f3
6 files changed +69 -12
Dockerfile
+5
@@ -54,6 +54,7 @@ LABEL maintainer="Steven Allen <steven@stebalien.com>"
54 ENV SRC_DIR /go-ipfs
55 COPY --from=0 $SRC_DIR/cmd/ipfs/ipfs /usr/local/bin/ipfs
56 COPY --from=0 $SRC_DIR/bin/container_daemon /usr/local/bin/start_ipfs
57 +COPY --from=0 $SRC_DIR/bin/container_init_run /usr/local/bin/container_init_run
58 COPY --from=0 /tmp/su-exec/su-exec-static /sbin/su-exec
59 COPY --from=0 /tmp/tini /sbin/tini
60 COPY --from=0 /bin/fusermount /usr/local/bin/fusermount
@@ -93,6 +94,10 @@ RUN mkdir -p $IPFS_PATH \
94 RUN mkdir /ipfs /ipns \
95 && chown ipfs:users /ipfs /ipns
96
97 +# Create the init scripts directory
98 +RUN mkdir /container-init.d \
99 + && chown ipfs:users /container-init.d
100 +
101 # Expose the fs-repo as a volume.
102 # start_ipfs initializes an fs-repo if none is mounted.
103 # Important this happens after the USER directive so permissions are correct.
bin/container_daemon
+9 -10
@@ -1,9 +1,10 @@
1 #!/bin/sh
2 set -e
3 +
4 user=ipfs
5 repo="$IPFS_PATH"
6
6 -if [ `id -u` -eq 0 ]; then
7 +if [ "$(id -u)" -eq 0 ]; then
8 echo "Changing user to $user"
9 # ensure folder is writable
10 su-exec "$user" test -w "$repo" || chown -R -- "$user" "$repo"
@@ -14,14 +15,11 @@ fi
15 # 2nd invocation with regular user
16 ipfs version
17
18 +
19 if [ -e "$repo/config" ]; then
20 echo "Found IPFS fs-repo at $repo"
21 else
20 - case "$IPFS_PROFILE" in
21 - "") INIT_ARGS="" ;;
22 - *) INIT_ARGS="--profile=$IPFS_PROFILE" ;;
23 - esac
24 - ipfs init $INIT_ARGS
22 + ipfs init ${IPFS_PROFILE:+"--profile=$IPFS_PROFILE"}
23 ipfs config Addresses.API /ip4/0.0.0.0/tcp/5001
24 ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/8080
25
@@ -31,9 +29,9 @@ else
29 SWARM_KEY_PERM=0400
30
31 # Create a swarm key from a given environment variable
34 - if [ ! -z "$IPFS_SWARM_KEY" ] ; then
32 + if [ -n "$IPFS_SWARM_KEY" ] ; then
33 echo "Copying swarm key from variable..."
36 - echo -e "$IPFS_SWARM_KEY" >"$SWARM_KEY_FILE" || exit 1
34 + printf "%s\n" "$IPFS_SWARM_KEY" >"$SWARM_KEY_FILE" || exit 1
35 chmod $SWARM_KEY_PERM "$SWARM_KEY_FILE"
36 fi
37
@@ -43,14 +41,15 @@ else
41 # Check during initialization if a swarm key was provided and
42 # copy it to the ipfs directory with the right permissions
43 # WARNING: This will replace the swarm key if it exists
46 - if [ ! -z "$IPFS_SWARM_KEY_FILE" ] ; then
44 + if [ -n "$IPFS_SWARM_KEY_FILE" ] ; then
45 echo "Copying swarm key from file..."
46 install -m $SWARM_KEY_PERM "$IPFS_SWARM_KEY_FILE" "$SWARM_KEY_FILE" || exit 1
47 fi
48
49 # Unset the swarm key file variable
50 unset IPFS_SWARM_KEY_FILE
53 -
51 fi
52
53 +find /container-init.d -maxdepth 1 -type f -iname '*.sh' -print0 | sort -z | xargs -n 1 -0 -r container_init_run
54 +
55 exec ipfs "$@"
bin/container_init_run new
+14
@@ -0,0 +1,14 @@
1 +#!/bin/sh
2 +
3 +set -e
4 +
5 +# used by the container startup script for running initialization scripts
6 +
7 +script="$1"
8 +if [ -x "$script" ] ; then
9 + printf "Executing '%s'...\n" "$script"
10 + "$script"
11 +else
12 + printf "Sourcing '%s'...\n" "$script"
13 + . "$script"
14 +fi
test/ipfs-test-lib.sh
+10
@@ -70,6 +70,16 @@ docker_stop() {
70 docker stop "$1"
71 }
72
73 +# This takes a docker ID as argument
74 +docker_rm() {
75 + docker rm -f -v "$1" > /dev/null
76 +}
77 +
78 +# This takes a docker image name as argument
79 +docker_rmi() {
80 + docker rmi -f "$1" > /dev/null
81 +}
82 +
83 # Test whether all the expected lines are included in a file. The file
84 # can have extra lines.
85 #
test/sharness/t0300-docker-image.sh
+29 -2
@@ -29,7 +29,7 @@ TEST_TESTS_DIR=$(dirname "$TEST_SCRIPTS_DIR")
29 APP_ROOT_DIR=$(dirname "$TEST_TESTS_DIR")
30
31 test_expect_success "docker image build succeeds" '
32 - docker_build "$TEST_TESTS_DIR/../Dockerfile" "$APP_ROOT_DIR" >build-actual ||
32 + docker_build "$TEST_TESTS_DIR/../Dockerfile" "$APP_ROOT_DIR" | tee build-actual ||
33 test_fsh echo "TEST_TESTS_DIR: $TEST_TESTS_DIR" ||
34 test_fsh echo "APP_ROOT_DIR : $APP_ROOT_DIR" ||
35 test_fsh cat build-actual
@@ -41,8 +41,18 @@ test_expect_success "docker image build output looks good" '
41 test_fsh cat build-actual
42 '
43
44 +test_expect_success "write init scripts" '
45 + echo "ipfs config Foo Bar" > 001.sh &&
46 + echo "ipfs config Baz Qux" > 002.sh &&
47 + chmod +x 002.sh
48 +'
49 +
50 test_expect_success "docker image runs" '
45 - DOC_ID=$(docker run -d -p 127.0.0.1:5001:5001 -p 127.0.0.1:8080:8080 "$IMAGE_ID")
51 + DOC_ID=$(docker run -d \
52 + -p 127.0.0.1:5001:5001 -p 127.0.0.1:8080:8080 \
53 + -v "$PWD/001.sh":/container-init.d/001.sh \
54 + -v "$PWD/002.sh":/container-init.d/002.sh \
55 + "$IMAGE_ID")
56 '
57
58 test_expect_success "docker container gateway is up" '
@@ -53,6 +63,21 @@ test_expect_success "docker container API is up" '
63 pollEndpoint -host=/ip4/127.0.0.1/tcp/5001 -http-url http://localhost:5001/version -v -tries 30 -tout 1s
64 '
65
66 +test_expect_success "check that init scripts were run correctly and in the correct order" "
67 + echo -e \"Sourcing '/container-init.d/001.sh'...\nExecuting '/container-init.d/002.sh'...\" > expected &&
68 + docker logs $DOC_ID 2>/dev/null | grep -e 001.sh -e 002.sh > actual &&
69 + test_cmp actual expected
70 +"
71 +
72 +test_expect_success "check that init script configs were applied" '
73 + echo Bar > expected &&
74 + docker exec "$DOC_ID" ipfs config Foo > actual &&
75 + test_cmp actual expected &&
76 + echo Qux > expected &&
77 + docker exec "$DOC_ID" ipfs config Baz > actual &&
78 + test_cmp actual expected
79 +'
80 +
81 test_expect_success "simple ipfs add/cat can be run in docker container" '
82 expected="Hello Worlds" &&
83 HASH=$(docker_exec "$DOC_ID" "echo $(cat expected) | ipfs add | cut -d' ' -f2") &&
@@ -74,5 +99,7 @@ test_expect_success "stop docker container" '
99 docker_stop "$DOC_ID"
100 '
101
102 +docker_rm "$DOC_ID"
103 +docker_rmi "$IMAGE_ID"
104 test_done
105
test/sharness/t0301-docker-migrate.sh
+2
@@ -77,5 +77,7 @@ test_expect_success "correct version was requested" '
77 grep "/fs-repo-6-to-7/v1.1.1/fs-repo-6-to-7_v1.1.1_linux-amd64.tar.gz" dist_serv_out > /dev/null
78 '
79
80 +docker_rm "$DOC_ID"
81 +docker_rmi "$IMAGE_ID"
82 test_done
83