Docker for JHipster in Production mode
A few weeks ago I started a new personal project using JHipster. JHipster is a great Yeoman generator used to generate a complete Spring application on the server side and AngularJS on the client side. Julien Dubois, the lead developer of this project, has done a excellent job putting a bunch of technologies all together (here you can find a complete list of all the technologies used).
In the Jhipster project, you can find a docker container which includes all the necessary libraries for running your project in development mode. Nevertheless, a docker container for production mode is missing.
What does this docker include?
The present docker is an extension of Dubois docker, including a MySql database, Grunt, Bower, and a bash script which creates a database and runs the application using the production profile.
Usage
To obtain the docker image you will need to have installed Docker and run the following command:
[shell] docker pull ignaciosuay/jhipster-docker
[/shell]
Once the image is downloaded, you will need to run your application inside the container and forward all local ports exposed (8080 for tomcat, 9000 and 35729 for grunt, 22 for ssh and 3606 for mysql). Try running the following command and don’t forget to change “your_app_folder” to your local path:
[shell] sudo docker run -v ~/your_app_folder:/jhipster -p 8080:8080 -p 9000:9000 -p 35729:35729 -p 4022:22 -p 3306:3306 -t -i ignaciosuay/jhipster-docker /bin/bash
[/shell]
Finally you will need to run the script in /usr/local/bin, to start the mysql service and run the application using the production profile.
[shell] sh /usr/local/bin/script.sh
[/shell]
Change the database name
By default, the script will start the mysql service and will create a database called “jhipster”. If you would like to change the database name, you will need to modify the script on /usr/local/bin/script.sh:
[shell]
#!/bin/bash # Start mysql service and create a database called jhipster service mysql start MYSQL=`which mysql` $MYSQL -u root -e "CREATE DATABASE IF NOT EXISTS jhipster" # Run your project in production mode. # You need to run it as a jhipster user because # some of the tools are not meant to be run by the root user cd /jhipster sudo -u jhipster -H sh -c "mvn -Pprod spring-boot:run"
[/shell]
Use your favourite editor and add your database name at the end of the line 6, instead of “jhipster”.
Start Grunt
If you would like to see any change on the client side and reload your web application instantly then you will need to run the grunt server task. For this purpose, you will need to open a new terminal, access to your container and run grunt following these steps:
- Open a new terminal
- Get your container ID or name
- List your active containers by running:
[shell] sudo docker ps
[/shell] - Copy the container Id or the name
- List your active containers by running:
- Access your container:
[shell] sudo docker exec -i -t CONTAINER_ID bash
[/shell] - Start grunt serve task:
[shell] grunt serve
[/shell]
I followed your instructions and got the latest version of the docker file for jhipster, I however get a error:
unable to find gruntfile or unable to find local grunt.
This is the first time I am using docker, any help will be appreciated.
Hi Dipayan,
It looks like you are trying to run the command “grunt serve” in a folder which doesn’t contains a Gruntfile.js file. I can imagine 2 possible scenarios why this has happen:
1) You haven’t copy your project inside your folder. When you start your container you should run the following command:
sudo docker run -v ~/your_app_folder:/jhipster -p 8080:8080 -p 9000:9000 -p 35729:35729 -p 4022:22 -p 3306:3306 -t -i ignaciosuay/jhipster-docker /bin/bash
You need to be sure that the path to your local project is correct (~/your_app_folder:/jhipster). Everything in that folder will be copied to /jhipster inside your container.
2) Once you are logged in your container you need to be sure that you are in the correct path. In this case, I have copied my project to /jhipster folder, so inside this folder you will need to run: grunt serve.
Hope it helps! Please let me know if you have any comments or queries.