1
0
Fork 0
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:
Anca Iordache 2020-03-16 17:23:59 +01:00
parent 0c6fcde001
commit f1e4cca535
262 changed files with 0 additions and 0 deletions

70
sparkjava-mysql/README.md Normal file
View file

@ -0,0 +1,70 @@
## Compose sample application
### Java Spark application with MySQL 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: mysql:5.7
...
```
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 "sparkjava-mysql_default" with the default driver
Building backend
...
Successfully tagged sparkjava-mysql_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 sparkjava-mysql_db_1 ... done
Creating sparkjava-mysql_backend_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
ee1e4f05d9f6 sparkjava-mysql_backend "/bin/sh -c 'java -j…" 44 seconds ago Up 43 seconds 0.0.0.0:80->8080/tcp sparkjava-mysql_backend_1
716025ddf65b mysql:5.7 "docker-entrypoint.s…" 44 seconds ago Up 43 seconds 3306/tcp, 33060/tcp sparkjava-mysql_db_1
```
After the application starts, run:
```
$ curl localhost:80
["Blog post #0","Blog post #1","Blog post #2","Blog post #3","Blog post #4"]
```
Stop and remove the containers
```
$ docker-compose down
Stopping sparkjava-mysql_backend_1 ... done
Stopping sparkjava-mysql_db_1 ... done
Removing sparkjava-mysql_backend_1 ... done
Removing sparkjava-mysql_db_1 ... done
Removing network sparkjava-mysql_default
```

View file

@ -0,0 +1,10 @@
FROM maven:3.5-jdk-8-alpine AS build
COPY pom.xml .
RUN mvn --batch-mode dependency:resolve
COPY src/ src
RUN mvn --batch-mode clean compile assembly:single
FROM openjdk:8-jre-alpine3.7
EXPOSE 8080
COPY --from=build target/app.jar /app.jar
CMD java -jar /app.jar

59
sparkjava-mysql/backend/pom.xml Executable file
View file

@ -0,0 +1,59 @@
<?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>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>app</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.4</version>
</dependency>
</dependencies>
</project>

View file

@ -0,0 +1,78 @@
import com.google.gson.Gson;
import com.mysql.cj.jdbc.exceptions.CommunicationsException;
import static spark.Spark.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class App {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
prepare();
port(8080);
get("/", (req, res) -> new Gson().toJson(titles()));
}
private static List<String> titles() {
try (Connection conn = connect()) {
List<String> titles = new ArrayList<>();
try (Statement stmt = conn.createStatement()) {
try (ResultSet rs = stmt.executeQuery("SELECT title FROM blog")) {
while (rs.next()) {
titles.add(rs.getString("title"));
}
}
}
return titles;
} catch (Exception ex) {
return new ArrayList<>();
}
}
public static void prepare() throws Exception {
try (Connection conn = connect()) {
recreateTable(conn);
insertData(conn);
}
}
private static void insertData(Connection conn) throws SQLException {
for (int i = 0; i < 5; i++) {
try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO blog (title) VALUES (?);")) {
stmt.setString(1, "Blog post #" + i);
stmt.execute();
}
}
}
private static void recreateTable(Connection conn) throws SQLException {
try (Statement stmt = conn.createStatement()) {
stmt.execute("DROP TABLE IF EXISTS blog");
stmt.execute("CREATE TABLE IF NOT EXISTS blog (id int NOT NULL AUTO_INCREMENT, title varchar(255), PRIMARY KEY (id))");
}
}
private static Connection connect() throws Exception {
for (int i = 0; i < 60; i++) {
try {
return DriverManager.getConnection("jdbc:mysql://db/example?useSSL=false", "root", Files.lines(Paths.get("/run/secrets/db-password")).findFirst().get());
} catch (CommunicationsException ex) {
Thread.sleep(1000L);
continue;
} catch (Exception ex) {
throw ex;
}
}
throw new RuntimeException("Unable to connect to MySQL");
}
}

View file

@ -0,0 +1 @@
db-vp2lf

View file

@ -0,0 +1,23 @@
version: "3.7"
services:
backend:
build: backend
ports:
- 80:8080
secrets:
- db-password
db:
environment:
MYSQL_DATABASE: example
MYSQL_ROOT_PASSWORD_FILE: /run/secrets/db-password
image: mysql:5.7
restart: always
secrets:
- db-password
volumes:
- db-data:/var/lib/mysql
volumes:
db-data: {}
secrets:
db-password:
file: db/password.txt