Rework Docker support

- Use multi-stage build for the application image
- Utilize connection pool init timeout for waiting on database container startup,
"docker-compose-wait" dependency is no longer required
- Override database host through environment variable - used in docker-compose
- Rename database scripts for explicit ordering (db container loads them alphabetically)
This commit is contained in:
P0nk
2021-04-03 14:59:37 +02:00
parent 8b7e989fed
commit 90687007f5
10 changed files with 44 additions and 20 deletions

3
.dockerignore Normal file
View File

@@ -0,0 +1,3 @@
/docs
/handbook
/tools

1
.gitignore vendored
View File

@@ -2,6 +2,7 @@
.idea/
*.iml
/target
/docker-db-data
# build files

View File

@@ -1,13 +1,20 @@
# Docker support, thanks to xinyifly
# Initial Docker support thanks to xinyifly
FROM openjdk:8u171-jdk-alpine
RUN apk -U add tini
WORKDIR /mnt
#
# Build stage
#
FROM maven:3.6.3-jdk-8 AS build
COPY src /home/app/src
COPY pom.xml /home/app
RUN mvn -f /home/app/pom.xml clean package
#
# Package stage
#
FROM openjdk:8
COPY --from=build /home/app/target/Cosmic.jar /usr/local/lib/Cosmic.jar
COPY ./ ./
RUN sh ./posix-compile.sh
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.6.0/wait /wait
RUN chmod +x /wait
EXPOSE 8484 7575 7576 7577
ENTRYPOINT ["tini", "--"]
CMD /wait && sh ./posix-launch.sh
ENTRYPOINT ["java", "-jar", "/usr/local/lib/Cosmic.jar"]

View File

@@ -162,7 +162,8 @@ server:
USE_THREAD_TRACKER: false #[SEVERE] This deadlock auditing thing will bloat the memory as fast as the time frame one takes to lose track of a raindrop on a tempesting day. Only for debugging purposes.
#Database Configuration
DB_URL: "jdbc:mysql://localhost:3306/cosmic"
DB_URL_FORMAT: "jdbc:mysql://%s:3306/cosmic"
DB_HOST: "localhost"
DB_USER: "cosmic_server"
DB_PASS: "snailshell"
INIT_CONNECTION_POOL_TIMEOUT: 60 # Seconds

View File

@@ -1,20 +1,22 @@
version: '3'
services:
maplestory:
network_mode: "host"
build: .
depends_on:
- db
ports:
- "8484:8484"
- "7575-7577:7575-7577"
environment:
WAIT_HOSTS: localhost:3306
DB_HOST: "db"
db:
network_mode: "host"
image: mysql:5.6
image: mysql:8.0.23
environment:
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
MYSQL_RANDOM_ROOT_PASSWORD: "true"
MYSQL_DATABASE: "cosmic"
MYSQL_USER: "root"
MYSQL_PASSWORD: ""
MYSQL_USER: "cosmic_server"
MYSQL_PASSWORD: "snailshell"
volumes:
- ./docker-db-data:/var/lib/mysql
- ./sql:/docker-entrypoint-initdb.d

View File

@@ -5,7 +5,8 @@ public class ServerConfig {
public boolean USE_THREAD_TRACKER;
//Database Configuration
public String DB_URL;
public String DB_URL_FORMAT;
public String DB_HOST;
public String DB_USER;
public String DB_PASS;
public int INIT_CONNECTION_POOL_TIMEOUT;

View File

@@ -27,10 +27,19 @@ public class DatabaseConnection {
return dataSource.getConnection();
}
private static String getDbUrl() {
// Environment variables override what's defined in the config file
// This feature is used for the Docker support
String hostOverride = System.getenv("DB_HOST");
String host = hostOverride != null ? hostOverride : YamlConfig.config.server.DB_HOST;
String dbUrl = String.format(YamlConfig.config.server.DB_URL_FORMAT, host);
return dbUrl;
}
private static HikariConfig getConfig() {
HikariConfig config = new HikariConfig();
config.setJdbcUrl(YamlConfig.config.server.DB_URL);
config.setJdbcUrl(getDbUrl());
config.setUsername(YamlConfig.config.server.DB_USER);
config.setPassword(YamlConfig.config.server.DB_PASS);