SOFABoot packages an application into a Docker image using the same method as Spring Boot. This topic describes how to create a Docker image for a sample SOFABoot application, based on the official Spring Boot documentation.
Prerequisites
Before you run the sample project, you must install Docker. For more information, see the official Docker documentation.
If you use macOS, you might encounter a connection issue during packaging. For a solution, see Port 2375 issue with Docker on Mac.
Procedure
This tutorial describes how to use a Dockerfile to package the JAR file of a sample SOFABoot application into an image.
Create a Dockerfile
Docker uses a file format called a Dockerfile to build application images. Create a Dockerfile for a Spring Boot application:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
// The name of the application JAR package must follow the artifactId-version format.
COPY sofaboot-docker-demo-web-1.0.0.jar /app.jar
ENV JAVA_OPTS=""
# The commercial edition of SOFAStack middleware requires a four-tuple for server-side authentication. Inject the tuple as environment variables during container deployment as shown below. For more information, see the Service Mesh Quick Start document.
# COM_ALIPAY_ENV=shared
# ENV COM_ALIPAY_INSTANCEID=
# ENV COM_ANTCLOUD_ANTVIP_ENDPOINT=
# ENV COM_ANTCLOUD_MW_ACCESS=
# ENV COM_ANTCLOUD_MW_SECRET=
ENTRYPOINT ["sh","-c","java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar"]java:8 is the official Java 8 (JDK 1.8) image from Docker Hub. The FROM instruction uses this base image, which means that Java is already installed in the container. Then, you can use custom commands to run the Spring Boot application:
VOLUME /tmp: Creates the /tmp directory and makes it persistent in the Docker data folder. Spring Boot uses an embedded Tomcat container that uses /tmp as its default working directory.COPY sofaboot-docker-demo-web-1.0.0.jar /app.jar: Copies the application JAR file to/app.jar.ENTRYPOINT: Specifies the default command to execute when the container starts.
Configure the Maven packaging plugin
Add the Docker Maven packaging plugin to the pom.xml file of the app/web module:
Check the local image packaging configuration. If you find outputDirectory or classifier configurations, comment them out or remove them.
<!-- Docker Maven plugin -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<configuration>
<imageName>${project.groupId}/${project.artifactId}:${project.version}</imageName>
<!-- The directory of the Dockerfile -->
<dockerDirectory>src/main/resources/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>Packaging
Execute
mvn clean install -DskipTestsin the project module to install it.Switch to the web module directory and package the image:
- cd app/web - mvn package docker:build -Dmaven.test.skipThe packaging log is as follows:
Step1/6: FROM openjdk:8-jdk-alpine --->3675b9f543c5 Step2/6: VOLUME /tmp --->Using cache ---> c7a81ace946a Step3/6: COPY sofaboot-docker-demo-web-1.0.0.jar app.jar ---> f24e191bc980 Step4/6: RUN sh -c 'touch /app.jar' --->Running in 777a918b3314 Removing intermediate container 777a918b3314 --->183a3067e911 Step5/6: ENV JAVA_OPTS="" --->Running in 70c238ef5b5b Removing intermediate container 70c238ef5b5b ---> a155e72ed7fc Step6/6: ENTRYPOINT ["sh","-c","java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar"] --->Running in c53eaccaf58f Removing intermediate container c53eaccaf58f --->57bb9c53cfe6 ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null} Successfully built 57bb9c53cfe6 Successfully tagged com.alipay.sofa/sofaboot-docker-demo-web:1.0.0Run the
docker imagescommand to view the created image:- docker imagesThe output is as follows:
REPOSITORY TAG IMAGE ID CREATED SIZE com.alipay.sofa/sofaboot-docker-demo-web 1.0.057bb9c53cfe6About a minute ago 105MB