Skip to content

Commit

Permalink
#2: Add support for DB creds in the ENV because its 2016
Browse files Browse the repository at this point in the history
  • Loading branch information
pirog committed Apr 1, 2016
1 parent 50657b7 commit 2b27e44
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 17 deletions.
6 changes: 6 additions & 0 deletions 1/apache/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ RUN curl -fSL "https://github.com/backdrop/backdrop/archive/${BACKDROP_VERSION}.
&& tar -xz --strip-components=1 -f backdrop.tar.gz \
&& rm backdrop.tar.gz \
&& chown -R www-data:www-data sites

# Add custom entrypoint to set BACKDROP_SETTINGS correctly
COPY docker-entrypoint.sh /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
CMD ["apache2-foreground"]
53 changes: 53 additions & 0 deletions 1/apache/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash
set -e

if [[ "$1" == apache2* ]] || [ "$1" == php-fpm ]; then
if [ -n "$MYSQL_PORT_3306_TCP" ]; then
if [ -z "$BACKDROP_DB_HOST" ]; then
BACKDROP_DB_HOST='mysql'
else
echo >&2 'warning: both BACKDROP_DB_HOST and MYSQL_PORT_3306_TCP found'
echo >&2 " Connecting to BACKDROP_DB_HOST ($BACKDROP_DB_HOST)"
echo >&2 ' instead of the linked mysql container'
fi
fi

if [ -z "$BACKDROP_DB_HOST" ]; then
echo >&2 'error: missing BACKDROP_DB_HOST and MYSQL_PORT_3306_ADDR environment variables'
echo >&2 ' Did you forget to --link some_mysql_container:mysql or set an external db'
echo >&2 ' with -e BACKDROP_DB_HOST=hostname?'
exit 1
fi

# if we're linked to MySQL and thus have credentials already, let's use them
: ${BACKDROP_DB_USER:=${MYSQL_ENV_MYSQL_USER:-root}}
if [ "$BACKDROP_DB_USER" = 'root' ]; then
: ${BACKDROP_DB_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD}
fi

: ${BACKDROP_DB_PASSWORD:=$MYSQL_ENV_MYSQL_PASSWORD}
: ${BACKDROP_DB_NAME:=${MYSQL_ENV_MYSQL_DATABASE:-backdrop}}
: ${BACKDROP_DB_PORT:=${MYSQL_ENV_MYSQL_PORT:-3306}}
: ${BACKDROP_DB_DRIVER:=${MYSQL_ENV_MYSQL_DRIVER:-mysql}}

if [ -z "$BACKDROP_DB_PASSWORD" ]; then
echo >&2 'error: missing required BACKDROP_DB_PASSWORD environment variable'
echo >&2 ' Did you forget to -e BACKDROP_DB_PASSWORD=... ?'
echo >&2
echo >&2 ' (Also of interest might be BACKDROP_DB_USER and BACKDROP_DB_NAME.)'
exit 1
fi

# lets construct our BACKDROP_SETTINGS and pass them into apache or fpm
export BACKDROP_SETTINGS="{\"databases\":{\"default\":{\"default\":{\"host\":\"database\",\"port\":$BACKDROP_DB_PORT,\"username\":\"$BACKDROP_DB_USER\",\"password\":\"$BACKDROP_DB_PASSWORD\",\"database\":\"$BACKDROP_DB_NAME\",\"driver\":\"$BACKDROP_DB_DRIVER\"}}}}"
if [[ "$1" == apache2* ]]; then
echo "PassEnv BACKDROP_SETTINGS" > /etc/apache2/conf-enabled/backdrop.conf
elif [[ "$1" == php-fpm* ]]; then
POOL_ENV_LINE="env['BACKDROP_SETTINGS'] = $BACKDROP_SETTINGS"
POOL_FILE=/usr/local/etc/php-fpm.d/www.conf
grep -q "$POOL_ENV_LINE" "$POOL_FILE" || echo "$POOL_ENV_LINE" >> "$POOL_FILE"
fi

fi

exec "$@"
6 changes: 6 additions & 0 deletions 1/fpm/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ RUN curl -fSL "https://github.com/backdrop/backdrop/archive/${BACKDROP_VERSION}.
&& tar -xz --strip-components=1 -f backdrop.tar.gz \
&& rm backdrop.tar.gz \
&& chown -R www-data:www-data sites

# Add custom entrypoint to set BACKDROP_SETTINGS correctly
COPY docker-entrypoint.sh /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
CMD ["php-fpm"]
53 changes: 53 additions & 0 deletions 1/fpm/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash
set -e

if [[ "$1" == apache2* ]] || [ "$1" == php-fpm ]; then
if [ -n "$MYSQL_PORT_3306_TCP" ]; then
if [ -z "$BACKDROP_DB_HOST" ]; then
BACKDROP_DB_HOST='mysql'
else
echo >&2 'warning: both BACKDROP_DB_HOST and MYSQL_PORT_3306_TCP found'
echo >&2 " Connecting to BACKDROP_DB_HOST ($BACKDROP_DB_HOST)"
echo >&2 ' instead of the linked mysql container'
fi
fi

if [ -z "$BACKDROP_DB_HOST" ]; then
echo >&2 'error: missing BACKDROP_DB_HOST and MYSQL_PORT_3306_ADDR environment variables'
echo >&2 ' Did you forget to --link some_mysql_container:mysql or set an external db'
echo >&2 ' with -e BACKDROP_DB_HOST=hostname?'
exit 1
fi

# if we're linked to MySQL and thus have credentials already, let's use them
: ${BACKDROP_DB_USER:=${MYSQL_ENV_MYSQL_USER:-root}}
if [ "$BACKDROP_DB_USER" = 'root' ]; then
: ${BACKDROP_DB_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD}
fi

: ${BACKDROP_DB_PASSWORD:=$MYSQL_ENV_MYSQL_PASSWORD}
: ${BACKDROP_DB_NAME:=${MYSQL_ENV_MYSQL_DATABASE:-backdrop}}
: ${BACKDROP_DB_PORT:=${MYSQL_ENV_MYSQL_PORT:-3306}}
: ${BACKDROP_DB_DRIVER:=${MYSQL_ENV_MYSQL_DRIVER:-mysql}}

if [ -z "$BACKDROP_DB_PASSWORD" ]; then
echo >&2 'error: missing required BACKDROP_DB_PASSWORD environment variable'
echo >&2 ' Did you forget to -e BACKDROP_DB_PASSWORD=... ?'
echo >&2
echo >&2 ' (Also of interest might be BACKDROP_DB_USER and BACKDROP_DB_NAME.)'
exit 1
fi

# lets construct our BACKDROP_SETTINGS and pass them into apache or fpm
export BACKDROP_SETTINGS="{\"databases\":{\"default\":{\"default\":{\"host\":\"database\",\"port\":$BACKDROP_DB_PORT,\"username\":\"$BACKDROP_DB_USER\",\"password\":\"$BACKDROP_DB_PASSWORD\",\"database\":\"$BACKDROP_DB_NAME\",\"driver\":\"$BACKDROP_DB_DRIVER\"}}}}"
if [[ "$1" == apache2* ]]; then
echo "PassEnv BACKDROP_SETTINGS" > /etc/apache2/conf-enabled/backdrop.conf
elif [[ "$1" == php-fpm* ]]; then
POOL_ENV_LINE="env['BACKDROP_SETTINGS'] = $BACKDROP_SETTINGS"
POOL_FILE=/usr/local/etc/php-fpm.d/www.conf
grep -q "$POOL_ENV_LINE" "$POOL_FILE" || echo "$POOL_ENV_LINE" >> "$POOL_FILE"
fi

fi

exec "$@"
56 changes: 39 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,62 @@ The comprehensive CMS for small to medium sized businesses and non-profits.
The basic pattern for starting a `backdrop` instance is:

```console
$ docker run --name some-backdrop -d kalabox/backdrop
$ docker run --name some-backdrop --link some-mysql:mysql -d backdrop/backdrop
```

The following environment variables are also honored for configuring your Backdrop CMS instance:

- `-e BACKDROP_DB_HOST=...` (defaults to the IP and port of the linked `mysql` container)
- `-e BACKDROP_DB_USER=...` (defaults to "root")
- `-e BACKDROP_DB_PASSWORD=...` (defaults to the value of the `MYSQL_ROOT_PASSWORD` environment variable from the linked `mysql` container)
- `-e BACKDROP_DB_NAME=...` (defaults to "backdrop")
- `-e BACKDROP_DB_PORT=...` (defaults to 3306)
- `-e BACKDROP_DB_DRIVER=...` (defaults to "mysql")

The `BACKDROP_DB_NAME` **must already exist** on the given MySQL server. Check out the [official mysql image](https://hub.docker.com/_/mysql/) for more info on spinning up a DB.

If you'd like to be able to access the instance from the host without the container's IP, standard port mappings can be used:

```console
$ docker run --name some-backdrop -p 8080:80 kalabox/backdrop
$ docker run --name some-backdrop --link some-mysql:mysql -p 8080:80 -d backdrop/backdrop
```

Then, access it via `http://localhost:8080` or `http://host-ip:8080` in a browser.

There are multiple database types supported by this image, most easily used via standard container linking. In the default configuration, SQLite can be used to avoid a second container and write to flat-files. More detailed instructions for different (more production-ready) database types follow.

When first accessing the webserver provided by this image, it will go through a brief setup process. The details provided below are specifically for the "Set up database" step of that configuration process.

## MySQL
If you'd like to use an external database instead of a linked `mysql` container, specify the hostname and port with `BACKDROP_DB_HOST`/`BACKDROP_DB_PORT` along with the password in `BACKDROP_DB_PASSWORD` and the username in `BACKDROP_DB_USER` (if it is something other than `root`):

```console
$ docker run --name some-backdrop --link some-mysql:mysql -d kalabox/backdrop
$ docker run --name some-backdrop \
-e BACKDROP_DB_HOST=10.1.2.3 \
-e BACKDROP_DB_PORT=10432 \
-e BACKDROP_DB_USER=... \
-e BACKDROP_DB_PASSWORD=... \
-d backdrop/backdrop
```

- Database type: `MySQL, MariaDB, or equivalent`
- Database name/username/password: `<details for accessing your MySQL instance>` (`MYSQL_USER`, `MYSQL_PASSWORD`, `MYSQL_DATABASE`; see environment variables in the description for [`mysql`](https://registry.hub.docker.com/_/mysql/))
- ADVANCED OPTIONS; Database host: `mysql` (for using the `/etc/hosts` entry added by `--link` to access the linked container's MySQL instance)
## ... via [`docker-compose`](https://github.com/docker/compose)

## PostgreSQL
Example `docker-compose.yml` for `backdrop`:

```yaml
backdrop:
image: backdrop/backdrop
links:
- db:mysql
ports:
- 8080:80

db:
image: mysql
environment:
MYSQL_USER: backdrop
MYSQL_PASSWORD: backdrop
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
MYSQL_DATABASE: backdrop

```console
$ docker run --name some-backdrop --link some-postgres:postgres -d kalabox/backdrop
```

- Database type: `PostgreSQL`
- Database name/username/password: `<details for accessing your PostgreSQL instance>` (`POSTGRES_USER`, `POSTGRES_PASSWORD`; see environment variables in the description for [`postgres`](https://registry.hub.docker.com/_/postgres/))
- ADVANCED OPTIONS; Database host: `postgres` (for using the `/etc/hosts` entry added by `--link` to access the linked container's PostgreSQL instance)
Run `docker-compose up`, wait for it to initialize completely, and visit `http://localhost:8080` or `http://host-ip:8080`.

## Adding additional libraries / extensions

Expand Down

0 comments on commit 2b27e44

Please sign in to comment.