diff --git a/bitnami/express/Dockerfile b/bitnami/express/Dockerfile index 6197a059008a..72890e81e05d 100644 --- a/bitnami/express/Dockerfile +++ b/bitnami/express/Dockerfile @@ -24,7 +24,7 @@ COPY rootfs/ / # The extra files that we bundle should use the Bitnami User # so the entrypoint does not have any permission issues -RUN chown -R bitnami: /app /app_template +RUN chown -R bitnami: /app /dist USER bitnami diff --git a/bitnami/express/docker-compose-mariadb.yml b/bitnami/express/docker-compose-mariadb.yml new file mode 100644 index 000000000000..3c39386a9969 --- /dev/null +++ b/bitnami/express/docker-compose-mariadb.yml @@ -0,0 +1,20 @@ +version: '2' + +services: + mariadb: + image: bitnami/mariadb:latest + environment: + - MARIADB_DATABASE=my_app_development + + myapp: + tty: true # Enables debugging capabilities when attached to this container. + image: bitnami/express:latest + environment: + - PORT=3000 + - DATABASE_URL=mysql://root@mariadb/my_app_development + depends_on: + - mariadb + ports: + - 3000:3000 + volumes: + - .:/app diff --git a/bitnami/express/docker-compose.yml b/bitnami/express/docker-compose.yml index ad1e8a2c69dd..a742d1ae5d7e 100644 --- a/bitnami/express/docker-compose.yml +++ b/bitnami/express/docker-compose.yml @@ -8,6 +8,7 @@ services: tty: true # Enables debugging capabilities when attached to this container. image: bitnami/express:latest environment: + - PORT=3000 - DATABASE_URL=mongodb://mongodb:27017/my_app_development depends_on: - mongodb diff --git a/bitnami/express/rootfs/app-entrypoint.sh b/bitnami/express/rootfs/app-entrypoint.sh index fba3e5aeaf22..5c9da4a3beaa 100755 --- a/bitnami/express/rootfs/app-entrypoint.sh +++ b/bitnami/express/rootfs/app-entrypoint.sh @@ -9,7 +9,7 @@ fresh_container() { } app_present() { - [ -f /app/app.js ] + [ -f package.json ] } dependencies_up_to_date() { @@ -19,31 +19,47 @@ dependencies_up_to_date() { } database_tier_exists() { - [ ! -z "$(getent hosts mongodb)" ] + [ -n "$(getent hosts mongodb mariadb)" ] +} + +__wait_for_db() { + local host=$1 + local port=$2 + local ip_address=$(getent hosts $1 | awk '{ print $1 }') + + log "Connecting to at $host server at $ip_address" + + counter=0 + until nc -z $ip_address $port; do + counter=$((counter+1)) + if [ $counter == 10 ]; then + log "Error: Couldn't connect to $host server." + return 1 + fi + log "Trying to connect to $host server at $ip_address. Attempt $counter." + sleep 5 + done + log "Connected to $host server" } wait_for_db() { - mongodb_address=$(getent hosts mongodb | awk '{ print $1 }') - counter=0 + if getent hosts mongodb >/dev/null; then + __wait_for_db mongodb 27017 + fi - log "Connecting to MongoDB at $mongodb_address" - - until nc -z $mongodb_address 27017; do - counter=$((counter+1)) - if [ $counter == 10 ]; then - log "Error: Couldn't connect to MongoDB." - exit 1 - fi - log "Trying to connect to MongoDB at $mongodb_address. Attempt $counter." - sleep 5 - done - log "Connected to MongoDB database" + if getent hosts mariadb >/dev/null; then + __wait_for_db mariadb 3306 + fi } -setup_db() { - npm install mongodb@2.1.18 --save - log "Adding MongoDB example files under /config/mongodb.js" - cp -rn /app_template/config . +add_database_support() { + if getent hosts mongodb >/dev/null && ! npm ls mongodb >/dev/null; then + npm install --save mongodb + fi + + if getent hosts mariadb >/dev/null && ! npm ls mysql >/dev/null; then + npm install --save mysql + fi } log () { @@ -51,9 +67,20 @@ log () { } if [ "$1" == npm -a "$2" == "start" ]; then + if database_tier_exists; then + wait_for_db + fi + if ! app_present; then log "Creating express application" express . -f + + if database_tier_exists; then + add_database_support + fi + + log "Adding dist samples" + cp -r /dist/samples . fi if ! dependencies_up_to_date; then @@ -62,10 +89,6 @@ if [ "$1" == npm -a "$2" == "start" ]; then log "Dependencies updated" fi - if database_tier_exists; then - wait_for_db - fi - if ! fresh_container; then echo "#########################################################################" echo " " @@ -76,9 +99,7 @@ if [ "$1" == npm -a "$2" == "start" ]; then echo " " echo "#########################################################################" else - if database_tier_exists; then - setup_db - fi + # Perform any app initialization tasks here. log "Initialization finished" fi diff --git a/bitnami/express/rootfs/dist/samples/mariadb.js b/bitnami/express/rootfs/dist/samples/mariadb.js new file mode 100644 index 000000000000..1e3449d88ffc --- /dev/null +++ b/bitnami/express/rootfs/dist/samples/mariadb.js @@ -0,0 +1,18 @@ +/* + Note: Generated by Bitnami: + Configuration file that shows how to use the built in MySQL database in your project. + Based on the examples found here: https://github.com/mysqljs/mysql +*/ + +var mysql = require('mysql') + , assert = require('assert');; + +// Connection URL configured in your docker-compose.yml file +var url = process.env.DATABASE_URL; + +var connection = mysql.createConnection(url); +connection.connect(function(err) { + assert.equal(null, err); + console.log("Connected correctly to MySQL server"); + connection.end(); +}); diff --git a/bitnami/express/rootfs/app_template/config/mongodb.js b/bitnami/express/rootfs/dist/samples/mongodb.js similarity index 94% rename from bitnami/express/rootfs/app_template/config/mongodb.js rename to bitnami/express/rootfs/dist/samples/mongodb.js index f41dd80dcb06..ad668e76ec86 100644 --- a/bitnami/express/rootfs/app_template/config/mongodb.js +++ b/bitnami/express/rootfs/dist/samples/mongodb.js @@ -11,7 +11,7 @@ var MongoClient = require('mongodb').MongoClient , assert = require('assert'); // Connection URL configured in your docker-compose.yml file -var url = process.env.DATABASE_URL +var url = process.env.DATABASE_URL; MongoClient.connect(url, function(err, db) { assert.equal(null, err);