diff --git a/README.md b/README.md
index a9f2dc4..e94398d 100644
--- a/README.md
+++ b/README.md
@@ -39,6 +39,7 @@ Mozilla Firefox is a free and open-source web browser developed by Mozilla Found
* [Shell Access](#shell-access)
* [Increasing Shared Memory Size](#increasing-shared-memory-size)
* [Sound Support](#sound-support)
+ * [Setting Firefox Preferences Via Environment Variables](#setting-firefox-preferences-via-environment-variables)
* [Troubleshooting](#troubleshooting)
* [Crashes](#crashes)
* [Support or Contact](#support-or-contact)
@@ -460,6 +461,42 @@ For Firefox to be able to use the audio device available on
the host, `/dev/snd` must be exposed to the container by adding the
`--device /dev/snd` parameter to the `docker run` command.
+## Setting Firefox Preferences Via Environment Variables
+
+Firefox preferences can be set via environment variables
+passed to the containter. During the startup, a script process all these
+variables and modify the preference file accordingly.
+
+The name of the environment variable must start with `FF_PREF_`, followed by a
+string of your choice. For example, `FF_PREF_MY_PREF` is a valid name.
+
+The content of the variable should be in the format `NAME=VAL`, where `NAME` is
+the name of the preference (as found in the `about:config` page) and `VAL` is
+its value. A value can be one of the following type:
+ - string
+ - integer
+ - boolean
+It is important to note that a value of type `string` should be surrounded by
+double quotes. Other types don't need them.
+
+For example, to set the `network.proxy.http` preference, one would pass the
+environment variable to the container by adding the following argument to the
+`docker run` command:
+
+```
+-e "FF_PREF_HTTP_PROXY=network.proxy.http=\"proxy.example.com\""
+```
+
+If a preference needs to be *removed*, its value should be set to `UNSET`. For
+example:
+
+```
+-e "FF_PREF_HTTP_PROXY=network.proxy.http=UNSET
+```
+
+**NOTE**: This is an advanced usage and it is recommended to set preferences
+via Firefox directly.
+
## Troubleshooting
### Crashes
diff --git a/appdefs.xml b/appdefs.xml
index 274f358..7fa9958 100644
--- a/appdefs.xml
+++ b/appdefs.xml
@@ -49,6 +49,44 @@ the host, `/dev/snd` must be exposed to the container by adding the
`--device /dev/snd` parameter to the `docker run` command.
+
+ Setting {{ defs.app.friendly_name }} Preferences Via Environment Variables
+
+{{ defs.app.friendly_name }} preferences can be set via environment variables
+passed to the containter. During the startup, a script process all these
+variables and modify the preference file accordingly.
+
+The name of the environment variable must start with `FF_PREF_`, followed by a
+string of your choice. For example, `FF_PREF_MY_PREF` is a valid name.
+
+The content of the variable should be in the format `NAME=VAL`, where `NAME` is
+the name of the preference (as found in the `about:config` page) and `VAL` is
+its value. A value can be one of the following type:
+ - string
+ - integer
+ - boolean
+It is important to note that a value of type `string` should be surrounded by
+double quotes. Other types don't need them.
+
+For example, to set the `network.proxy.http` preference, one would pass the
+environment variable to the container by adding the following argument to the
+`docker run` command:
+
+```
+-e "FF_PREF_HTTP_PROXY=network.proxy.http=\"proxy.example.com\""
+```
+
+If a preference needs to be *removed*, its value should be set to `UNSET`. For
+example:
+
+```
+-e "FF_PREF_HTTP_PROXY=network.proxy.http=UNSET
+```
+
+**NOTE**: This is an advanced usage and it is recommended to set preferences
+via {{ defs.app.friendly_name }} directly.
+
+
Troubleshooting
diff --git a/rootfs/etc/cont-init.d/firefox-set-prefs-from-env.sh b/rootfs/etc/cont-init.d/firefox-set-prefs-from-env.sh
new file mode 100755
index 0000000..2898de2
--- /dev/null
+++ b/rootfs/etc/cont-init.d/firefox-set-prefs-from-env.sh
@@ -0,0 +1,48 @@
+#!/usr/bin/with-contenv sh
+
+set -e
+
+log() {
+ echo "[cont-init.d] $(basename $0): $*"
+}
+
+PREF_FILE="${1:-/config/profile/prefs.js}"
+
+if [ -z "$PREF_FILE" ]; then
+ log "ERROR: Preference file not set."
+ exit 1
+fi
+
+mkdir -p "$(dirname "$PREF_FILE")"
+[ -f "$PREF_FILE" ] || touch "$PREF_FILE"
+
+env | grep "^FF_PREF_" | while read ENV
+do
+ ENAME="$(echo "$ENV" | cut -d '=' -f1)"
+ EVAL="$(echo "$ENV" | cut -d '=' -f2-)"
+
+ if [ -z "$EVAL" ]; then
+ log "Skipping environment variable '$ENAME': no value set."
+ continue
+ fi
+
+ if echo "$EVAL" | grep -q "="; then
+ PNAME="$(echo "$EVAL" | cut -d '=' -f1)"
+ PVAL="$(echo "$EVAL" | cut -d '=' -f2-)"
+ [ -n "$PVAL" ] || PVAL='""'
+ else
+ PNAME="$EVAL"
+ PVAL='""'
+ fi
+
+ if [ "$PVAL" = "UNSET" ]; then
+ log "Removing preference '$PNAME'..."
+ sed -i "/user_pref(\"$PNAME\",.*);/d" "$PREF_FILE"
+ elif grep -q "user_pref(\"$PNAME\"," "$PREF_FILE"; then
+ log "Setting preference '$PNAME'..."
+ sed -i "s/user_pref(\"$PNAME\",.*);/user_pref(\"$PNAME\", $PVAL);/" "$PREF_FILE"
+ else
+ log "Setting new preference '$PNAME'..."
+ echo "user_pref(\"$PNAME\", $PVAL);" >> "$PREF_FILE"
+ fi
+done