1.按照创建server的方式创建一个项目,可以参照https://www.cnblogs.com/SakerLiu/p/9743577.html
2.在pom.xml文件中添加插件,代码如下所示
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- tag::plugin[] --> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> <configuration> <imageName>lemon/test</imageName> <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> <!-- end::plugin[] --> </plugins> </build>
3.在main文件夹下新建docker文件夹,在下面创建Dockerfile文件,内容如下
FROM frolvlad/alpine-oraclejdk8:slim VOLUME /tmp ADD demo_server-0.0.1-SNAPSHOT.jar app.jar #RUN bash -c ‘touch /app.jar‘ ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] EXPOSE 8801
4.在主程序文件中添加如下内容
package com.example.demo_server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class DemoServerApplication { public static void main(String[] args) { SpringApplication.run(DemoServerApplication.class, args); } }
5.在application.yml文件中添加如下内容
server: port: 8801 eureka: instance: prefer-ip-address: true client: registerWithEureka: false fetchRegistry: false
6..本文采用maven编译,环境为Ubuntu16.04,在文件夹下输入下面的指令进行编译
mvn clean package docker:build
7.最后输出成功信息,并运行docker测试,首先查看镜像
docker images
然后执行下面的命令
docker run lemon/test -d -p 8801:8801
访问localhost:8801,出现下图表示正常运行
###########################容易出现的问题#####################################
权限不足,输入下面的命令尝试解决
vim ~/.bashrc #输入 export DOCKER_HOST=tcp://192.168.5.153:2375
或者输入下面
vim /lib/systemd/system/docker.service #输入 ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
原文地址:https://www.cnblogs.com/SakerLiu/p/9746023.html
时间: 2024-10-08 06:25:20