mirror of
https://github.com/itzg/docker-minecraft-server.git
synced 2026-11-10 10:41:00 +00:00
Merge pull request #299 from Silthus/master
feat(minecraft-server): replace environment variables in configs
This commit is contained in:
31 files changed
+110
-15
No files matched your search
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
+15
-14
@@ -3,19 +3,19 @@ FROM openjdk:8u171-jre-alpine
|
|||||||
LABEL maintainer "itzg"
|
LABEL maintainer "itzg"
|
||||||
|
|
||||||
RUN apk add --no-cache -U \
|
RUN apk add --no-cache -U \
|
||||||
openssl \
|
openssl \
|
||||||
imagemagick \
|
imagemagick \
|
||||||
lsof \
|
lsof \
|
||||||
su-exec \
|
su-exec \
|
||||||
shadow \
|
shadow \
|
||||||
bash \
|
bash \
|
||||||
curl iputils wget \
|
curl iputils wget \
|
||||||
git \
|
git \
|
||||||
jq \
|
jq \
|
||||||
mysql-client \
|
mysql-client \
|
||||||
tzdata \
|
tzdata \
|
||||||
rsync \
|
rsync \
|
||||||
python python-dev py2-pip
|
python python-dev py2-pip
|
||||||
|
|
||||||
RUN pip install mcstatus
|
RUN pip install mcstatus
|
||||||
|
|
||||||
@@ -71,7 +71,8 @@ ENV UID=1000 GID=1000 \
|
|||||||
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= MODS= SERVER_PORT=25565 ONLINE_MODE=TRUE CONSOLE=true SERVER_NAME="Dedicated Server"
|
LEVEL_TYPE=DEFAULT GENERATOR_SETTINGS= WORLD= MODPACK= MODS= SERVER_PORT=25565 ONLINE_MODE=TRUE CONSOLE=true SERVER_NAME="Dedicated Server" \
|
||||||
|
REPLACE_ENV_VARIABLES="FALSE" ENV_VARIABLE_PREFIX="CFG_"
|
||||||
|
|
||||||
COPY start* /
|
COPY start* /
|
||||||
RUN dos2unix /start* && chmod +x /start*
|
RUN dos2unix /start* && chmod +x /start*
|
||||||
@@ -202,6 +202,85 @@ This works well if you want to have a common set of modules in a separate
|
|||||||
location, but still have multiple worlds with different server requirements
|
location, but still have multiple worlds with different server requirements
|
||||||
in either persistent volumes or a downloadable archive.
|
in either persistent volumes or a downloadable archive.
|
||||||
|
|
||||||
|
### Replacing variables inside configs
|
||||||
|
|
||||||
|
Sometimes you have mods or plugins that require configuration information that is only available at runtime.
|
||||||
|
For example if you need to configure a plugin to connect to a database,
|
||||||
|
you don't want to include this information in your Git repository or Docker image.
|
||||||
|
Or maybe you have some runtime information like the server name that needs to be set
|
||||||
|
in your config files after the container starts.
|
||||||
|
|
||||||
|
For those cases there is the option to replace defined variables inside your configs
|
||||||
|
with environment variables defined at container runtime.
|
||||||
|
|
||||||
|
If you set the enviroment variable `REPLACE_ENV_VARIABLES` to `TRUE` the startup script
|
||||||
|
will go thru all files inside your `/data` volume and replace variables that match your
|
||||||
|
defined environment variables. Variables that you want to replace need to be wrapped
|
||||||
|
inside `${YOUR_VARIABLE}` curly brackets and prefixed with a dollar sign. This is the regular
|
||||||
|
syntax for enviromment variables inside strings or config files.
|
||||||
|
|
||||||
|
Optionally you can also define a prefix to only match predefined enviroment variables.
|
||||||
|
|
||||||
|
`ENV_VARIABLE_PREFIX="CFG_"` <-- this is the default prefix
|
||||||
|
|
||||||
|
There are some limitations to what characters you can use.
|
||||||
|
|
||||||
|
| Type | Allowed Characters |
|
||||||
|
| ----- | ------------------- |
|
||||||
|
| Name | `0-9a-zA-Z_-` |
|
||||||
|
| Value | `0-9a-zA-Z_-:/=?.+` |
|
||||||
|
|
||||||
|
Here is a full example where we want to replace values inside a `database.yml`.
|
||||||
|
|
||||||
|
```yml
|
||||||
|
...
|
||||||
|
database:
|
||||||
|
host: ${CFG_DB_HOST}
|
||||||
|
name: ${CFG_DB_NAME}
|
||||||
|
password: ${CFG_DB_PASSWORD}
|
||||||
|
```
|
||||||
|
|
||||||
|
This is how your `docker-compose.yml` file could look like:
|
||||||
|
|
||||||
|
```yml
|
||||||
|
version: '3'
|
||||||
|
# Other docker-compose examples in /examples
|
||||||
|
|
||||||
|
services:
|
||||||
|
minecraft:
|
||||||
|
image: itzg/minecraft-server
|
||||||
|
ports:
|
||||||
|
- "25565:25565"
|
||||||
|
volumes:
|
||||||
|
- "mc:/data"
|
||||||
|
environment:
|
||||||
|
EULA: "TRUE"
|
||||||
|
CONSOLE: "false"
|
||||||
|
ENABLE_RCON: "true"
|
||||||
|
RCON_PASSWORD: "testing"
|
||||||
|
RCON_PORT: 28016
|
||||||
|
# enable env variable replacement
|
||||||
|
REPLACE_ENV_VARIABLES: "TRUE"
|
||||||
|
# define an optional prefix for your env variables you want to replace
|
||||||
|
ENV_VARIABLE_PREFIX: "CFG_"
|
||||||
|
# and here are the actual variables
|
||||||
|
CFG_DB_HOST: "http://localhost:3306"
|
||||||
|
CFG_DB_NAME: "minecraft"
|
||||||
|
CFG_DB_PASSWORD: "ug23u3bg39o-ogADSs"
|
||||||
|
restart: always
|
||||||
|
rcon:
|
||||||
|
image: itzg/rcon
|
||||||
|
ports:
|
||||||
|
- "4326:4326"
|
||||||
|
- "4327:4327"
|
||||||
|
volumes:
|
||||||
|
- "rcon:/opt/rcon-web-admin/db"
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
mc:
|
||||||
|
rcon:
|
||||||
|
```
|
||||||
|
|
||||||
## Running a Bukkit/Spigot server
|
## Running a Bukkit/Spigot server
|
||||||
|
|
||||||
Enable Bukkit/Spigot server mode by adding a `-e TYPE=BUKKIT -e VERSION=1.8` or `-e TYPE=SPIGOT -e VERSION=1.8` to your command-line.
|
Enable Bukkit/Spigot server mode by adding a `-e TYPE=BUKKIT -e VERSION=1.8` or `-e TYPE=SPIGOT -e VERSION=1.8` to your command-line.
|
||||||
|
|||||||
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
+1
-1
@@ -132,4 +132,4 @@ else
|
|||||||
echo "server.properties already created, skipping"
|
echo "server.properties already created, skipping"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
exec /start-minecraftFinalSetup $@
|
exec /start-finalSetup05EnvVariables $@
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
if [ "$REPLACE_ENV_VARIABLES" = "TRUE" ]; then
|
||||||
|
echo "Replacing env variables in configs that match the prefix $ENV_VARIABLE_PREFIX..."
|
||||||
|
while IFS='=' read -r name value ; do
|
||||||
|
# check if name of env variable matches the prefix
|
||||||
|
# sanity check environment variables to avoid code injections
|
||||||
|
if [[ "$name" = $ENV_VARIABLE_PREFIX* ]] && [[ $value =~ ^[0-9a-zA-Z_:/=?.+\-]*$ ]] && [[ $name =~ ^[0-9a-zA-Z_\-]*$ ]]; then
|
||||||
|
echo "Replacing $name with $value ..."
|
||||||
|
find /data/ -type f \( -name "*.yml" -or -name "*.yaml" -or -name "*.txt" -or -name "*.cfg" -or -name "*.properties" \) -exec sed -i 's#${'"$name"'}#'"$value"'#g' {} \;
|
||||||
|
fi
|
||||||
|
done < <(env)
|
||||||
|
fi
|
||||||
|
|
||||||
|
exec /start-minecraftFinalSetup $@
|
||||||
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Executable → Regular
File mode changed.
Reference in new issue
Block a user