Skip to content

BMS on Docker Compose (CentOS Linux)

abatac edited this page Oct 18, 2023 · 2 revisions

Docker Compose is a tool for defining and running multi-container Docker applications.

Scope

Step 1 - Install Docker

Install needed packages:

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

Configure the docker-ce repo:

sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

Install docker-ce:

sudo yum install docker-ce

Add your user to the docker group with the following command.

sudo usermod -aG docker $whoami

Note: You will need to re-login via SSH to the server for user permission for docker group to take effect

Set Docker to start automatically at boot time:

sudo systemctl enable docker.service

Finally, start the Docker service:

sudo systemctl start docker.service

Step 2 - Install Docker Compose

Install python-pip

sudo yum install -y python-pip unzip

You will also need to upgrade your Python packages on CentOS 7 to get docker-compose to run successfully:

sudo yum upgrade python*

Run this command to download the current stable release of Docker Compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.25.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Apply executable permissions to the binary

sudo chmod +x /usr/local/bin/docker-compose

Test the Installation

$ docker-compose --version
docker-compose version 1.25.4, build 1110ad01

Related Link - https://docs.docker.com/compose/install/

Step 3 - Docker Login

The image for bmsapp is stored in a dockerhub repository so we need to login first in order to access it.

docker login --username whoami

Step 4 - Download DockerCompose and Configure

Clone the DockerCompose project in github

# Install git if not yet available
$ sudo yum install git -y
$ git clone https://github.com/IntegratedBreedingPlatform/DockerCompose.git

We need to do some initial configuration before running our container. Configure DockerCompose by copying .env.sample to .env and modifying the values accordingly

$ cd DockerCompose
$ cp .env.sample .env
$ vi .env`

a. Default Configuration

Note: For PRODUCTION use make sure to adjust the BMS_MEM value. The recommended size would be at least 4GB which is around 4000m.

# New DockerHub repository used FOR V20 above
BMS_IMG_URI=ibpbmsdocker/bmsapp
BMS_RELEASE=20.0
BMS_DB_PASS=CHANGE_PASSWORD
BMS_CROPS=maize,wheat,rice
# BMS_MEMORY and LIQUIBASE_PARAM are available ONLY FOR V18 above
BMS_MEMORY=4g
# To disable liquibase, set param to blank
# To enable, set to -Dspring.profiles.active=development
LIQUIBASE_PARAM=-Dspring.profiles.active=development

Save the file and move to Step 5

b. Use a custom SSL certificate (bought from a trusted source)

BMS_IMG_URI=ibpbmsdocker/bmsapp
BMS_RELEASE=20.0
BMS_DB_PASS=CHANGE_PASSWORD
BMS_CROPS=maize,wheat,rice

# BMS_MEMORY and LIQUIBASE_PARAM are available ONLY FOR V18 above
BMS_MEMORY=4g
# To disable liquibase, set param to blank
# To enable, set to -Dspring.profiles.active=development
LIQUIBASE_PARAM=-Dspring.profiles.active=development

# Variables when running BMS with SSL
BMS_SSL_HOST=www.example.com
[email protected]
BMS_SCHEME=https
BMS_PORT=443

Save file. And setup the certificate files. Make sure you have the following files that are named accordingly.

  1. KEY - www.example.com.key
  2. CRT - www.example.com.crt

Copy these files to the certs folder under nginx.

$ cd DockerCompose/nginx
$ mkdir certs
$ cp ~/www.example.com.key .
$ cp ~/www.example.com.crt .

Overwrite the docker-compose file to utilize the custom ssl configuration.

cd DockerCompose
cp docker-compose-custom-ssl.yml docker-compose.yml

Step 5 - Run the DockerCompose containers

Once the env variables are set, run the bmsdock containers via docker-compose

$ cd DockerCompose
$ docker-compose up -d
...
...
Creating bmsdocker_bmsmysql_1 ... done
Creating bmsdocker_bmsapp_1   ... done

Check if containers are running

$ docker ps
CONTAINER ID        IMAGE                                                      COMMAND                  CREATED             STATUS              PORTS                    NAMES
352bfcbd723d        526819308695.dkr.ecr.us-east-2.amazonaws.com/bmsapp:20.0   "/run.sh"                23 seconds ago      Up 22 seconds       0.0.0.0:8080->8080/tcp   bmsdocker_bmsapp_1
35d452526032        mysql:5.6                                                  "docker-entrypoint.s…"   28 seconds ago      Up 22 seconds       0.0.0.0:3307->3306/tcp   bmsdocker_bmsmysql_1

Check in browser after 3-5mins

http://localhost/ibpworkbench/main

FAQs

How to restart bmsdocker containers?

Use docker-compose to restart the bmsdocker containers

$ docker-compose restart

How to cleanup data and start fresh?

Stop and remove containers

$ docker-compose down
Stopping bmsdocker_bmsapp_1   ... done
Stopping bmsdocker_bmsmysql_1 ... done
Removing bmsdocker_bmsapp_1   ... done
Removing bmsdocker_bmsmysql_1 ... done
Removing network bmsdocker_bmsnet

Remove the mysql data volume

$ docker volume rm bmsdocker_mysqldata

Start the containers

$ docker-compose up -d

How to create new crops via groovy script?

Access the bmsapp container's (e.g. bmsdocker_bmsapp_1) shell

$ docker exec -it bmsdocker_bmsapp_1 /bin/bash
root@6ee468a9c216:/usr/local/tomcat#

Go to the setuputils directory

cd /bms/DBScripts/setuputils/

Run groovy command

groovy createcropschema.groovy bmsdocker rice merged

Once the new crop is created, you can now exit the container

exit

You need to restart the containers before the new crop is reflected. Ensure that LIQUIBASE_PARAM=-Dspring.profiles.active=development is enabled.

How to use DockerCompose with HTTPS?

The DockerCompose comes with a docker-compose-ssl.yml file. This is the configuration which contains the additional containers in order to run bmsapp w/ SSL. This utilizes letsencrypt for the SSL certificate and an nginx-proxy container to handle the incoming traffic. Creation and Automatic Renewal of the certificate is handled by one of the containers as well.

First, let’s backup the current config and use the new config.

$ mv docker-compose.yml docker-compose-http.yml
$ mv docker-compose-ssl.yml docker-compose.yml

Configure the .env file. Set the value for the domain and email. BMS_SCHEME and BMS_PORT should be https and 443 respectively.

$ vi .env
BMS_SSL_HOST=<domain>
BMS_SSL_EMAIL=<email>
BMS_SCHEME=https
BMS_PORT=443

Start or restart the containers.

$ docker-compose stop
$ docker-compose up -d

How to use disable execution of Liquibase?

Disabling of Liquibase is recommended once the deployment is complete and we want to avoid database changes due to unexpected or unplanned restarts. This is applicable only for v18 release and above.

$ vi .env
...
# set LIQUIBASE_PARAM to blank to disable execution of Liquibase
LIQUIBASE_PARAM=

How to configure JVM memory allocation pool?

It is possible to set the memory allocation pool for JVM.This is applicable only for v18 release and above.

$ vi .env
...
BMS_MEMORY=4g

How to setup a Centralized Logging Directory

Below are the noteable logs that are important for troubleshooting issues.

  • nginx-proxy - /var/log/nginx/
  • bmsapp
    • /usr/local/tomcat/logs
    • /usr/local/tomcat/webapps/logs
  • mysql - /var/log/mysql

We can mount this to the host file by setting up to directory and mounting it using the docker-compose.yml file.

$ cd DockerCompose
$ mkdir logs
$ cd logs
$ mkdir {nginx,mysql,bms}

Once we have the directory setup, let us edit the config file to mount each logs accordingly.

$ vi docker-compose.yml

# under bmsapp -> volumes
- ./logs/bms:/usr/local/tomcat/logs
- ./logs/bms:/usr/local/tomcat/webapps/logs

# under bmsmysql -> volumes
- ./logs/mysql:/var/log/mysql

# under nginx-proxy
- ./logs/nginx:/var/log/nginx

How to mount BMS war modules

To mount BMS war modules from the local file system:

Make sure to build the war files first with the right db.host:

db.host=bmsmysql

Modify docker-compose.yml to mount the war files from the local file system, e.g:

services:
  bmsapp:
    volumes:
      - mysqldata:/var/lib/mysql
      - ./custom:/custom
      - /opt/bms4/BMSAPI/target/bmsapi.war:/usr/local/tomcat/webapps/bmsapi.war
      - /opt/bms4/Fieldbook/target/Fieldbook.war:/usr/local/tomcat/webapps/Fieldbook.war
      - /opt/bms4/Workbench/target/ibpworkbench.war:/usr/local/tomcat/webapps/ibpworkbench.war
      - /opt/bms4/InventoryManager/target/inventory-manager.war:/usr/local/tomcat/webapps/inventory-manager.war

Or if you have a local tomcat webapps folder:

services:
  bmsapp:
    volumes:
      - mysqldata:/var/lib/mysql
      - ./custom:/custom
      - /opt/tomcat/webapps:/usr/local/tomcat/webapps

Start the containers.

How to run multiple MySQL containers in Docker Compose.

We can't have multiple MySQL processes sharing the same data directory. In our compose file, every database container should be mapped with a different volume. We will need to either create one volume per container or configure MySQL to use a unique subdirectory of /var/lib/MySQL for storing data. If we simply remove the volumes: key from each database service, they will all get a unique anonymous volume (because that's how the MySQL image is configured). Alternatively, we can declare and mount a separate volume for each service like below:

version: '2'
services:
  first_db:
    image: mysql:latest
    volumes:
      - "first_db:/var/lib/mysql"
    restart: always
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: rootpw
      MYSQL_DATABASE: first
      MYSQL_USER: first
      MYSQL_PASSWORD: first
  second_db:
    image: mysql:latest
    volumes:
      - "second_db:/var/lib/mysql"
    restart: always
    ports:
      - 3307:3306
    environment:
      MYSQL_ROOT_PASSWORD: rootpw
      MYSQL_DATABASE: second
      MYSQL_USER: second
      MYSQL_PASSWORD: second
volumes:
    first:
    second:

Here is how we can connect to both instances from the host machine:

mysql -h 127.0.0.1 -u first -P 3306 -p first
mysql -h 127.0.0.1 -u second -P 3307 -p second