Skip to content

Commit

Permalink
Add MySQL support with Docker Compose #24
Browse files Browse the repository at this point in the history
  • Loading branch information
arey committed Mar 1, 2019
1 parent 9a565ab commit b7e45bb
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 14 deletions.
24 changes: 24 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: '2'
services:
mysql-petclinic:
image: mysql:5.7
container_name: mysql-petclinic
ports:
- 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=petclinic
- MYSQL_DATABASE=petclinic
volumes:
- "./conf.d:/etc/mysql/conf.d:ro"


spring-petclinic:
image: arey/springboot-petclinic
container_name: spring-petclinic
mem_limit: 512M
depends_on:
- mysql-petclinic
ports:
- 8080:8080
environment:
- SPRING_PROFILES_ACTIVE=mysql,prod
4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<version>1.2.0</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<serverId>docker-hub</serverId>
<registryUrl>https://index.docker.io/v1/</registryUrl>
<resources>
<resource>
<targetPath>/</targetPath>
Expand Down
62 changes: 50 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

## Running petclinic locally
```
git clone https://github.com/spring-petclinic/spring-petclinic-angularjs.git
cd spring-petclinic-angularjs
./mvnw clean install
cd spring-petclinic-server
../mvnw spring-boot:run
git clone https://github.com/spring-petclinic/spring-petclinic-angularjs.git
cd spring-petclinic-angularjs
./mvnw clean install
cd spring-petclinic-server
../mvnw spring-boot:run
```

You can then access petclinic here: http://localhost:8080/
Expand All @@ -23,32 +23,70 @@ Our issue tracker is available here: https://github.com/spring-petclinic/spring-

In its default configuration, Petclinic uses an in-memory database (HSQLDB) which gets populated at startup with data.
A similar setups is provided for MySql in case a persistent database configuration is needed.
To run petclinic locally using MySQL database, it is needed to change profile defined in application.properties file.
To run petclinic locally using MySQL database, it is needed to change profile defined in the application.properties` file.

For MySQL database, it is needed to switch profile. There is two ways:

1. Update application properties: open the `application.properties` file, then change the value `hsqldb` to `mysql`
2. Use a Spring Boot JVM parameter: simply start the JVM with the `-Dspring.profiles.active=mysql.prod` parameter.


Before do this, would be good to check properties defined in `application-mysql.properties` file.
Before do this, it would be good to change JDBC url properties defined in the `application-mysql.properties` file:

```
spring.datasource.url = jdbc:mysql://localhost:3306/petclinic?useUnicode=true
spring.datasource.username=pc
spring.datasource.username=root
spring.datasource.password=petclinic
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database=MYSQL
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=none
```

The `localhost` host should be set for a MySQL dabase instance started on your local machine.
You may also start a MySql database with docker:

```
docker run --name mysql-petclinic -e MYSQL_ROOT_PASSWORD=petclinic -e MYSQL_DATABASE=petclinic -p 3306:3306 mysql:5.7
```


## Docker

### Run an image

To run a Docker image of Petclinic with its embedded HSQL database, you may

```
docker run -p 8080:8080 -t --name springboot-petclinic arey/springboot-petclinic
```

If you want to use MySQL, you first have to change the `spring.datasource.url` declared in the `application-mysql.properties` file.
You have to rebuild the image (see next section).
Then you could activated the `mysql` profile:

```
docker run -e "SPRING_PROFILES_ACTIVE=mysql,prod" -p 8080:8080 -t --name springboot-petclinic arey/springboot-petclinic
```

### Use Docker Compose

The simplest way is to use docker-compose

```
docker-compose up
```

### Build an image

To rebuild a Docker image on your device:
```
./mvnw clean install
cd spring-petclinic-server
mvn clean package docker:build
```

To publish a new image into Docker Hub:
```
mvn clean package docker:build -DpushImageTag
```

## Working with Petclinic in Eclipse/STS

### prerequisites
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ spring.datasource.data=classpath:db/mysql/data.sql

# MySQL config start
#----------------------------------------------------------------
spring.datasource.url = jdbc:mysql://localhost:3306/petclinic?useUnicode=true
# Choose the MySQL database host and port
# Local development with a MySQL docker image started on your machine
#spring.datasource.url = jdbc:mysql://localhost:3306/petclinic?useUnicode=true
# Use the mysql-petclinic host references by the docker-compose.yml
spring.datasource.url = jdbc:mysql://mysql-petclinic:3306/petclinic?useUnicode=true
spring.datasource.username=root
spring.datasource.password=petclinic
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Expand Down

0 comments on commit b7e45bb

Please sign in to comment.