Skip to content

Commit 63b0025

Browse files
Caianlidelguseggert
authored
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 <[email protected]> Co-authored-by: Marcin Rataj <[email protected]> Co-authored-by: Gus Eggert <[email protected]>
1 parent bb68a68 commit 63b0025

File tree

6 files changed

+69
-12
lines changed

6 files changed

+69
-12
lines changed

Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ LABEL maintainer="Steven Allen <[email protected]>"
5454
ENV SRC_DIR /go-ipfs
5555
COPY --from=0 $SRC_DIR/cmd/ipfs/ipfs /usr/local/bin/ipfs
5656
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
5758
COPY --from=0 /tmp/su-exec/su-exec-static /sbin/su-exec
5859
COPY --from=0 /tmp/tini /sbin/tini
5960
COPY --from=0 /bin/fusermount /usr/local/bin/fusermount
@@ -93,6 +94,10 @@ RUN mkdir -p $IPFS_PATH \
9394
RUN mkdir /ipfs /ipns \
9495
&& chown ipfs:users /ipfs /ipns
9596

97+
# Create the init scripts directory
98+
RUN mkdir /container-init.d \
99+
&& chown ipfs:users /container-init.d
100+
96101
# Expose the fs-repo as a volume.
97102
# start_ipfs initializes an fs-repo if none is mounted.
98103
# Important this happens after the USER directive so permissions are correct.

bin/container_daemon

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#!/bin/sh
22
set -e
3+
34
user=ipfs
45
repo="$IPFS_PATH"
56

6-
if [ `id -u` -eq 0 ]; then
7+
if [ "$(id -u)" -eq 0 ]; then
78
echo "Changing user to $user"
89
# ensure folder is writable
910
su-exec "$user" test -w "$repo" || chown -R -- "$user" "$repo"
@@ -14,14 +15,11 @@ fi
1415
# 2nd invocation with regular user
1516
ipfs version
1617

18+
1719
if [ -e "$repo/config" ]; then
1820
echo "Found IPFS fs-repo at $repo"
1921
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"}
2523
ipfs config Addresses.API /ip4/0.0.0.0/tcp/5001
2624
ipfs config Addresses.Gateway /ip4/0.0.0.0/tcp/8080
2725

@@ -31,9 +29,9 @@ else
3129
SWARM_KEY_PERM=0400
3230

3331
# Create a swarm key from a given environment variable
34-
if [ ! -z "$IPFS_SWARM_KEY" ] ; then
32+
if [ -n "$IPFS_SWARM_KEY" ] ; then
3533
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
3735
chmod $SWARM_KEY_PERM "$SWARM_KEY_FILE"
3836
fi
3937

@@ -43,14 +41,15 @@ else
4341
# Check during initialization if a swarm key was provided and
4442
# copy it to the ipfs directory with the right permissions
4543
# 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
4745
echo "Copying swarm key from file..."
4846
install -m $SWARM_KEY_PERM "$IPFS_SWARM_KEY_FILE" "$SWARM_KEY_FILE" || exit 1
4947
fi
5048

5149
# Unset the swarm key file variable
5250
unset IPFS_SWARM_KEY_FILE
53-
5451
fi
5552

53+
find /container-init.d -maxdepth 1 -type f -iname '*.sh' -print0 | sort -z | xargs -n 1 -0 -r container_init_run
54+
5655
exec ipfs "$@"

bin/container_init_run

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ docker_stop() {
7070
docker stop "$1"
7171
}
7272

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+
7383
# Test whether all the expected lines are included in a file. The file
7484
# can have extra lines.
7585
#

test/sharness/t0300-docker-image.sh

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ TEST_TESTS_DIR=$(dirname "$TEST_SCRIPTS_DIR")
2929
APP_ROOT_DIR=$(dirname "$TEST_TESTS_DIR")
3030

3131
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 ||
3333
test_fsh echo "TEST_TESTS_DIR: $TEST_TESTS_DIR" ||
3434
test_fsh echo "APP_ROOT_DIR : $APP_ROOT_DIR" ||
3535
test_fsh cat build-actual
@@ -41,8 +41,18 @@ test_expect_success "docker image build output looks good" '
4141
test_fsh cat build-actual
4242
'
4343

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+
4450
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")
4656
'
4757

4858
test_expect_success "docker container gateway is up" '
@@ -53,6 +63,21 @@ test_expect_success "docker container API is up" '
5363
pollEndpoint -host=/ip4/127.0.0.1/tcp/5001 -http-url http://localhost:5001/version -v -tries 30 -tout 1s
5464
'
5565

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+
5681
test_expect_success "simple ipfs add/cat can be run in docker container" '
5782
expected="Hello Worlds" &&
5883
HASH=$(docker_exec "$DOC_ID" "echo $(cat expected) | ipfs add | cut -d' ' -f2") &&
@@ -74,5 +99,7 @@ test_expect_success "stop docker container" '
7499
docker_stop "$DOC_ID"
75100
'
76101

102+
docker_rm "$DOC_ID"
103+
docker_rmi "$IMAGE_ID"
77104
test_done
78105

test/sharness/t0301-docker-migrate.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,7 @@ test_expect_success "correct version was requested" '
7777
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
7878
'
7979

80+
docker_rm "$DOC_ID"
81+
docker_rmi "$IMAGE_ID"
8082
test_done
8183

0 commit comments

Comments
 (0)