您好,登錄后才能下訂單哦!
一、開發(fā)前準(zhǔn)備
1. Docker的安裝可以參考https://docs.docker.com/install/
2. 配置docker遠(yuǎn)程連接端口
vi /usr/lib/systemd/system/docker.service
找到 ExecStart,在最后面添加 -H tcp://0.0.0.0:2375,如下圖所示
3. 重啟docker
systemctl daemon-reload
systemctl start docker
4. 開放端口
firewall-cmd --zone=public --add-port=2375/tcp --permanent
5. Idea安裝插件,重啟
6. 連接遠(yuǎn)程docker
(1) 編輯配置
(2) 填遠(yuǎn)程docker地址
(3) 連接成功,會列出遠(yuǎn)程docker容器和鏡像
二、新建項目
1. 創(chuàng)建springboot項目
項目結(jié)構(gòu)圖
(1) 配置pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>docker-demo</groupId> <artifactId>com.demo</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath /> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <docker.image.prefix>com.demo</docker.image.prefix> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>docker-maven-plugin</artifactId> <version>1.0.0</version> <configuration> <dockerDirectory>src/main/docker</dockerDirectory> <resources> <resource> <targetPath>/</targetPath> <directory>${project.build.directory}</directory> <include>${project.build.finalName}.jar</include> </resource> </resources> </configuration> </plugin> <plugin> <artifactId>maven-antrun-plugin</artifactId> <executions> <execution> <phase>package</phase> <configuration> <tasks> <copy todir="src/main/docker" file="target/${project.artifactId}-${project.version}.${project.packaging}"></copy> </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> </project>
(2) 在src/main目錄下創(chuàng)建docker目錄,并創(chuàng)建Dockerfile文件
FROM openjdk:8-jdk-alpine ADD *.jar app.jar ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
(3) 在resource目錄下創(chuàng)建application.properties文件
logging.config=classpath:logback.xml logging.path=/home/developer/app/logs/ server.port=8990
(4) 創(chuàng)建DockerApplication文件
@SpringBootApplication public class DockerApplication { public static void main(String[] args) { SpringApplication.run(DockerApplication.class, args); } }
(5) 創(chuàng)建DockerController文件
@RestController public class DockerController { static Log log = LogFactory.getLog(DockerController.class); @RequestMapping("/") public String index() { log.info("Hello Docker!"); return "Hello Docker!"; } }
(6) 增加配置
命令解釋
(7) Maven打包
(8) 運(yùn)行
先pull基礎(chǔ)鏡像,然后再打包鏡像,并將鏡像部署到遠(yuǎn)程docker運(yùn)行
這里我們可以看到鏡像名稱為docker-demo:1.1,docker容器為docker-server
(9) 運(yùn)行成功
(10) 瀏覽器訪問
(11) 日志查看
自此通過idea 部署springboot項目到docker成功!難以想象,部署一個Javaweb項目竟然如此簡單方便!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。