Skip to content

Commit

Permalink
Merge branch 'feature/database-linking'
Browse files Browse the repository at this point in the history
  • Loading branch information
Sameer Naik committed Apr 14, 2014
2 parents e769ce2 + 2c0525c commit a4ea198
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

**latest**
- support linking to mysql and postgresql containers
- added postgresql server support
- upgrade to redmine_agile v1.1.2
- added SMTP_STARTTLS config option
Expand Down
120 changes: 120 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
- [MySQL](#mysql)
- [Internal MySQL Server](#internal-mysql-server)
- [External MySQL Server](#external-mysql-server)
- [Linking to MySQL Container](#linking-to-mysql-container)
- [PostgreSQL](#postgresql)
- [External PostgreSQL Server](#external-postgresql-server)
- [Linking to PostgreSQL Container](#linking-to-postgresql-container)
- [Mail](#mail)
- [Putting it all together](#putting-it-all-together)
- [Available Configuration Parameters](#available-configuration-parameters)
Expand Down Expand Up @@ -125,6 +127,63 @@ docker run -name redmine -d \

This will initialize the redmine database and after a couple of minutes your redmine instance should be ready to use.

#### Linking to MySQL Container
You can link this image with a mysql container for the database requirements. The alias of the mysql server container should be set to **mysql** while linking with the redmine image.

If a mysql container is linked, only the DB_HOST and DB_PORT settings are automatically retrieved using the linkage. You may still need to set other database connection parameters such as the DB_NAME, DB_USER, DB_PASS and so on.

To illustrate linking with a mysql container, we will use the [sameersbn/mysql](https://github.com/sameersbn/docker-mysql) image. When using docker-mysql in production you should mount a volume for the mysql data store. Please refer the [README](https://github.com/sameersbn/docker-mysql/blob/master/README.md) of docker-mysql for details.

First, lets pull the mysql image from the docker index.
```bash
docker pull sameersbn/mysql:latest
```

For data persistence lets create a store for the mysql and start the container.
```bash
mkdir -p /opt/mysql/data
docker run --name mysql -d \
-v /opt/mysql/data:/var/lib/mysql \
sameersbn/mysql:latest
```

You should now have the mysql server running. By default the sameersbn/mysql image does not assign a password for the root user and allows remote connections for the root user from the 172.17.%.% address space. This means you can login to the mysql server from the host as the root user.

Now, lets login to the mysql server and create a user and database for the redmine application.

```bash
mysql -uroot -h $(docker inspect mysql | grep IPAddres | awk -F'"' '{print $4}')
```

```sql
CREATE USER 'redmine'@'172.17.%.%' IDENTIFIED BY 'password';
CREATE DATABASE IF NOT EXISTS `redmine_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `redmine_production`.* TO 'redmine'@'172.17.%.%';
FLUSH PRIVILEGES;
```

Now that we have the database created for redmine, lets install the database schema. This is done by starting the redmine container with the **app:db:migrate** command.

```bash
docker run -name redmine -i -t -rm --link mysql:mysql \
-e "DB_USER=redmine" -e "DB_PASS=password" \
-e "DB_NAME=redmine_production" \
-v /opt/redmine/files:/redmine/files \
sameersbn/redmine:latest app:db:migrate
```

**NOTE: The above setup is performed only for the first run**.

We are now ready to start the redmine application.

```bash
docker run -name redmine -d --link mysql:mysql \
-e "DB_USER=redmine" -e "DB_PASS=password" \
-e "DB_NAME=redmine_production" \
-v /opt/redmine/files:/redmine/files \
sameersbn/redmine:latest
```

### PostgreSQL

#### External PostgreSQL Server
Expand All @@ -148,6 +207,67 @@ docker run -name redmine -d \

This will initialize the redmine database and after a couple of minutes your redmine instance should be ready to use.

#### Linking to PostgreSQL Container
You can link this image with a postgresql container for the database requirements. The alias of the postgresql server container should be set to **postgresql** while linking with the redmine image.

If a postgresql container is linked, only the DB_HOST and DB_PORT settings are automatically retrieved using the linkage. You may still need to set other database connection parameters such as the DB_NAME, DB_USER, DB_PASS and so on.

To illustrate linking with a postgresql container, we will use the [sameersbn/postgresql](https://github.com/sameersbn/docker-postgresql) image. When using postgresql image in production you should mount a volume for the postgresql data store. Please refer the [README](https://github.com/sameersbn/docker-postgresql/blob/master/README.md) of docker-postgresql for details.

First, lets pull the postgresql image from the docker index.
```bash
docker pull sameersbn/postgresql:latest
```

For data persistence lets create a store for the postgresql and start the container.
```bash
mkdir -p /opt/postgresql/data
docker run --name postgresql -d \
-v /opt/postgresql/data:/var/lib/postgresql \
sameersbn/postgresql:latest
```

You should now have the postgresql server running. The password for the postgres user can be found in the logs of the postgresql image.

```bash
docker logs postgresql
```

Now, lets login to the postgresql server and create a user and database for the redmine application.

```bash
POSTGRESQL_IP=$(docker inspect postgresql | grep IPAddres | awk -F'"' '{print $4}')
psql -U postgres -h ${POSTGRESQL_IP}
```

```sql
CREATE USER redmine WITH PASSWORD 'password';
CREATE DATABASE redmine_production;
GRANT ALL PRIVILEGES ON DATABASE redmine_production to redmine;
```

Now that we have the database created for redmine, lets install the database schema. This is done by starting the redmine container with the **app:db:migrate** command.

```bash
docker run -name redmine -i -t -rm --link postgresql:postgresql \
-e "DB_USER=redmine" -e "DB_PASS=password" \
-e "DB_NAME=redmine_production" \
-v /opt/redmine/files:/redmine/files \
sameersbn/redmine:latest app:db:migrate
```

**NOTE: The above setup is performed only for the first run**.

We are now ready to start the redmine application.

```bash
docker run -name redmine -d --link postgresql:postgresql \
-e "DB_USER=redmine" -e "DB_PASS=password" \
-e "DB_NAME=redmine_production" \
-v /opt/redmine/files:/redmine/files \
sameersbn/redmine:latest
```

### Mail
The mail configuration should be specified using environment variables while starting the redmine image. The configuration defaults to using gmail to send emails and requires the specification of a valid username and password to login to the gmail servers.

Expand Down
22 changes: 20 additions & 2 deletions assets/init
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/bin/bash

DB_HOST=${DB_HOST:-localhost}
DB_HOST=${DB_HOST:-}
DB_PORT=${DB_PORT:-}
DB_NAME=${DB_NAME:-redmine_production}
DB_USER=${DB_USER:-root}
DB_PASS=${DB_PASS:-}
DB_POOL=${DB_POOL:-5}
DB_TYPE=${DB_TYPE:-mysql}
DB_TYPE=${DB_TYPE:-}

MEMCACHED_SIZE=${MEMCACHED_SIZE:-64}

Expand All @@ -29,6 +29,24 @@ export APACHE_PID_FILE="/var/run/apache2.pid"
export APACHE_LOCK_DIR="/var/run/lock/apache2"
export APACHE_RUN_DIR="/var/run/apache2"

# is a mysql or postgresql database linked?
# requires that the mysql or postgresql containers have exposed
# port 3306 and 5432 respectively.
if [ -n "${MYSQL_PORT_3306_TCP_ADDR}" ]; then
DB_TYPE=mysql
DB_HOST=${DB_HOST:-${MYSQL_PORT_3306_TCP_ADDR}}
DB_PORT=${DB_PORT:-${MYSQL_PORT_3306_TCP_PORT}}
elif [ -n "${POSTGRESQL_PORT_5432_TCP_ADDR}" ]; then
DB_TYPE=postgres
DB_HOST=${DB_HOST:-${POSTGRESQL_PORT_5432_TCP_ADDR}}
DB_PORT=${DB_PORT:-${POSTGRESQL_PORT_5432_TCP_PORT}}
fi

# fallback to using the internal mysql server
DB_TYPE=${DB_TYPE:-mysql}
DB_HOST=${DB_HOST:-localhost}

# use default port number if it is still not set
case "${DB_TYPE}" in
mysql) DB_PORT=${DB_PORT:-3306} ;;
postgres) DB_PORT=${DB_PORT:-5432} ;;
Expand Down

0 comments on commit a4ea198

Please sign in to comment.