mirror of
https://github.com/docker/awesome-compose.git
synced 2025-04-19 07:18:06 +02:00
Move all samples to the root dir
Signed-off-by: Anca Iordache <anca.iordache@docker.com>
This commit is contained in:
parent
0c6fcde001
commit
f1e4cca535
262 changed files with 0 additions and 0 deletions
80
spring-postgres/README.md
Normal file
80
spring-postgres/README.md
Normal file
|
@ -0,0 +1,80 @@
|
|||
## Compose sample application
|
||||
### Java application with Spring framework and a Postgres database
|
||||
|
||||
Project structure:
|
||||
```
|
||||
.
|
||||
├── backend
|
||||
│ ├── Dockerfile
|
||||
│ └── ...
|
||||
├── db
|
||||
│ └── password.txt
|
||||
├── docker-compose.yaml
|
||||
└── README.md
|
||||
|
||||
```
|
||||
|
||||
[_docker-compose.yaml_](docker-compose.yaml)
|
||||
```
|
||||
version: "3.7"
|
||||
services:
|
||||
backend:
|
||||
build: backend
|
||||
ports:
|
||||
- 80:8080
|
||||
db:
|
||||
image: postgres
|
||||
...
|
||||
```
|
||||
The compose file defines an application with two services `backend` and `db`.
|
||||
When deploying the application, docker-compose maps port 8080 of the backend service container to port 80 of the host as specified in the file.
|
||||
Make sure port 80 on the host is not already being in use.
|
||||
|
||||
## Deploy with docker-compose
|
||||
|
||||
```
|
||||
$ docker-compose up -d
|
||||
Creating network "spring-postgres_default" with the default driver
|
||||
Building backend
|
||||
Step 1/11 : FROM maven:3.5-jdk-9 AS build
|
||||
3.5-jdk-9: Pulling from library/maven
|
||||
...
|
||||
Successfully tagged spring-postgres_backend:latest
|
||||
WARNING: Image for service backend was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
|
||||
Creating spring-postgres_backend_1 ... done
|
||||
Creating spring-postgres_db_1 ... done
|
||||
```
|
||||
|
||||
## Expected result
|
||||
|
||||
Listing containers must show two containers running and the port mapping as below:
|
||||
```
|
||||
$ docker ps
|
||||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
||||
56236f640eaa postgres "docker-entrypoint.s…" 29 seconds ago Up 28 seconds 5432/tcp spring-postgres_db_1
|
||||
6e69472dc2c0 spring-postgres_backend "java -Djava.securit…" 29 seconds ago Up 28 seconds 0.0.0.0:80->8080/tcp spring-postgres_backend_1
|
||||
```
|
||||
|
||||
After the application starts, navigate to `http://localhost:80` in your web browse or run:
|
||||
```
|
||||
$ curl localhost:80
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<title>Getting Started: Serving Web Content</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
</head>
|
||||
<body>
|
||||
<p>Hello from Docker!</p>
|
||||
</body>
|
||||
```
|
||||
|
||||
Stop and remove the containers
|
||||
```
|
||||
$ docker-compose down
|
||||
Stopping spring-postgres_db_1 ... done
|
||||
Stopping spring-postgres_backend_1 ... done
|
||||
Removing spring-postgres_db_1 ... done
|
||||
Removing spring-postgres_backend_1 ... done
|
||||
Removing network spring-postgres_default
|
||||
```
|
20
spring-postgres/backend/Dockerfile
Executable file
20
spring-postgres/backend/Dockerfile
Executable file
|
@ -0,0 +1,20 @@
|
|||
FROM maven:3.6.3-jdk-11 AS builder
|
||||
WORKDIR /workdir/server
|
||||
COPY pom.xml /workdir/server/pom.xml
|
||||
RUN mvn dependency:go-offline
|
||||
|
||||
COPY src /workdir/server/src
|
||||
RUN mvn install
|
||||
RUN mkdir -p target/depency
|
||||
WORKDIR /workdir/server/target/dependency
|
||||
RUN jar -xf ../*.jar
|
||||
|
||||
FROM openjdk:11-jre-slim
|
||||
|
||||
EXPOSE 8080
|
||||
VOLUME /tmp
|
||||
ARG DEPENDENCY=/workdir/server/target/dependency
|
||||
COPY --from=builder ${DEPENDENCY}/BOOT-INF/lib /app/lib
|
||||
COPY --from=builder ${DEPENDENCY}/META-INF /app/META-INF
|
||||
COPY --from=builder ${DEPENDENCY}/BOOT-INF/classes /app
|
||||
ENTRYPOINT ["java","-cp","app:app/lib/*","com.company.project.Application"]
|
80
spring-postgres/backend/pom.xml
Executable file
80
spring-postgres/backend/pom.xml
Executable file
|
@ -0,0 +1,80 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.company</groupId>
|
||||
<artifactId>project</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>New App</name>
|
||||
<description>My new SpringBoot app</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.5.RELEASE</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>11</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-freemarker</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-jersey</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.session</groupId>
|
||||
<artifactId>spring-session-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<scope>runtime</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
16
spring-postgres/backend/src/main/java/com/company/project/Application.java
Executable file
16
spring-postgres/backend/src/main/java/com/company/project/Application.java
Executable file
|
@ -0,0 +1,16 @@
|
|||
package com.company.project;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan(basePackages = {"com.company.project"})
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.company.project.controllers;
|
||||
|
||||
import com.company.project.entity.Greeting;
|
||||
import com.company.project.repository.GreetingRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@Controller
|
||||
public class HomeController {
|
||||
|
||||
@Autowired
|
||||
private GreetingRepository repository;
|
||||
|
||||
@GetMapping("/")
|
||||
public String showHome(String name, Model model) {
|
||||
Greeting dockerGreeting = repository.findById(1).orElse(new Greeting("Not Found 😕"));
|
||||
model = model.addAttribute("name", dockerGreeting.getName());
|
||||
return "home";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.company.project.entity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "GREETINGS")
|
||||
public class Greeting {
|
||||
|
||||
@Id
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public Greeting() {
|
||||
}
|
||||
|
||||
public Greeting(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Greeting(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Greeting greeting = (Greeting) o;
|
||||
|
||||
return name.equals(greeting.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.company.project.repository;
|
||||
|
||||
import com.company.project.entity.Greeting;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface GreetingRepository extends CrudRepository<Greeting, Integer> {
|
||||
}
|
11
spring-postgres/backend/src/main/resources/application.properties
Executable file
11
spring-postgres/backend/src/main/resources/application.properties
Executable file
|
@ -0,0 +1,11 @@
|
|||
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
|
||||
spring.jpa.hibernate.ddl-auto=none
|
||||
spring.jpa.hibernate.show-sql=true
|
||||
|
||||
spring.datasource.url=jdbc:postgresql://db:5432/${POSTGRES_DB}
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=${POSTGRES_PASSWORD:db-wrz2z}
|
||||
spring.datasource.initialization-mode=always
|
||||
spring.datasource.initialize=true
|
||||
spring.datasource.schema=classpath:/schema.sql
|
||||
spring.datasource.continue-on-error=true
|
1
spring-postgres/backend/src/main/resources/data.sql
Normal file
1
spring-postgres/backend/src/main/resources/data.sql
Normal file
|
@ -0,0 +1 @@
|
|||
INSERT INTO GREETINGS(name) values ('Docker');
|
4
spring-postgres/backend/src/main/resources/schema.sql
Normal file
4
spring-postgres/backend/src/main/resources/schema.sql
Normal file
|
@ -0,0 +1,4 @@
|
|||
CREATE TABLE IF NOT EXISTS GREETINGS (
|
||||
id serial PRIMARY KEY,
|
||||
name varchar(50) NOT NULL
|
||||
);
|
9
spring-postgres/backend/src/main/resources/templates/home.ftlh
Executable file
9
spring-postgres/backend/src/main/resources/templates/home.ftlh
Executable file
|
@ -0,0 +1,9 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<title>Getting Started: Serving Web Content</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
</head>
|
||||
<body>
|
||||
<p>Hello from ${name}!</p>
|
||||
</body>
|
1
spring-postgres/db/password.txt
Normal file
1
spring-postgres/db/password.txt
Normal file
|
@ -0,0 +1 @@
|
|||
db-wrz2z
|
29
spring-postgres/docker-compose.yaml
Normal file
29
spring-postgres/docker-compose.yaml
Normal file
|
@ -0,0 +1,29 @@
|
|||
version: "3.7"
|
||||
services:
|
||||
backend:
|
||||
build: backend
|
||||
ports:
|
||||
- 80:8080
|
||||
environment:
|
||||
POSTGRES_DB: example
|
||||
networks:
|
||||
- spring-postges
|
||||
db:
|
||||
environment:
|
||||
POSTGRES_DB: example
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/db-password
|
||||
image: postgres
|
||||
restart: always
|
||||
secrets:
|
||||
- db-password
|
||||
volumes:
|
||||
- db-data:/var/lib/postgresql/data
|
||||
networks:
|
||||
- spring-postges
|
||||
volumes:
|
||||
db-data: {}
|
||||
secrets:
|
||||
db-password:
|
||||
file: db/password.txt
|
||||
networks:
|
||||
spring-postges: {}
|
Loading…
Add table
Add a link
Reference in a new issue