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
