├── mongo-init ├── Dockerfile └── data-import │ ├── customers │ └── customer.json │ └── import.sh ├── src └── main │ ├── resources │ └── application.yml │ └── java │ └── org │ └── davromalc │ └── tutorial │ ├── model │ └── Customer.java │ ├── repository │ └── CustomerRepository.java │ ├── Application.java │ └── controller │ └── CustomerRestController.java ├── Dockerfile ├── .gitignore ├── docker-compose.yml ├── README.adoc └── pom.xml /mongo-init/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM mongo 2 | COPY data-import/ /data-import/ 3 | RUN chmod +x /data-import/import.sh 4 | CMD /data-import/import.sh -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring.data.mongodb: 2 | database: customers # Database name. 3 | uri: mongodb://mongo:27017 # Mongo database URI. Cannot be set with host, port and credentials. -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM openjdk:8-jdk-alpine 2 | ADD target/spring-boot-mongo-docker-1.0.0.jar app.jar 3 | ENV JAVA_OPTS="" 4 | ENTRYPOINT exec java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.sw? 2 | .#* 3 | *# 4 | *~ 5 | .classpath 6 | .project 7 | .settings/ 8 | bin 9 | build 10 | target 11 | dependency-reduced-pom.xml 12 | *.sublime-* 13 | /scratch 14 | .gradle 15 | Guardfile 16 | README.html 17 | *.iml 18 | .idea 19 | -------------------------------------------------------------------------------- /mongo-init/data-import/customers/customer.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "_id" : "6fbe2532-395e-4829-97d3-631dd61e60eb", 4 | "firstName" : "Paul", 5 | "lastName" : "Gascoine" 6 | }, 7 | { 8 | "_id" : "6fbe2532-395e-4829-97d3-631dd61e60zz", 9 | "firstName" : "Alan", 10 | "lastName" : "Shearer" 11 | } 12 | ] 13 | 14 | -------------------------------------------------------------------------------- /src/main/java/org/davromalc/tutorial/model/Customer.java: -------------------------------------------------------------------------------- 1 | package org.davromalc.tutorial.model; 2 | 3 | import org.springframework.data.annotation.Id; 4 | 5 | import lombok.Data; 6 | 7 | 8 | @Data 9 | public class Customer { 10 | 11 | @Id 12 | public String id; 13 | 14 | public String firstName; 15 | public String lastName; 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/org/davromalc/tutorial/repository/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package org.davromalc.tutorial.repository; 2 | 3 | import java.util.List; 4 | 5 | import org.davromalc.tutorial.model.Customer; 6 | import org.springframework.data.mongodb.repository.MongoRepository; 7 | 8 | public interface CustomerRepository extends MongoRepository { 9 | 10 | public Customer findByFirstName(String firstName); 11 | public List findByLastName(String lastName); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/org/davromalc/tutorial/Application.java: -------------------------------------------------------------------------------- 1 | package org.davromalc.tutorial; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; 6 | 7 | @EnableMongoRepositories 8 | @SpringBootApplication 9 | public class Application { 10 | public static void main(String[] args) { 11 | SpringApplication.run(Application.class, args); 12 | } 13 | 14 | 15 | } 16 | -------------------------------------------------------------------------------- /mongo-init/data-import/import.sh: -------------------------------------------------------------------------------- 1 | cd /data-import 2 | pattern="./" 3 | folderpattern="/" 4 | initpattern="data-import" 5 | for D in `find . -type d` 6 | do 7 | folder=${D#$pattern} 8 | echo "Reading folder ${folder}" 9 | ls -1 ${folder}/*.json | sed 's/.json$//' | while read col; do 10 | filename=${col#$folder} 11 | echo "Read folder ${folder#initpattern} and file .${filename}.json" 12 | mongoimport --host mongo --db ${folder#initpattern} --collection ${filename#$folderpattern} --type json --file ${col}.json --jsonArray 13 | done 14 | done 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "2.1" 2 | services: 3 | mongo: 4 | image: mongo:3.2.4 5 | ports: 6 | - 27017:27017 7 | command: --smallfiles 8 | 9 | mongo-init: 10 | build: ./mongo-init 11 | links: 12 | - mongo 13 | 14 | mongo-client: 15 | image: mongoclient/mongoclient 16 | ports: 17 | - 3030:3000 18 | environment: 19 | - MONGOCLIENT_DEFAULT_CONNECTION_URL=mongodb://mongo:27017 20 | links: 21 | - mongo 22 | 23 | # APP *************************************************************************************** 24 | spring-boot-mongo-docker: 25 | image: davromalc/spring-boot-mongo-docker 26 | ports: 27 | - 8080:8080 28 | links: 29 | - mongo 30 | entrypoint: "java -Djava.security.egd=file:/dev/./urandom -jar /app.jar" 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/main/java/org/davromalc/tutorial/controller/CustomerRestController.java: -------------------------------------------------------------------------------- 1 | package org.davromalc.tutorial.controller; 2 | 3 | import java.util.List; 4 | 5 | import org.davromalc.tutorial.model.Customer; 6 | import org.davromalc.tutorial.repository.CustomerRepository; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.web.bind.annotation.RequestBody; 9 | import org.springframework.web.bind.annotation.RequestMapping; 10 | import org.springframework.web.bind.annotation.RequestMethod; 11 | import org.springframework.web.bind.annotation.RestController; 12 | 13 | import lombok.extern.slf4j.Slf4j; 14 | 15 | @RestController 16 | @Slf4j 17 | public class CustomerRestController { 18 | 19 | @Autowired 20 | private CustomerRepository repository; 21 | 22 | @RequestMapping("customer/") 23 | public List findAll(){ 24 | final List customers = repository.findAll(); 25 | log.info("Fetching customers from database {}" , customers); 26 | return customers; 27 | } 28 | 29 | @RequestMapping(value = "customer/" , method = RequestMethod.POST) 30 | public void save(@RequestBody Customer customer){ 31 | log.info("Storing customer in database {}", customer); 32 | repository.save(customer); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /README.adoc: -------------------------------------------------------------------------------- 1 | 2 | :spring_version: current 3 | :toc: 4 | :project_id: spring-boot-mongo-docker 5 | :icons: font 6 | :source-highlighter: prettify 7 | 8 | This guide walks you through the process of building a https://docker.com[Docker] image for running a Spring Boot application with mongo database. 9 | 10 | == What you'll build 11 | 12 | https://docker.com[Docker] is a Linux container management toolkit with a "social" aspect, allowing users to publish container images and consume those published by others. A Docker image is a recipe for running a containerized process, and in this guide we will build one for a simple Spring boot application. 13 | 14 | == What you'll need 15 | :java_version: 1.8 16 | include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/prereq_editor_jdk_buildtools.adoc[] 17 | 18 | 19 | 20 | [[initial]] 21 | == Set up a Spring Boot app 22 | 23 | 24 | 25 | If you want to run with Maven, execute: 26 | 27 | [subs="attributes"] 28 | ---- 29 | ./mvn package && java -jar target/spring-boot-mongo-docker-1.0.0.jar 30 | ---- 31 | 32 | and go to http://localhost:8080/customer/ to see your persisted customers. 33 | 34 | == Containerize It 35 | 36 | If you want to run with Docker, execute: 37 | 38 | [subs="attributes"] 39 | ---- 40 | ./docker-compose up 41 | ---- 42 | 43 | 44 | == Summary 45 | 46 | Congratulations! You've just created a Docker container for a Spring Boot app! Spring Boot apps run on port 8080 inside the container by default and we mapped that to the same port on the host using "-p" on the command line. 47 | 48 | == See Also 49 | 50 | The following guides may also be helpful: 51 | 52 | * https://spring.io/guides/gs/serving-web-content/[Serving Web Content with Spring MVC] 53 | * https://spring.io/guides/gs/spring-boot/[Building an Application with Spring Boot] 54 | 55 | include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/footer.adoc[] 56 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.davromalc 7 | spring-boot-mongo-docker 8 | 1.0.0 9 | 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 1.5.4.RELEASE 14 | 15 | 16 | 17 | 1.8 18 | davromalc 19 | 20 | 21 | 22 | 23 | org.springframework.boot 24 | spring-boot-starter-web 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-data-mongodb 29 | 30 | 31 | org.projectlombok 32 | lombok 33 | 1.16.16 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-test 38 | test 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | com.spotify 52 | dockerfile-maven-plugin 53 | 1.3.4 54 | 55 | ${docker.image.prefix}/${project.artifactId} 56 | 57 | 58 | 59 | default 60 | install 61 | 62 | build 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | --------------------------------------------------------------------------------