Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker: Updated user and data dir handling. #5276

Merged
merged 2 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,10 @@ ENV PATH="/node/bin:${PATH}" ALGOD_PORT="8080" KMD_PORT="7833" ALGORAND_DATA="/a
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates curl && \
rm -rf /var/lib/apt/lists/* && \
mkdir -p "$ALGORAND_DATA" && \
groupadd --system algorand && \
useradd --no-log-init --create-home --system --gid algorand algorand && \
groupadd --gid=999 --system algorand && \
useradd --uid=999 --no-log-init --create-home --system --gid algorand algorand && \
chown -R algorand:algorand /algod

USER algorand

COPY --chown=algorand:algorand --from=builder "/dist/bin/" "/node/bin/"
COPY --chown=algorand:algorand --from=builder "/dist/files/run/" "/node/run/"

Expand Down
20 changes: 3 additions & 17 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ The data directory located at `/algod/data`. Mounting a volume at that location

### Volume Permissions

The container executes in the context of the `algorand` user with it's own UID and GID which is handled differently depending on your operating system. Here are a few options for how to work with this environment:
The container executes in the context of the `algorand` user with UID=999 and GID=999 which is handled differently depending on your operating system or deployment platform. During startup the container temporarily runs as `root` in order to modify the permissions of `/algod/data`. It then changes to the `algorand` user. This can sometimes cause problems, for example if your deployment platform doesn't allow containers to run as the root user.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the run.sh only does the chown if running as root, it seems safe to run the container in a non-root context (provided externally mounted /algorand/data has the proper write permissions already). I think this section of the README could state that as such. In any case, this reads as discouraging running as non-root, which I don't think is warranted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Help! Could you suggest an alternative phrasing? I must have reworded this 5 times before getting to this point.

From my point of view, this is how I would recommend people run their container. It seems less likely to get us bug reports compared to telling people how to configure the data directory and UID. I tried to make it clear that the root user is being used so that anyone sensitive to this would not be surprised (and may even continue reading to see how to override the UID/GID.


#### Named Volume

Expand All @@ -91,23 +91,9 @@ docker volume create algod-data
docker run -it --rm -d -v algod-data:/algod/data algorand/algod
```

#### Local Directory without SELinux
#### Use specific UID and GID

Explicitly set the UID and GID of the container:

```bash
docker run -it --rm -d -v /srv/data:/algod/data -u $UID:$GID algorand/algod
```

#### Local Directory with SELinux

Set the UID and GID of the container while add the `Z` option to the volume definition:

```bash
docker run -it --rm -d -v /srv/data:/algod/data:Z -u $UID:$GID algorand/algod
```

> See the documentation on [configuring the selinux label](https://docs.docker.com/storage/bind-mounts/#configure-the-selinux-label).
On the host system, ensure the directory being mounted uses UID=999 and GID=999. If the directory already has these permissions you may override the default user with `-u 999:999`.

### Private Network

Expand Down
10 changes: 9 additions & 1 deletion docker/files/run/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ if [ "$DEBUG" = "1" ]; then
set -x
fi

# To allow mounting the data directory we need to change permissions
# to our algorand user. The script is initially run as the root user
# in order to change permissions, afterwards the script is re-launched
# as the algorand user.
if [ "$(id -u)" = '0' ]; then
chown -R algorand:algorand $ALGORAND_DATA
exec runuser -u algorand "$BASH_SOURCE"
fi

# Script to configure or resume a network. Based on environment settings the
# node will be setup with a private network or connect to a public network.

####################
# Helper functions #
####################
Expand Down