Docker部署SpringBoot

方式1(Dockerfile + JAR本地构建)

  • Dockerfile文件【src/main/docker/Dockerfile】
# Docker image for springboot file run
# 基础java镜像,需要提前安装:docker pull java:8
FROM java:8
# 作者
MAINTAINER lixing <[email protected]>
# 解决中文乱码
ENV LANG C.UTF-8:
# 设置alpine时区
ENV TIMEZONE Asia/Shanghai
# 在主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp
VOLUME /tmp
# 将Demo1-20211208.jar包添加到容器中并更名为Demo1.jar
ADD Demo1-20211208.jar Demo1.jar
# 暴露容器运行时的 8088 监听端口给外部
EXPOSE 8088/tcp
# 启动容器时执行的 Shell 命令
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/Demo1.jar"]
  • pom.xml文件
<build>
    <finalName>${project.artifactId}-${project.parent.version}finalName>
    <resources>
        
        <resource>
            <directory>src/main/resourcesdirectory>
            <filtering>truefiltering>
        resource>
        
        <resource>
            <directory>src/main/javadirectory>
            <includes>
                <include>**/*.xmlinclude>
            includes>
            <filtering>falsefiltering>
        resource>
    resources>
    <plugins>
        <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
            <configuration>
                
                <excludes>
                    <exclude>
                        <groupId>org.projectlombokgroupId>
                        <artifactId>lombokartifactId>
                    exclude>
                excludes>
                
                <fork>truefork>
                <addResources>trueaddResources>
            configuration>
        plugin>
        
        <plugin>
            <artifactId>maven-resources-pluginartifactId>
            <executions>
                <execution>
                    <id>copy-resourcesid>
                    <phase>validatephase>
                    <goals>
                        <goal>copy-resourcesgoal>
                    goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/Demo1outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/dockerdirectory>
                                <filtering>truefiltering>
                            resource>
                        resources>
                    configuration>
                execution>
            executions>
        plugin>
        
        <plugin>
            <groupId>org.apache.maven.pluginsgroupId>
            <artifactId>maven-antrun-pluginartifactId>
            <executions>
                <execution>
                    <phase>packagephase>
                    <goals>
                        <goal>rungoal>
                    goals>
                    <configuration>
                        <tasks> 
                            <copy todir="${project.build.directory}/Demo1">
                                
                                <fileset dir="${project.build.directory}">
                                    
                                    <include name="*.jar"/>
                                fileset>
                            copy>
                        tasks>
                    configuration>
                execution>
            executions>
        plugin>
    plugins>
build>
  • 制作镜像
# 将/target/Demo1文件夹移动到指定目录下进行部署,详细构建参数:https://www.runoob.com/docker/docker-build-command.html
cd ../Demo1 && docker build -t 镜像名称(小写):标签名称 .
# 查看docker镜像
docker images

Docker部署SpringBoot_第1张图片
Docker部署SpringBoot_第2张图片

  • 启动容器
# 启动容器
docker run -itd --name demo1[容器名称] -p 8088:8088 demo1[镜像名称]
# 查询日志
docker logs 容器id

在这里插入图片描述
Docker部署SpringBoot_第3张图片

你可能感兴趣的:(容器运行时,docker,java,运维)