Using Docker and Docker Compose is highly recommended in development, and is also a good solution in production.
Please note: this Docker configuration is used to run your generated application(s) inside a container image. It’s completely different from the Docker setup that JHipster also provides, which is for running the JHipster generator inside a container
JHipster provides a complete Docker support, in order to:
One great feature of using Docker Compose is that you can easily scale your containers, using the docker-compose scale command. This is very interesting if you use JHipster with a a microservices architecture.
docker-compose scale
When generating your application, JHipster generates for you:
Dockerfile
Those files are located inside folder src/main/docker/.
src/main/docker/
You have to install Docker and Docker Compose:
To create a Docker image of your application, and push it into your Docker registry:
./mvnw package -Pprod docker:build
./gradlew bootRepackage -Pprod buildDocker
This will package your application with the prod profile, and install the image.
prod
To run this image, use the Docker Compose configuration located in the src/main/docker folder of your application:
src/main/docker
docker-compose -f src/main/docker/app.yml up
This command will start up your application and the services it relies on (database, search engine, JHipster Registry…).
Running docker-compose -f src/main/docker/app.yml up already starts up your database automatically.
If you just want to start your database, and not the other services, use the Docker Compose configuration of your database:
docker-compose -f src/main/docker/mysql.yml up
docker-compose -f src/main/docker/postgresql.yml up
docker-compose -f src/main/docker/mongodb.yml up
If you want to use MongoDB with a replica set or shards and a shared configuration between them, you need to build and set up manually Mongo images. Follow these steps to do so:
docker-compose -f src/main/docker/mongodb-cluster.yml build
docker-compose -f src/main/docker/mongodb-cluster.yml up -d
docker-compose -f src/main/docker/mongodb-cluster.yml scale <name_of_your_app>-mongodb-node=X
docker
docker exec -it <yml_folder_name>_<name_of_your_app>-mongodb-node_1 mongo --eval 'var param=X, folder="<yml_folder_name>"' init_replicaset.js
docker exec -it <name_of_your_app>-mongodb mongo --eval 'sh.addShard("rs1/<yml_folder_name>_<name_of_your_app>-mongodb-node_1:27017")'
docker-compose -f src/main/docker/app.yml up -d <name_of_your_app>-app
If you want to add or remove some MongoDB nodes, just repeat step 3 and 4.
To start a Cassandra cluster to run your application locally, you can use the docker_compose file for development cassandra.yml: docker-compose -f src/main/docker/cassandra.yml up -d
cassandra.yml
docker-compose -f src/main/docker/cassandra.yml up -d
Docker-compose will start 3 services: - **-cassandra**, a container with the Cassandra node contact point - **-cassandra-node-1**, a container with a second Cassandra node joining the cluster - **-cassandra-migration** a container to automatically apply all migrations scripts (create the Keyspace, create the tables, all data migrations, ...)
The migration service is responsible to apply all the migration scripts from src/main/resources/config/cql in the following order:
create-keyspace.cql
create-tables.cql
cql/migration/\*.cql
*_added_entity_*.cql
jhipster:entity
On Mac OSx and Windows, Docker containers are not hosted directly but on a VirtualBox VM. Those, you can not access them in localhost but have to hit the VirtualBox IP.
You can override the Cassandra contact point (localhost by default) with this environment variable:
export SPRING_DATA_CASSANDRA_CONTACTPOINTS=`docker-machine ip default`
Cassandra nodes : Because Cassandra nodes are also hosted in the Virtual machine, the Cassandra Java driver will receive an error when trying to contact them after receiving their address from the contact point. To workaround this, you can add a routing rule to your routing table, (source).
Assuming the containers running the Cassandra nodes have IP address 172.18.0.x:
sudo route -n add 172.18.0.0/16 `docker-machine ip default`
#### In production:
The docker_compose files for Cassandra in production has 2 possible configurations: - cassandra-cluster.yml => is the default linked in app.yml. - cassandra-opscenter.yml => is an alternative with an additional container running OpsCenter to monitor the cluster. OpsCenter will be available at http://localhost:8888.
cassandra-cluster.yml
app.yml
cassandra-opscenter.yml
Unlike the other databases docker-compose configuration, the application will not automatically create the Keyspace and apply the changelogs. You have to first start the Cassandra cluster and manually apply the scripts before starting the rest of the services:
docker-compose -f src/main/docker/app.yml up -d <name_of_your_app>-cassandra
docker cp src/main/resources/config/cql/ <name_of_your_app>-cassandra:/
docker exec -it <name_of_your_app>-cassandra init-prod
docker exec -it <name_of_your_app>-cassandra execute-cql /cql/changelog/<insert_default_users scripts>.cql
docker-compose -f src/main/docker/app.yml up -d
One big difference between Cassandra and the other databases, is that you can scale your cluster with Docker Compose. To have X+1 nodes in your cluster, run:
docker-compose -f src/main/docker/cassandra-cluster.yml scale <name_of_your_app>-cassandra-node=X
Running docker-compose -f src/main/docker/app.yml up already starts up your search engine automatically.
If you just want to start your Elasticsearch node, and not the other services, use its specific Docker Compose configuration::
docker-compose -f src/main/docker/elasticsearch.yml up
A Docker Compose configuration is generated for running Sonar:
docker-compose -f src/main/docker/sonar.yml up
To analyze your code, run Sonar on your project:
./mvnw sonar:sonar
./gradlew sonar
The Sonar reports will be available at: http://localhost:9000
You can use docker ps -a to list all the containers
docker ps -a
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fc35e1090021 mysql "/entrypoint.sh mysql" 4 seconds ago Up 4 seconds 0.0.0.0:3306->3306/tcp sampleApplication-mysql
Run docker-compose scale test-app=4 to have 4 instances of application “test” running.
docker-compose scale test-app=4
docker-compose -f src/main/docker/app.yml stop
You can also use directly Docker:
docker stop <container_id>
When you stop a container, the data is not deleted, unless you delete the container.
Be careful! All data will be deleted:
docker rm <container_id>
If you have selected to generate a microservices architecture, each application (gateway, microservice) has a Dockerfile and a Docker Compose configurations, like with a normal monolithic application.
But you can use the specific docker-compose sub-generator, which will generate a global Docker Compose configuration for all your gateway(s) and microservices. This will allow you to deploy and scale your complete architecture with one command.
docker-compose
mkdir docker-compose
cd docker-compose
yo jhipster:docker-compose
This will generate a global Docker Compose configuration, type docker-compose up to run it, and have all your services running at once.
docker-compose up
This configuration will have a pre-configured JHipster Registry, that will configure your services automatically:
bootstrap-prod.yml
spring.cloud.config.fail-fast
spring.cloud.config.retry