Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Run container as non-root, simplify H2 handling in entrypoint (#64977)

Rodrigo López Dato54bd11c5b9d7
diff --git a/Dockerfile b/Dockerfile
index 9d08fef8883..0445454a063 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -57,8 +57,22 @@ RUN apk add -U bash fontconfig curl font-noto font-noto-arabic font-noto-hebrew
COPY --from=builder /home/node/target/uberjar/metabase.jar /app/
COPY bin/docker/run_metabase.sh /app/
+# Also used as the group name
+ARG USERNAME=metabase
+ARG MGID=2000
+ARG MUID=2000
+
+RUN addgroup --gid $MGID --system $USERNAME \
+ && adduser --disabled-password --uid $MUID --ingroup $USERNAME $USERNAME
+
+# ensure writable directory exists for default H2 database location
+RUN mkdir -p /metabase.db && chown $USERNAME:$USERNAME /metabase.db
+
+USER $USERNAME
+
# expose our default runtime port
EXPOSE 3000
# run it
+ENV MB_DB_FILE=/metabase.db/metabase.db
ENTRYPOINT ["/app/run_metabase.sh"]
diff --git a/bin/docker/Dockerfile b/bin/docker/Dockerfile
index 1575aa02d15..01271fcbe55 100644
--- a/bin/docker/Dockerfile
+++ b/bin/docker/Dockerfile
@@ -19,13 +19,22 @@ RUN apk add -U bash fontconfig curl font-noto font-noto-arabic font-noto-hebrew
# add Metabase jar & add our run script to the image
COPY ./metabase.jar ./run_metabase.sh /app/
+# Also used as the group name
+ARG USERNAME=metabase
+ARG MGID=2000
+ARG MUID=2000
+
+RUN addgroup --gid $MGID --system $USERNAME \
+ && adduser --disabled-password --uid $MUID --ingroup $USERNAME $USERNAME
+
+# ensure writable directory exists for default H2 database location
+RUN mkdir -p /metabase.db && chown $USERNAME:$USERNAME /metabase.db
+
+USER $USERNAME
+
# expose our default runtime port
EXPOSE 3000
-# if you have an H2 database that you want to initialize the new Metabase
-# instance with, mount it in the container as a volume that will match the
-# pattern /app/initial*.db:
-# $ docker run ... -v $PWD/metabase.db.mv.db:/app/initial.db.mv.db ...
-
# run it
+ENV MB_DB_FILE=/metabase.db/metabase.db
ENTRYPOINT ["/app/run_metabase.sh"]
diff --git a/bin/docker/run_metabase.sh b/bin/docker/run_metabase.sh
index 0a03b56c6bf..3ade355c3ad 100755
--- a/bin/docker/run_metabase.sh
+++ b/bin/docker/run_metabase.sh
@@ -51,123 +51,9 @@ docker_setup_env() {
file_env 'MB_LDAP_BIND_DN'
}
-# detect if the container is started as root or not
-# if non-root, it's likely we run in a k8s environment with well maintained permissions
-# if root, we need to check some permissions in order to exec metabase with a non-root user
-# In that case, the container is run as root, metabase is run as a non-root user
-# Also, we call the docker_setup_env function before Metabase starts so it takes the Docker secrets in case there are any
-if [ $(id -u) -ne 0 ]; then
- # Launch the application
- # exec is here twice on purpose to ensure that metabase runs as PID 1 (the init process)
- # and thus receives signals sent to the container. This allows it to shutdown cleanly on exit
- docker_setup_env
- exec /bin/sh -c "exec java $JAVA_OPTS -jar /app/metabase.jar $@"
-else
- # Avoid running metabase (or any server) as root where possible
- # If you want to use specific user and group ids that matches an existing
- # account on the host pass them in $MGID and $MUID when starting metabase
- MGID=${MGID:-2000}
- MUID=${MUID:-2000}
- #
- ## create the group if it does not exist
- ## TODO: edit an existing group if MGID has changed
- getent group metabase > /dev/null 2>&1
- group_exists=$?
- if [ $group_exists -ne 0 ]; then
- addgroup --gid $MGID --system metabase
- fi
-
- # create the user if it does not exist
- # TODO: edit an existing user if MGID has changed
- id -u metabase > /dev/null 2>&1
- user_exists=$?
- if [[ $user_exists -ne 0 ]]; then
- adduser --disabled-password -u $MUID --ingroup metabase metabase
- fi
-
- db_file=${MB_DB_FILE:-/metabase.db}
-
- # In order to run metabase as a non-root user in docker, we need to handle various
- # cases where we where previously ran as root and have an existing database that
- # consists of a bunch of files, that are owned by root, sitting in a directory that
- # may only be writable by root. It's not safe to simply change the ownership or
- # permissions of an unknown directory that may be a volume mounted on the host, so
- # we will need to detect this and make a place that is going to be safe to set
- # permissions on.
-
- # So first some preliminary checks:
-
- # 1. Does this container have an existing H2 database file?
- # 2. or an existing H2 database in it's own directory,
- # 3. or neither?
+# Launch the application
+# exec is here twice on purpose to ensure that metabase runs as PID 1 (the init process)
+# and thus receives signals sent to the container. This allows it to shutdown cleanly on exit
+docker_setup_env
-
- # is there a pre-existing files only database without a metabase specific directory?
- if ls $db_file\.* > /dev/null 2>&1; then
- db_exists=true
- else
- db_exists=false
- fi
- # is it an old style file
- if [[ -d "$db_file" ]]; then
- db_directory=true
- else
- db_directory=false
- fi
-
- # If the db exits, and it's just some files in a shared directory we could do
- # serious damage to peoples home or /tmp directories if we where to set the
- # permissions on that directory to allow metabase to create db-lock and db-part
- # file there. To keep them safe we make a new directory with the same name and
- # move the db file into the new directory. If we where passed the name of a
- # directory rather than a specific file, then we are safe to set permissions on
- # that directory so there is no need to move anything.
-
- # an example file would look like /tmp/metabase.db/metabase.db.mv.db
- new_db_dir=$(dirname $db_file)/$(basename $db_file)
-
- if [[ $db_exists = "true" && ! $db_directory = "true" ]]; then
- mkdir $new_db_dir
- mv $db_file\.* $new_db_dir/
- fi
-
- # and for the new install case we create the directory
- if [[ $db_exists = "false" && $db_directory = "false" ]]; then
- mkdir $new_db_dir
- fi
-
- # the case where the DB exists and is a directory, there is nothing to do
- # so nothing happens here. This will be the normal case.
-
- # next we tell metabase use the files we just moved into the directory
- # or create the files in that directory if they don't exist.
- docker_setup_env
- export MB_DB_FILE=$new_db_dir/$(basename $db_file)
-
- # TODO: print big scary warning if they are configuring an ephemeral instance
-
- chown metabase:metabase $new_db_dir $new_db_dir/* 2>/dev/null # all that fussing makes this safe
-
- # Ensure JAR file is world readable
- chmod o+r /app/metabase.jar
-
- # Initialize the Metabase db from H2 dump, if available
- INITIAL_DB=$(ls /app/initial*.db 2> /dev/null | head -n 1)
- if [ -f "${INITIAL_DB}" ]; then
- echo "Initializing Metabase database from H2 database ${INITIAL_DB}..."
- chmod o+r ${INITIAL_DB}
- su metabase -s /bin/sh -c "exec java $JAVA_OPTS -jar /app/metabase.jar load-from-h2 ${INITIAL_DB%.mv.db} $@"
-
- if [ $? -ne 0 ]; then
- echo "Failed to initialize database from H2 database!"
- exit 1
- fi
-
- echo "Done."
- fi
-
- # Launch the application
- # exec is here twice on purpose to ensure that metabase runs as PID 1 (the init process)
- # and thus receives signals sent to the container. This allows it to shutdown cleanly on exit
- exec su metabase -s /bin/sh -c "export JAVA_HOME=/opt/java/openjdk && export PATH=\$JAVA_HOME/bin:\$PATH && exec java $JAVA_OPTS -jar /app/metabase.jar $@"
-fi
+exec /bin/sh -c "exec java $JAVA_OPTS -jar /app/metabase.jar $@"
diff --git a/docs/installation-and-operation/running-metabase-on-docker.md b/docs/installation-and-operation/running-metabase-on-docker.md
index 115cd9938e7..edaa56ec1c1 100644
--- a/docs/installation-and-operation/running-metabase-on-docker.md
+++ b/docs/installation-and-operation/running-metabase-on-docker.md
@@ -163,10 +163,9 @@ networks:
## Additional Docker maintenance and configuration
- [Customizing the Metabase Jetty server](#customizing-the-metabase-jetty-server)
-- [Docker-specific environment variables](#docker-specific-environment-variables)
- [Setting the Java Timezone](#setting-the-java-timezone)
- [Copying the application database](#copying-the-application-database)
-- [Mounting a mapped file storage volume](#mounting-a-mapped-file-storage-volume)
+- [Persisting the H2 application database on your host machine](#persisting-the-h2-application-database-on-your-host-machine)
- [Getting your config back if you stopped your container](#getting-your-config-back-if-you-stopped-your-container)
- [Adding external dependencies or plugins](#adding-external-dependencies-or-plugins)
- [Use Docker Secrets to hide sensitive parameters](#use-docker-secrets-to-hide-sensitive-parameters)
@@ -177,16 +176,6 @@ networks:
You can use any of the custom settings from [Customizing the Metabase Jetty Webserver](../configuring-metabase/customizing-jetty-webserver.md) by setting environment variables in your Docker run command.
-### Docker-specific environment variables
-
-In addition to the standard custom settings there are two docker specific environment variables `MUID` and `MGID` which are used to set the user and group IDs used by metabase when running in a docker container. These settings make it possible to match file permissions when files, such as the application database, are shared between the host and the container.
-
-Here's how to use a database file, owned by your account and stored in your home directory:
-
-```
-docker run -d -v ~/my-metabase-db:/metabase.db --name metabase -e MB_DB_FILE=/metabase.db -e MUID=$UID -e MGID=$GID -p 3000:3000 metabase/metabase
-```
-
### Setting the Java Timezone
It's best to set your Java timezone to match the timezone you'd like all your reports to come in. You can do this by simply specifying the `JAVA_TIMEZONE` environment variable which is picked up by the Metabase launch script. For example:
@@ -199,27 +188,34 @@ docker run -d -p 3000:3000 \
### Copying the application database
-The default location for the application database in the container is `/metabase.db/metabase.db.mv.db`. You can copy this directory out of the container using the following command (replacing `CONTAINER_ID` with the actual container ID or name, `metabase` if you named the container):
+When you run Metabase without an external application database, it will use an embedded H2 database in the container's filesystem. The default location for this H2 database is the directory `/metabase.db`. You can copy this directory out of the container using the following command (replacing `CONTAINER_ID` with the actual container ID or name, `metabase`, if you named the container):
```
docker cp CONTAINER_ID:/metabase.db ./
```
-The DB contents will be left in a directory named metabase.db.
+The database contents will be left in a directory named `metabase.db` on your host filesystem.
-### Mounting a mapped file storage volume
+### Persisting the H2 application database on your host machine
-To persist your data outside of the container and make it available for use between container launches, we can mount a local file path inside our container.
+By default, Metabase stores the embedded H2 application database inside the container. If the container is ever deleted, or you create a new one running a different Metabase version, the database will be lost.
-```
+You can make the H2 database more durable by storing it on your host machine, and mounting it inside the container.
+
+You will need to ensure this directory is read-writable by the container. One way to achieve this is by running the container as the same user from your host machine:
+
+```bash
+# Ensure the directory exists and is owned by your host machine's user
+mkdir ~/metabase-data
+
+# Run Metabase as your host machine's user
docker run -d -p 3000:3000 \
-v ~/metabase-data:/metabase-data \
- -e "MB_DB_FILE=/metabase-data/metabase.db" \
+ --user $(id -u):$(id -g) \
+ -e MB_DB_FILE=/metabase-data/metabase.db \
--name metabase metabase/metabase
```
-When you launch your container, Metabase will use the database file (`MB_DB_FILE`) at `~/metabase-data/metabase.db` instead of its default location. and we are mounting that folder from our local filesystem into the container.
-
### Getting your config back if you stopped your container
If you've previously run and configured your Metabase using the local Database and then stopped the container, your data will still be there unless you deleted the container with the `docker rm` command. To recover your previous configuration:
@@ -249,7 +245,7 @@ docker run -d -p 3000:3000 --name metabase mycompany/metabase-custom
430bb02a37bb2471176e54ca323d0940c4e0ee210c3ab04262cb6576fe4ded6d
```
-You should have your previously configured Metabase Installation back. If it's not the one you expected, try a different stopped container and repeat these steps.
+You should have your previously configured Metabase installation back. If it's not the one you expected, try a different stopped container and repeat these steps.
### Adding external dependencies or plugins
diff --git a/docs/troubleshooting-guide/docker.md b/docs/troubleshooting-guide/docker.md
index 2632b11a7c5..0cc803667e3 100644
--- a/docs/troubleshooting-guide/docker.md
+++ b/docs/troubleshooting-guide/docker.md
@@ -153,4 +153,4 @@ These steps will help you determine whether this the problem is with the network
If you're running Metabase from the JAR in any Unix-like operating system, you can see which user is running Metabase by opening a terminal and typing `ps -uA | grep metabase`.
[configuring-application-database]: ../installation-and-operation/configuring-application-database.md
-[persistent-volume]: ../installation-and-operation/running-metabase-on-docker.md#mounting-a-mapped-file-storage-volume
+[persistent-volume]: ../installation-and-operation/running-metabase-on-docker.md#persisting-the-h2-application-database-on-your-host-machine
diff --git a/resources/openapi/openapi.json b/resources/openapi/openapi.json
index 9ecb1cb730b..570246c2786 100644
--- a/resources/openapi/openapi.json
+++ b/resources/openapi/openapi.json
@@ -39163,4 +39163,4 @@
"description" : "Metabase API",
"url" : ""
} ]
-}
\ No newline at end of file
+}