├── data ├── mysqlconf │ ├── my.cnf │ ├── docker.cnf │ └── mysql.cnf └── sonarqube │ └── docker │ ├── .gitignore │ └── com │ └── basgeekball │ └── db │ ├── Detector.class │ └── Detector.java ├── assets ├── docker.png └── sonarqube.png ├── .gitignore ├── mysql_mac └── Dockerfile ├── docker-compose-postgres.yml ├── docker-compose-mysql.yml ├── .travis.yml ├── LICENSE └── README.md /data/mysqlconf/my.cnf: -------------------------------------------------------------------------------- 1 | [mysqld] 2 | max_allowed_packet=32M 3 | -------------------------------------------------------------------------------- /data/sonarqube/docker/.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | !Detector.class 3 | -------------------------------------------------------------------------------- /data/mysqlconf/docker.cnf: -------------------------------------------------------------------------------- 1 | [mysqld] 2 | skip-host-cache 3 | skip-name-resolve 4 | -------------------------------------------------------------------------------- /assets/docker.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thyrlian/SonarOnDocker/HEAD/assets/docker.png -------------------------------------------------------------------------------- /assets/sonarqube.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thyrlian/SonarOnDocker/HEAD/assets/sonarqube.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # persistent data on host 2 | data/sonarqube/* 3 | !data/sonarqube/docker/ 4 | data/mysql/* 5 | data/postgresql/* 6 | 7 | # macOS 8 | .DS_Store 9 | -------------------------------------------------------------------------------- /data/sonarqube/docker/com/basgeekball/db/Detector.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thyrlian/SonarOnDocker/HEAD/data/sonarqube/docker/com/basgeekball/db/Detector.class -------------------------------------------------------------------------------- /mysql_mac/Dockerfile: -------------------------------------------------------------------------------- 1 | # ====================================================================== # 2 | # 3 | # MySQL 4 | # official MySQL https://hub.docker.com/_/mysql/ 5 | # Try to solve the mounting volume permission issue on Mac OSX 6 | # 7 | # ====================================================================== # 8 | 9 | 10 | # Base image 11 | # ---------------------------------------------------------------------- # 12 | FROM mysql:latest 13 | 14 | 15 | # Author 16 | # ---------------------------------------------------------------------- # 17 | MAINTAINER Jing Li 18 | 19 | 20 | # Execute commands 21 | # ---------------------------------------------------------------------- # 22 | RUN usermod -u 1000 mysql \ 23 | && mkdir -p /var/run/mysqld \ 24 | && chmod -R 777 /var/run/mysqld 25 | -------------------------------------------------------------------------------- /data/mysqlconf/mysql.cnf: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. 2 | # 3 | # This program is free software; you can redistribute it and/or modify 4 | # it under the terms of the GNU General Public License as published by 5 | # the Free Software Foundation; version 2 of the License. 6 | # 7 | # This program is distributed in the hope that it will be useful, 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 | # GNU General Public License for more details. 11 | # 12 | # You should have received a copy of the GNU General Public License 13 | # along with this program; if not, write to the Free Software 14 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 15 | 16 | # 17 | # The MySQL Client configuration file. 18 | # 19 | # For explanations see 20 | # http://dev.mysql.com/doc/mysql/en/server-system-variables.html 21 | 22 | [mysql] 23 | -------------------------------------------------------------------------------- /docker-compose-postgres.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | 3 | services: 4 | sonarqube: 5 | image: sonarqube 6 | ports: 7 | - "9000:9000" 8 | volumes: 9 | - /opt/sonarqube/extensions 10 | - ./data/sonarqube/docker:/opt/sonarqube/docker 11 | environment: 12 | - SONARQUBE_JDBC_URL=jdbc:postgresql://postgres:5432/sonar?charSet=UNICODE 13 | - SONARQUBE_JDBC_USERNAME=sonar 14 | - SONARQUBE_JDBC_PASSWORD=sonar 15 | links: 16 | - postgres 17 | entrypoint: | 18 | bash -c 'bash -s < ⚠ Can not identify DB."); 27 | System.exit(1); 28 | } 29 | try { 30 | Class.forName(driver); 31 | } catch (ClassNotFoundException e) { 32 | System.out.println("#==> ⚠ Can not find JDBC driver for " + name + "."); 33 | e.printStackTrace(); 34 | System.exit(1); 35 | } 36 | Properties prop = new Properties(); 37 | prop.put("user", "sonar"); 38 | prop.put("password", "sonar"); 39 | int retries = 120; 40 | long interval = 500; 41 | Connection connection; 42 | for (int i = 0; i < retries; i++) { 43 | try { 44 | connection = DriverManager.getConnection(url, prop); 45 | if (connection != null) { 46 | System.out.println("#==> ⚡ DB connection is successful."); 47 | return; 48 | } 49 | } catch (SQLException e) { 50 | System.out.println("#==> ⚠ Can not establish a connection to the DB."); 51 | } 52 | Thread.sleep(interval); 53 | } 54 | System.out.println("#==> ⚠ Failed to connect to the DB. Quit."); 55 | System.exit(1); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | 3 | language: bash 4 | 5 | services: 6 | - docker 7 | 8 | env: 9 | global: 10 | - SONARQUBE_VERSION="lts" 11 | 12 | matrix: 13 | include: 14 | - env: DB_NAME="mysql" DB_VERSION="5.7" DB_LOG="ready for connections" COMPOSE_FILE="$TRAVIS_BUILD_DIR/SonarOnDocker/docker-compose-mysql.yml" SONARQUBE_VERSION="7.7-community" 15 | - env: DB_NAME="postgres" DB_VERSION="10" DB_LOG="ready to accept connections" COMPOSE_FILE="$TRAVIS_BUILD_DIR/SonarOnDocker/docker-compose-postgres.yml" 16 | 17 | git: 18 | depth: 1 19 | 20 | install: 21 | - git clone --branch=$TRAVIS_BRANCH https://github.com/thyrlian/SonarOnDocker.git 22 | 23 | before_script: 24 | - sudo sysctl -w vm.max_map_count=262144 25 | 26 | script: 27 | - docker pull sonarqube:$SONARQUBE_VERSION 28 | - docker pull $DB_NAME:$DB_VERSION 29 | - sed -i "s/image\:\ sonarqube/image\:\ sonarqube\:$SONARQUBE_VERSION/g" $COMPOSE_FILE 30 | - sed -i "s/image\:\ $DB_NAME/image\:\ $DB_NAME\:$DB_VERSION/g" $COMPOSE_FILE 31 | - docker-compose -f $COMPOSE_FILE run --service-ports -d sonarqube 32 | - for ((i=1; i<=300; i++)) { sleep 1; if docker ps -aqf "ancestor=$DB_NAME:$DB_VERSION" | xargs docker logs 2>&1 | grep "$DB_LOG" &> /dev/null && docker ps -aqf "ancestor=sonarqube:$SONARQUBE_VERSION" | xargs docker logs 2>&1 | grep "SonarQube is up" &> /dev/null; then return 0; fi } && return 1 33 | - curl -s -w "%{http_code}\\n" "http://localhost:9000/" -o /dev/null | grep "200\|302" &> /dev/null 34 | 35 | after_success: 36 | - echo "☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑" 37 | - echo "---------- Docker orchestration succeeded! ----------" 38 | - echo "☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑☑" 39 | 40 | after_failure: 41 | - echo "☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒" 42 | - echo "---------- Docker orchestration failed! ----------" 43 | - echo "☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒☒" 44 | 45 | after_script: 46 | - echo "---------- DB logs ----------" 47 | - echo "==================================================" 48 | - docker ps -aqf "ancestor=$DB_NAME:$DB_VERSION" | xargs docker logs 2>&1 49 | - echo "==================================================" 50 | - echo "---------- SonarQube logs ----------" 51 | - echo "==================================================" 52 | - docker ps -aqf "ancestor=sonarqube:$SONARQUBE_VERSION" | xargs docker logs 2>&1 53 | - echo "==================================================" 54 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sonar On Docker 2 | 3 | A complete guide to running [**SonarQube**](http://www.sonarqube.org) with any DB in [**Docker**](http://www.docker.com). 4 | 5 | [![Build Status](https://travis-ci.org/thyrlian/SonarOnDocker.svg?branch=master)](https://travis-ci.org/thyrlian/SonarOnDocker) 6 | 7 | 8 | 9 | 10 | 11 | ## Pitfalls 12 | 13 | Orchestrating Docker with compose sounds easy, but there are a few pitfalls in practice. Read on to learn about the whole story, or if you just wanna run it, jump directly to [**Getting Started**](https://github.com/thyrlian/SonarOnDocker/blob/master/README.md#getting-started). 14 | 15 | When running SonarQube and database containers together by compose for the first time, you may encounter errors like this: 16 | 17 | ```console 18 | Can not connect to database. Please check connectivity and settings (see the properties prefixed by 'sonar.jdbc.'). 19 | ``` 20 | 21 | It’s because database initialization process takes a bit longer than SonarQube’s boot time, especially when there is no persistent database. 22 | 23 | So, how to detect the readiness state of the database connection? 24 | 25 | **What failed**: 26 | 27 | * [**`depends_on`**](https://docs.docker.com/compose/compose-file/#depends_on) **option**: You can specify this option in the docker-compose.yml file to start services in dependency order, but it won't wait for the dependent service to be ready. 28 | 29 | * **wait script**: The [wait-for-it](https://github.com/vishnubob/wait-for-it) script recommended in Docker's [Controlling startup order in Compose](https://docs.docker.com/compose/startup-order/) article can be used to check the availability of the database port and wait. Unfortunately, this doesn't help either. The reason is that the port will be available right after the database container starts, but that doesn’t mean the database connection is ready. Just forget about `nc -v -n -z -w1 $HOST $PORT`. 30 | 31 | * [**`HEALTHCHECK`**](https://docs.docker.com/engine/reference/builder/#healthcheck) **instruction**: This new feature is available for Dockerfiles since version 1.12, but not yet for docker-compose (now it is [there](https://docs.docker.com/compose/compose-file/#healthcheck)). Usage: `HEALTHCHECK [OPTIONS] CMD command`. This sounds promising, but you still have to write the command on your own, to tell Docker what to check. 32 | 33 | * **Database command**: How about running `mysql -e "select 1"` to check the database availability? Yep - but wait a second - the SonarQube container doesn't have a mysql client installed, and we have no control over the official SonarQube docker image. 34 | 35 | * **Web Server**: Yet another hack - what if we set up a minimal (one-liner) web server in the MySQL container that responds with the database status? Something like `while true; do echo -e "HTTP/1.1 200 OK\r\n\r\n$(db_status)" | nc -l -q 0 -p 9999; done`. Unfortunately again, netcat is usually not part of the database image. 36 | 37 | * **Database logs**: MySQL writes its readiness status to the logs, so maybe we could try searching there with `grep 'ready for connections'`. Normally, the logs are only accessible within the MySQL container, or from the host machine, but not from the SonarQube container. Perhaps we could try to persist MySQL logs to the host directory by adding `command: bash -c "mkdir -p /var/log/mysql && mysqld 2>&1 | tee /var/log/mysql/mysql.log"` and `volumes: ./data/mysql:/var/log/mysql`. Then we could mount the volume to share it with the SonarQube container, so that it would be available there. But do we really want to mess with adding `command` and `volumes` configurations on both services' sides? 38 | 39 | There has to be a better way… 40 | 41 | **What worked**: 42 | 43 | * **JDBC**: finally, there comes an easy solution - creating a [Java file](https://github.com/thyrlian/SonarOnDocker/blob/master/data/sonarqube/docker/com/basgeekball/db/Detector.java) with some JDBC code that checks the database availability (lucky for us, a Java environment and the JDBC jar file are both available in the SonarQube container). All we needed to do is override the entrypoint of the SonarQube container to first check the database availability via this Java code, then run the default entrypoint shell script when the database is ready. Pretty slick and it works great! 44 | 45 | ## Disclaimer 46 | 47 | Starting with [SonarQube v7.9](https://docs.sonarqube.org/7.9/requirements/requirements/), it will no longer support MySQL. For more information, please visit [End of Life of MySQL Support](https://community.sonarsource.com/t/end-of-life-of-mysql-support). As a result, for any paragraph below mentioning MySQL, please ignore. Unless you still wanna use MySQL with SonarQube v7.7 (or earlier version). 48 | 49 | ## Getting Started 50 | 51 | ### Setup 52 | 53 | 1. Make sure that you've cloned the whole project, particularly the [***Detector.java***](https://github.com/thyrlian/SonarOnDocker/blob/master/data/sonarqube/docker/com/basgeekball/db/Detector.java) - for checking the readiness of database. 54 | 55 | 2. Pull the desired version of docker images for [**SonarQube**](https://hub.docker.com/_/sonarqube/) and database (e.g. [**MySQL**](https://hub.docker.com/_/mysql) or [**PostgreSQL**](https://hub.docker.com/_/postgres)): 56 | 57 | ```bash 58 | # pull SonarQube image 59 | docker pull sonarqube[:TAG] 60 | # pull database image 61 | docker pull mysql[:TAG] 62 | # or 63 | docker pull postgres[:TAG] 64 | ``` 65 | 66 | **Heads-up**: It's NOT a good idea to directly use the latest version of a database without checking the SonarQube requirements ([prerequisite](https://docs.sonarqube.org/latest/requirements/requirements/) for the latest SonarQube version, or [docs](https://docs.sonarqube.org/latest/previous-versions/) for previous versions). For instance, SonarQube 6.3 only supports MySQL 5.6 & 5.7. And if you spin up SonarQube 6.3 with MySQL 8.0, an exception would be thrown: 67 | 68 | ```console 69 | com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. 70 | ``` 71 | 72 | 3. (Optional - MySQL & macOS only) There is a permission problem when mount a host directory in MySQL container using `boot2docker`. 73 | 74 | ```console 75 | [ERROR] InnoDB: Operating system error number 13 in a file operation. 76 | [ERROR] InnoDB: The error means mysqld does not have the access rights to the directory. 77 | ``` 78 | 79 | **Solution**: 80 | 81 | * Build a custom MySQL image for macOS (don't forget to change the `latest` tag in `mysql_mac/Dockerfile`): 82 | 83 | ```console 84 | docker build -t mysql_mac[:TAG] [PATH_OF_THIS_REPO_ON_YOUR_DISK]/mysql_mac/ 85 | ``` 86 | 87 | * Edit ***docker-compose-mysql.yml***, replace `image: mysql` by `image: mysql_mac`. 88 | 89 | 4. In order to persist data, you need to setup mounting data volumes: replace two mounting points under volumes in ***docker-compose-\.yml*** file. 90 | 91 | ``` 92 | - [PATH_TO_PERSIST_SONAR_DATA_ON_HOST]:/opt/sonarqube/extensions 93 | - [PATH_TO_PERSIST_DATABASE_DATA_ON_HOST]:[DATABASE_VOLUMES] 94 | ``` 95 | 96 | Note: the path to persist data on host could be a relative path, e.g.: `./data/xyz` 97 | 98 | 5. Instead of using default empty tag or dynamic `latest` tag, please alter them in `Dockerfile` or `docker-compose` file with more specific tags. Because `latest` can lead to unpredictable and unrepeatable image builds. 99 | 100 | ### Play 101 | 102 | ```console 103 | docker-compose -f [PATH_OF_THIS_REPO_ON_YOUR_DISK]/docker-compose-.yml up 104 | ``` 105 | 106 | ## Persist Data 107 | 108 | * SonarQube's plugins. 109 | 110 | ```bash 111 | /opt/sonarqube/extensions 112 | ``` 113 | 114 | * All historical analysis data, imported rules, changed settings are saved in database. 115 | 116 | ```bash 117 | /var/lib/mysql 118 | # or 119 | /var/lib/postgresql 120 | ``` 121 | 122 | Don't persist ElasticSearch indices (which is located at `/opt/sonarqube/data/es`), let it rebuild by itself, otherwise it could cause problem during upgrading. And any ungraceful shutdown (such as crash) of SonarQube could lead to out-of-sync indices. 123 | 124 | ## Upgrading 125 | 126 | ⚠ Always keep a **backed up database** in case upgrade fails and roll back is needed. 127 | 128 | ### MySQL 129 | 130 | 1. Perform a logical backup on the old version of MySQL 131 | 132 | ```console 133 | mysqldump -u sonar -p --opt sonar > [PATH_TO_MYSQL_BACKUP]/sonar.sql 134 | ``` 135 | 136 | 2. Start a MySQL docker container (new version of MySQL) 137 | 138 | ```console 139 | docker run -i -t -v [PATH_TO_MYSQL_BACKUP]:/tmp -v [PATH_TO_PERSIST_DB]:/var/lib/mysql mysql /bin/bash 140 | ``` 141 | 142 | 3. Start MySQL server 143 | 144 | ```console 145 | /etc/init.d/mysql start 146 | ``` 147 | 148 | 4. Start MySQL client 149 | 150 | ```console 151 | mysql 152 | ``` 153 | 154 | 5. Create and use the database 155 | 156 | ```sql 157 | create database sonar; 158 | use sonar; 159 | ``` 160 | 161 | 6. Grant privileges to user 162 | 163 | ```sql 164 | grant all on sonar.* to 'sonar'@'%' identified by 'sonar'; 165 | grant all on sonar.* to 'sonar'@'localhost' identified by 'sonar'; 166 | grant usage on *.* to sonar@localhost identified by 'sonar'; 167 | grant all privileges on sonar.* to sonar@localhost; 168 | ``` 169 | 170 | 7. Restore the backup file (by executing SQL script) 171 | 172 | ```sql 173 | source /tmp/sonar.sql 174 | ``` 175 | 176 | 8. Quit MySQL client 177 | 178 | ```sql 179 | exit 180 | ``` 181 | 182 | 9. Stop MySQL server 183 | 184 | ```console 185 | /etc/init.d/mysql stop 186 | ``` 187 | 188 | Now you have successfully restored the database on the new version of MySQL. The database data are stored in ***path_to_persist_db*** of your host. 189 | 190 | ### SonarQube 191 | 192 | SonarQube Server upgrade process is automated, you have nothing to manually change in the SonarQube Database. 193 | 194 | **Migration path**: `[YOUR_VERSION] -> LTS (if exists) -> [EXPECTED_VERSION]` 195 | 196 | [Upgrading guide by SonarQube](https://docs.sonarqube.org/latest/setup/upgrading/) (just for reference, please follow below steps.) 197 | 198 | Don't try to stop the SonarQube server, if you kill the process, the SonarQube container exits immediately. So you can't really upgrade SonarQube by hand within its container. Don't worry, just try below steps. 199 | 200 | Steps: 201 | 202 | 1. Use the new sonarqube image in ***docker-compose-\.yml***; 203 | 2. Run `docker-compose -f [PATH_OF_THIS_REPO_ON_YOUR_DISK]/docker-compose-.yml up`; 204 | 3. Wait until sonarqube is up. 205 | 206 | For big SonarQube upgrading, it also requires database upgrading, but this happens automatically. 207 | 208 | 1. After the new SonarQube container is up, open its web page, you'll be redirected to a maintenance page; 209 | 210 | ```console 211 | sonarqube_1 | 2099.12.31 12:00:00 WARN web[o.s.s.p.DatabaseServerCompatibility] Database must be upgraded. Please backup database and browse /setup 212 | sonarqube_1 | 2099.12.31 12:00:00 INFO web[o.s.s.p.Platform] DB needs migration, entering safe mode 213 | ``` 214 | 215 | 2. Open ***http://[YOUR_SONARQUBE_URL]:9000/setup***; 216 | 217 | 3. Click the **Upgrade** button. 218 | 219 | The database upgrade can take several minutes. When the DB migration ends successfully, the page will display "Database is up-to-date", then redirect you to home page. 220 | 221 | ## License 222 | 223 | Copyright (c) 2016-2019 Jing Li. **SonarOnDocker** is released under the GNU Lesser General Public License, Version 3.0. See the [LICENSE](https://github.com/thyrlian/SonarOnDocker/blob/master/LICENSE) file for details. 224 | --------------------------------------------------------------------------------