Skip to main content

9C4.Use Docker/Starter.sh

trydoforOriginalPracticeDockerOperationAbout 3 min

9C4.Use Docker/Starter.sh

Wings provides two ways to publish and start the app.

  • script - shell-based publishing and management
  • docker - docker-based publishing and management

9C4.1.Script Way

  • release.sh - script to pull, package and push
  • starter.sh - script to start, stop and monitor

The scripts support the <same-name>.env to override the default config.

1a.release.sh

## soft link script with app name
ln -s wings-release.sh winx-admin.sh
## create the same name env file
vi winx-admin.env
## pull source code from git
./winx-admin.sh pull
## build by mvn or web pack
./winx-admin.sh pack
## push the result to app server
./winx-admin.sh push
## for more help
./winx-admin.sh help

1b.starter.sh

## soft link script with app name
ln -s wings-starter.sh winx-admin.sh
## create the same name env file
vi winx-admin.env
## safely start
./winx-admin.sh start
## check status
./winx-admin.sh status
## safely stop
./winx-admin.sh stop
## for more help
./winx-admin.sh help

9C4.2.Docker Way

Docker management is simple and consistent without relying on env and conf of host. The following features/libs are optional and can be removed before building.

  • spring-boot-devtools-*.jar
  • spring-boot-docker-compose-*.jar
  • spring-boot-admin-*.jar
  • spring -actuator*

2a. Layered Build

SpringBoot project after repackage, usually be about 100+M, and the libs more than 95+%. Therefore, in practice, docker is splited into 2 layers, dep and app.

  • docker-dep - all *.jar exlude *-SNAPSHOT.jar
  • docker-app - *-SNAPSHOT.jar and project files (classes/,resources/)

Equivalent to java -Djarmode=layertools -jar target/*.jar extract,

  • dependencies - docker-dep
  • spring-boot-loader - ignored, main-class starts faster then JarLauncher
  • snapshot-dependencies - docker-app
  • application - docker-app

This way, docker-dep compiles once and docker-app compiles every time with a small size.

2b.Build Practices

In order to build Docker, there are roughly three practices,

  • docker.sh - Dockerfile based builds, more flexible
  • jib maven - OCI format, no Dockerfile, optional docker
  • buildpack maven - OCI format, no Dockerfile, requires docker

2c.docker.sh Build

Use wings-docker.sh, keep springboot structure, start it with JarLauncher.

## spring repackage to fatjar
mvn clean package
## unzip fatjar and build docker-dep
wings-docker.sh unzip dep target/winx-admin-3.2.110-SNAPSHOT.jar
wings-docker.sh build dep target/winx-admin-3.2.110-SNAPSHOT.jar
## build docker-app from docker-dep
wings-docker.sh unzip app target/winx-admin-3.2.110-SNAPSHOT.jar
wings-docker.sh build app target/winx-admin-3.2.110-SNAPSHOT.jar
## build docker-all in one step
wings-docker.sh build all target/winx-admin-3.2.110-SNAPSHOT.jar

After build, the layout of /app/ in docker is as follows,

  • BOOT-INF/{classes,lib,...} - code and deps
  • META-INF/{services,spring.components,...} - Conf and Prop
  • org - Spring JarLauncher
  • {conf,data,logs} - VOLUME

The springboot repackage is optimized for libs, for example

  • Remove spring-boot-autoconfigure-processor.jar
    • gen spring-autoconfigure-metadata.properties when compile
  • Remove spring-boot-configuration-processor.jar
    • gen spring-configuration-metadata.json when compile
  • Remove spring-boot-starter-*.jar
    • empty package as springboot convention
  • Add spring-boot-jarmode-layertools.jar
    • to unpack by java -Djarmode=layertools

2d.docker Start

## bridge network
WINGS_DOCKER_NET=wings-app
## create directory and config
mkdir -p {data,conf,logs}
cat > conf/application.properties <<'EOF'
## mysql host
winx.database.host=host.docker.internal:51487
## boot admin host
spring.boot.admin.client.url=http://host.docker.internal:8093
## jdbc debug
logging.level.com.zaxxer.hikari.HikariConfig=DEBUG
EOF
## docker options
WINGS_DOCKER_OPTS=(--network $WINGS_DOCKER_NET -e TZ=Asia/Shanghai -v ./data:/app/data -v ./conf:/app/conf -v ./logs:/app/logs -p 8091:8080)
docker network create --driver bridge $WINGS_DOCKER_NET
## run into docker
docker run -it --rm ${WINGS_DOCKER_OPTS[@]} --user root --entrypoint /bin/bash winx-admin:3.2.110-SNAPSHOT
## start springboot app
docker run -it --rm ${WINGS_DOCKER_OPTS[@]} winx-admin:3.2.110-SNAPSHOT

The above script takes winx-admin as an example and changes 8091 to 8093 if winx-devops. After starting the admin and devops docker containers separately, the following URLs can be accessed to test

2e.Jib Build OCI

Jibopen in new window require docker optional, build OCI format by default

## mvn compile the devops
mvn clean install
## use docker daemon
mvn -P'docker,docker-dep' jib:dockerBuild -Ddocker.to.prefix=fessional/
mvn -P'docker,docker-app' jib:dockerBuild -Ddocker.to.prefix=fessional/ -Ddocker.from.prefix=docker://fessional/
#mvn -P'docker,docker-app' jib:build -Ddocker.to.prefix=fessional/ -Ddocker.from.prefix=fessional/
## use docker registry
mvn -P'docker,docker-dep' jib:dockerBuild -Ddocker.to.prefix=docker.io/fessional/
mvn -P'docker,docker-app' jib:dockerBuild -Ddocker.to.prefix=docker.io/fessional/ -Ddocker.from.prefix=docker.io/fessional/
## build docker-all in one step
mvn -P'docker' jib:dockerBuild -Ddocker.to.prefix=fessional/

Additionally, the two build methods differ in the following ways.

  • jib:dockerBuild - depends on Docker
  • jib:build - no Docker required, pushes to docker hub by default.

After build, the layout of /app/ in docker is as follows,

  • {classes,libs} - code and deps
  • resources/META-INF/spring.components - conf and prop
  • jib-classpath-file - java classpath
  • jib-main-class-file - java main
  • {conf,data,logs} - VOLUME

2f.Jib Start

Same as docker startup, but with professional prefix to diff.

## docker options
WINGS_DOCKER_OPTS=(--network $WINGS_DOCKER_NET -e TZ=Asia/Shanghai -v ./data:/app/data -v ./conf:/app/conf -v ./logs:/app/logs -p 8093:8080)
## run into docker
docker run -it --rm ${WINGS_DOCKER_OPTS[@]} --user root --entrypoint /bin/bash fessional/winx-devops:3.2.110-SNAPSHOT
## start springboot app
docker run -it --rm ${WINGS_DOCKER_OPTS[@]} fessional/winx-devops:3.2.110-SNAPSHOT

2g.buildpack Build

It is necessary to customize the following properties of the CNB according to the actual needs of the project.

  • builder - default paketobuildpacks/builder-jammy-base:latest
  • runImage - default unspecified
  • env - build environment
  • buildpacks - recommended customization

The default build does not split into dep and app. it is only used as an example.

## mvn compile the devops
mvn clean install
## use docker daemon
mvn -P 'image-paketo' spring-boot:build-image

After build, the layout of /workspace/ (hard-code, how to change?) is same as fatjar,

  • BOOT-INF/{classes,lib,...} - code and deps
  • META-INF/{services,spring.components,...} - Conf and Prop
  • org - Spring JarLauncher
  • {conf,data,logs} - VOLUME