Docker開啟遠(yuǎn)程訪問
- linux配置開啟遠(yuǎn)程訪問
[root@izwz9eftauv7x69f5jvi96z docker]# vim /lib/systemd/system/docker.service
#修改ExecStart這行
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
#重新加載配置文件
[root@izwz9eftauv7x69f5jvi96z docker]# systemctl daemon-reload
#重啟服務(wù)
[root@izwz9eftauv7x69f5jvi96z docker]# systemctl restart docker.service
#查看端口是否開啟
[root@izwz9eftauv7x69f5jvi96z docker]# netstat -nlpt
#直接curl看是否生效
[root@izwz9eftauv7x69f5jvi96z docker]# curl http://127.0.0.1:2375/info
-
windows配置開啟遠(yuǎn)程訪問
使用管理員打開cmd,開啟端口防火墻
netsh advfirewall firewall add rule name="docker_daemon" dir=in action=allow protocol=TCP localport=2375
Intellij IDEA安裝Docker插件
打開idea戳护,從File->Setting->Plugins->Instal JetBrains plugin進(jìn)入插件安裝界面抄谐,搜索docker鸵隧,可以看到Docker integration,點(diǎn)擊有弊案的Install按鈕進(jìn)行安裝碳抄,安裝完成后重啟idea。
重啟后配置docker犁苏,連接到遠(yuǎn)程docker服務(wù)。從File->Settting->Build,Execution,Deployment->Docker打開配置界面扩所。
Spring Boot服務(wù)docker部署
新建一個(gè)spring boot工程围详,編寫測試接口
修改pom文件,添加properties,添加plugin
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<java.version>1.8</java.version>
<docker.image.prefix>bozai</docker.image.prefix>
</properties>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<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>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory></dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>
配置Dockerfile文件助赞,在工程根目錄下新建Dockerfile文件
內(nèi)容如下:
FROM java:8
VOLUME /tmp
COPY target/demo-0.0.1-SNAPSHOT.jar demo.jar
RUN bash -c "touch /demo.jar"
EXPOSE 8080
ENTRYPOINT ["java","-jar","demo.jar"]
創(chuàng)建Docker鏡像
將工程打包买羞,在idea Terminal中執(zhí)行mvn clean package命令進(jìn)行編譯打包,打包后會(huì)在target目錄下生成jar包雹食。生成jar包后畜普,可以在本地啟動(dòng)服務(wù),進(jìn)行測試群叶。測試后吃挑,配置docker鏡像制件命令,從Run->Edit Configration進(jìn)入配置界面街立。
點(diǎn)擊Docker舶衬,再點(diǎn)擊+號(hào),添加一個(gè)docker file命令赎离,輸入Name逛犹,選擇Server,選擇Dockerfile文件,輸入鏡像tag梁剔,完成配置虽画。
完成后執(zhí)行。
執(zhí)行成功后荣病,可以看到遠(yuǎn)程docker上看到這個(gè)鏡像狸捕。
打開瀏覽器訪問
參考文章:
https://my.oschina.net/wuweixiang/blog/2874064
https://blog.csdn.net/sealir/article/details/81200662