mc: remove the need for entrypoint script to run as root

This commit is contained in:
Geoff Bourne 2018-04-25 20:57:57 -05:00
parent 488e478374
commit d93f6bad16
5 changed files with 103 additions and 136 deletions

View File

@ -21,10 +21,7 @@ HEALTHCHECK CMD mcstatus localhost ping
RUN addgroup -g 1000 minecraft \ RUN addgroup -g 1000 minecraft \
&& adduser -Ss /bin/false -u 1000 -G minecraft -h /home/minecraft minecraft \ && adduser -Ss /bin/false -u 1000 -G minecraft -h /home/minecraft minecraft \
&& mkdir /data \ && mkdir -m 777 /data /mods /config /plugins \
&& mkdir /config \
&& mkdir /mods \
&& mkdir /plugins \
&& chown minecraft:minecraft /data /config /mods /plugins /home/minecraft && chown minecraft:minecraft /data /config /mods /plugins /home/minecraft
EXPOSE 25565 25575 EXPOSE 25565 25575
@ -34,16 +31,17 @@ ADD https://github.com/itzg/rcon-cli/releases/download/1.3/rcon-cli_linux_amd64
COPY mcadmin.jq /usr/share COPY mcadmin.jq /usr/share
RUN chmod +x /usr/local/bin/* RUN chmod +x /usr/local/bin/*
VOLUME ["/data","/mods","/config","/plugins","/home/minecraft"] VOLUME ["/data","/mods","/config","/plugins"]
COPY server.properties /tmp/server.properties COPY server.properties /tmp/server.properties
WORKDIR /data WORKDIR /data
ENTRYPOINT [ "/start" ] ENTRYPOINT [ "/start" ]
ENV UID=1000 GID=1000 \ ENV JVM_XX_OPTS="-XX:+UseG1GC" MEMORY="1G" \
JVM_XX_OPTS="-XX:+UseG1GC" MEMORY="1G" \
TYPE=VANILLA VERSION=LATEST FORGEVERSION=RECOMMENDED SPONGEBRANCH=STABLE SPONGEVERSION= LEVEL=world \ TYPE=VANILLA VERSION=LATEST FORGEVERSION=RECOMMENDED SPONGEBRANCH=STABLE SPONGEVERSION= LEVEL=world \
PVP=true DIFFICULTY=easy ENABLE_RCON=true RCON_PORT=25575 RCON_PASSWORD=minecraft \ PVP=true DIFFICULTY=easy ENABLE_RCON=true RCON_PORT=25575 RCON_PASSWORD=minecraft \
LEVEL_TYPE=DEFAULT GENERATOR_SETTINGS= WORLD= MODPACK= SERVER_PORT=25565 ONLINE_MODE=TRUE CONSOLE=true LEVEL_TYPE=DEFAULT GENERATOR_SETTINGS= WORLD= MODPACK= SERVER_PORT=25565 ONLINE_MODE=TRUE CONSOLE=true
COPY start* / COPY start* /
USER minecraft

View File

@ -91,17 +91,6 @@ to map a directory on your host machine to the container's `/data` directory, su
When attached in this way you can stop the server, edit the configuration under your attached `/path/on/host` When attached in this way you can stop the server, edit the configuration under your attached `/path/on/host`
and start the server again with `docker start CONTAINERID` to pick up the new configuration. and start the server again with `docker start CONTAINERID` to pick up the new configuration.
**NOTE**: By default, the files in the attached directory will be owned by the host user with UID of 1000 and host group with GID of 1000.
You can use an different UID and GID by passing the options:
-e UID=1000 -e GID=1000
replacing 1000 with a UID and GID that is present on the host.
Here is one way to find the UID and GID:
id some_host_user
getent group some_host_group
## Versions ## Versions
To use a different Minecraft version, pass the `VERSION` environment variable, which can have the value To use a different Minecraft version, pass the `VERSION` environment variable, which can have the value
@ -750,10 +739,6 @@ ways to adjust the memory settings:
The values of all three are passed directly to the JVM and support format/units as The values of all three are passed directly to the JVM and support format/units as
`<size>[g|G|m|M|k|K]`. `<size>[g|G|m|M|k|K]`.
### /data ownership
In order to adapt to differences in `UID` and `GID` settings the entry script will attempt to correct ownership and writability of the `/data` directory. This logic can be disabled by setting `-e SKIP_OWNERSHIP_FIX=TRUE`.
### JVM Options ### JVM Options
General JVM options can be passed to the Minecraft Server invocation by passing a `JVM_OPTS` General JVM options can be passed to the Minecraft Server invocation by passing a `JVM_OPTS`

View File

@ -1,23 +1,95 @@
#!/bin/sh #!/bin/bash
set -e shopt -s nullglob
# Since Alpine doesn't have a usermod command we have to directly manipulate the passwd/group files :(
sed -i "/^minecraft/s/:1000:1000:/:${UID}:${GID}:/" /etc/passwd
sed -i "/^minecraft/s/:1000:/:${GID}:/" /etc/group
if [ "$SKIP_OWNERSHIP_FIX" != "TRUE" ]; then #umask 002
fix_ownership() { export HOME=/data
dir=$1
if ! su-exec minecraft test -w $dir; then if [ ! -e /data/eula.txt ]; then
echo "Correcting writability of $dir ..." if [ "$EULA" != "" ]; then
chown -R minecraft:minecraft $dir echo "# Generated via Docker on $(date)" > eula.txt
chmod -R u+w $dir echo "eula=$EULA" >> eula.txt
if [ $? != 0 ]; then
echo "ERROR: unable to write eula to /data. Please make sure attached directory is writable by uid=${UID}"
exit 2
fi fi
} else
echo ""
fix_ownership /data echo "Please accept the Minecraft EULA at"
fix_ownership /home/minecraft echo " https://account.mojang.com/documents/minecraft_eula"
echo "by adding the following immediately after 'docker run':"
echo " -e EULA=TRUE"
echo ""
exit 1
fi
fi fi
echo "Switching to user 'minecraft'" if ! touch /data/.verify_access; then
su-exec minecraft /start-configuration $@ echo "ERROR: /data doesn't seem to be writable. Please make sure attached directory is writable by uid=${UID} "
exit 2
fi
if [[ $PROXY ]]; then
export http_proxy="$PROXY"
export https_proxy="$PROXY"
echo "INFO: Giving proxy time to startup..."
sleep 5
fi
export SERVER_PROPERTIES=/data/server.properties
export FTB_DIR=/data/FeedTheBeast
export VERSIONS_JSON=https://launchermeta.mojang.com/mc/game/version_manifest.json
echo "Checking version information."
case "X$VERSION" in
X|XLATEST|Xlatest)
export VANILLA_VERSION=`curl -fsSL $VERSIONS_JSON | jq -r '.latest.release'`
;;
XSNAPSHOT|Xsnapshot)
export VANILLA_VERSION=`curl -fsSL $VERSIONS_JSON | jq -r '.latest.snapshot'`
;;
X[1-9]*)
export VANILLA_VERSION=$VERSION
;;
*)
export VANILLA_VERSION=`curl -fsSL $VERSIONS_JSON | jq -r '.latest.release'`
;;
esac
cd /data
export ORIGINAL_TYPE=${TYPE}
echo "Checking type information."
case "$TYPE" in
*BUKKIT|*bukkit|SPIGOT|spigot)
exec /start-deployBukkitSpigot $@
;;
PAPER|paper)
exec /start-deployPaper $@
;;
FORGE|forge)
exec /start-deployForge $@
;;
FTB|ftb)
exec /start-deployFTB $@
;;
VANILLA|vanilla)
exec /start-deployVanilla $@
;;
SPONGEVANILLA|spongevanilla)
exec /start-deploySpongeVanilla $@
;;
*)
echo "Invalid type: '$TYPE'"
echo "Must be: VANILLA, FORGE, BUKKIT, SPIGOT, PAPER, FTB, SPONGEVANILLA"
exit 1
;;
esac

View File

@ -1,95 +0,0 @@
#!/bin/bash
shopt -s nullglob
#umask 002
export HOME=/data
if [ ! -e /data/eula.txt ]; then
if [ "$EULA" != "" ]; then
echo "# Generated via Docker on $(date)" > eula.txt
echo "eula=$EULA" >> eula.txt
if [ $? != 0 ]; then
echo "ERROR: unable to write eula to /data. Please make sure attached directory is writable by uid=${UID}"
exit 2
fi
else
echo ""
echo "Please accept the Minecraft EULA at"
echo " https://account.mojang.com/documents/minecraft_eula"
echo "by adding the following immediately after 'docker run':"
echo " -e EULA=TRUE"
echo ""
exit 1
fi
fi
if ! touch /data/.verify_access; then
echo "ERROR: /data doesn't seem to be writable. Please make sure attached directory is writable by uid=${UID} "
exit 2
fi
if [[ $PROXY ]]; then
export http_proxy="$PROXY"
export https_proxy="$PROXY"
echo "INFO: Giving proxy time to startup..."
sleep 5
fi
export SERVER_PROPERTIES=/data/server.properties
export FTB_DIR=/data/FeedTheBeast
export VERSIONS_JSON=https://launchermeta.mojang.com/mc/game/version_manifest.json
echo "Checking version information."
case "X$VERSION" in
X|XLATEST|Xlatest)
export VANILLA_VERSION=`curl -fsSL $VERSIONS_JSON | jq -r '.latest.release'`
;;
XSNAPSHOT|Xsnapshot)
export VANILLA_VERSION=`curl -fsSL $VERSIONS_JSON | jq -r '.latest.snapshot'`
;;
X[1-9]*)
export VANILLA_VERSION=$VERSION
;;
*)
export VANILLA_VERSION=`curl -fsSL $VERSIONS_JSON | jq -r '.latest.release'`
;;
esac
cd /data
export ORIGINAL_TYPE=${TYPE}
echo "Checking type information."
case "$TYPE" in
*BUKKIT|*bukkit|SPIGOT|spigot)
exec /start-deployBukkitSpigot $@
;;
PAPER|paper)
exec /start-deployPaper $@
;;
FORGE|forge)
exec /start-deployForge $@
;;
FTB|ftb)
exec /start-deployFTB $@
;;
VANILLA|vanilla)
exec /start-deployVanilla $@
;;
SPONGEVANILLA|spongevanilla)
exec /start-deploySpongeVanilla $@
;;
*)
echo "Invalid type: '$TYPE'"
echo "Must be: VANILLA, FORGE, BUKKIT, SPIGOT, PAPER, FTB, SPONGEVANILLA"
exit 1
;;
esac

View File

@ -28,8 +28,15 @@ function downloadSpigot {
;; ;;
esac esac
local downloadVersion
if [[ ${VERSION} == LATEST ]]; then
downloadVersion=${VANILLA_VERSION}
else
downloadVersion=${VERSION}
fi
if [[ -z $downloadUrl ]]; then if [[ -z $downloadUrl ]]; then
downloadUrl="https://cdn.getbukkit.org/${getbukkitFlavor}/${getbukkitFlavor}-${VERSION}.jar" downloadUrl="https://cdn.getbukkit.org/${getbukkitFlavor}/${getbukkitFlavor}-${downloadVersion}.jar"
fi fi
echo "Downloading $match ..." echo "Downloading $match ..."