Skip to content

Commit 646dbde

Browse files
Merge pull request #3744 from kpcyrd/feat/docker
docker: Automatically fix permissions
2 parents 988b207 + 3c96b09 commit 646dbde

File tree

4 files changed

+23
-38
lines changed

4 files changed

+23
-38
lines changed

Dockerfile

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ EXPOSE 4002/udp
1313
EXPOSE 5001
1414
EXPOSE 8080
1515

16-
# Volume for mounting an IPFS fs-repo
17-
# This is moved to the bottom for technical reasons.
18-
#VOLUME $IPFS_PATH
19-
2016
# IPFS API to use for fetching gx packages.
2117
# This can be a gateway too, since its read-only API provides all gx needs.
2218
# - e.g. /ip4/172.17.0.1/tcp/8080 if the Docker host
@@ -33,14 +29,17 @@ ENV GOPATH /go
3329
ENV PATH /go/bin:$PATH
3430
ENV SRC_PATH /go/src/github.com/ipfs/go-ipfs
3531

32+
# Expose the fs-repo as a volume.
33+
# start_ipfs initializes an fs-repo if none is mounted
34+
VOLUME $IPFS_PATH
35+
3636
# Get the go-ipfs sourcecode
3737
COPY . $SRC_PATH
3838

39-
RUN apk add --update musl-dev gcc go git bash wget ca-certificates \
40-
# Setup user and fs-repo directory
41-
&& mkdir -p $IPFS_PATH \
39+
RUN apk add --no-cache --virtual .build-deps-ipfs musl-dev gcc go git \
40+
&& apk add --no-cache tini su-exec bash wget ca-certificates \
41+
# Setup user
4242
&& adduser -D -h $IPFS_PATH -u 1000 ipfs \
43-
&& chown ipfs:ipfs $IPFS_PATH && chmod 755 $IPFS_PATH \
4443
# Install gx
4544
&& go get -u github.com/whyrusleeping/gx \
4645
&& go get -u github.com/whyrusleeping/gx-go \
@@ -58,22 +57,12 @@ RUN apk add --update musl-dev gcc go git bash wget ca-certificates \
5857
&& cp $SRC_PATH/bin/container_daemon /usr/local/bin/start_ipfs \
5958
&& chmod 755 /usr/local/bin/start_ipfs \
6059
# Remove all build-time dependencies
61-
&& apk del --purge musl-dev gcc go git && rm -rf $GOPATH && rm -vf $IPFS_PATH/api
62-
63-
# Call uid 1000 "ipfs"
64-
USER ipfs
65-
66-
# Expose the fs-repo as a volume.
67-
# We're doing this down here (and not at the top),
68-
# so that the overlay directory is owned by the ipfs user.
69-
# start_ipfs initializes an ephemeral fs-repo if none is mounted,
70-
# which is why uid=1000 needs write permissions there.
71-
VOLUME $IPFS_PATH
60+
&& apk del --purge .build-deps-ipfs && rm -rf $GOPATH && rm -vf $IPFS_PATH/api
7261

7362
# This just makes sure that:
7463
# 1. There's an fs-repo, and initializes one if there isn't.
7564
# 2. The API and Gateway are accessible from outside the container.
76-
ENTRYPOINT ["/usr/local/bin/start_ipfs"]
65+
ENTRYPOINT ["/sbin/tini", "--", "/usr/local/bin/start_ipfs"]
7766

7867
# Execute the daemon subcommand by default
7968
CMD ["daemon", "--migrate=true"]

Dockerfile.fast

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ ENV GOPATH /go
1919
ENV PATH /go/bin:$PATH
2020
ENV SRC_PATH /go/src/github.com/ipfs/go-ipfs
2121

22+
VOLUME $IPFS_PATH
23+
2224
# This is an optimization which avoids rebuilding
2325
# of the gx dependencies every time anything changes.
2426
# gx will only be invoked if the dependencies have changed.
@@ -28,10 +30,9 @@ ENV SRC_PATH /go/src/github.com/ipfs/go-ipfs
2830
# and trigger a re-run of all following commands.
2931
COPY ./package.json $SRC_PATH/package.json
3032

31-
RUN apk add --update musl-dev gcc go git bash wget ca-certificates \
32-
&& mkdir -p $IPFS_PATH \
33+
RUN apk add --no-cache --virtual .build-deps-ipfs musl-dev gcc go git \
34+
&& apk add --no-cache tini su-exec bash wget ca-certificates \
3335
&& adduser -D -h $IPFS_PATH -u 1000 ipfs \
34-
&& chown ipfs:ipfs $IPFS_PATH && chmod 755 $IPFS_PATH \
3536
&& go get -u github.com/whyrusleeping/gx \
3637
&& go get -u github.com/whyrusleeping/gx-go \
3738
&& ([ -z "$GX_IPFS" ] || echo $GX_IPFS > $IPFS_PATH/api) \
@@ -48,9 +49,7 @@ RUN cd $SRC_PATH \
4849
&& cp ipfs /usr/local/bin/ipfs \
4950
&& cp $SRC_PATH/bin/container_daemon /usr/local/bin/start_ipfs \
5051
&& chmod 755 /usr/local/bin/start_ipfs \
51-
&& apk del --purge musl-dev gcc go git && rm -rf $GOPATH && rm -vf $IPFS_PATH/api
52+
&& apk del --purge .build-deps-ipfs && rm -rf $GOPATH && rm -vf $IPFS_PATH/api
5253

53-
USER ipfs
54-
VOLUME $IPFS_PATH
55-
ENTRYPOINT ["/usr/local/bin/start_ipfs"]
54+
ENTRYPOINT ["/sbin/tini", "--", "/usr/local/bin/start_ipfs"]
5655
CMD ["daemon", "--migrate=true"]

README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,6 @@ IPFS files that will persist when you restart the container.
237237
export ipfs_staging=</absolute/path/to/somewhere/>
238238
export ipfs_data=</absolute/path/to/somewhere_else/>
239239

240-
Make sure docker can access these folders:
241-
242-
sudo chmod -R 777 /absolute/path/to/somewhere/
243-
sudo chmod -R 777 /absolute/path/to/somewhere_else/
244-
245240
Start a container running ipfs and expose ports 4001, 5001 and 8080:
246241

247242
docker run -d --name ipfs_host -v $ipfs_staging:/export -v $ipfs_data:/data/ipfs -p 8080:8080 -p 4001:4001 -p 5001:5001 ipfs/go-ipfs:latest

bin/container_daemon

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#!/bin/sh
2-
3-
user=$(whoami)
2+
set -e
3+
user=ipfs
44
repo="$IPFS_PATH"
55

6-
# Test whether the mounted directory is writable for us
7-
if [ ! -w "$repo" 2>/dev/null ]; then
8-
echo "error: $repo is not writable for user $user (uid=$(id -u $user))"
9-
exit 1
6+
if [ `id -u` -eq 0 ]; then
7+
# ensure folder is writable
8+
su-exec "$user" test -w "$repo" || chown -R -- "$user" "$repo"
9+
# restart script with new privileges
10+
exec su-exec "$user" "$0" "$@"
1011
fi
1112

13+
# 2nd invocation with regular user
1214
ipfs version
1315

1416
if [ -e "$repo/config" ]; then

0 commit comments

Comments
 (0)