Linux设置
1.开启端口号.
#开放2375端口
firewall-cmd --permanent --zone=public --add-port=2375/tcp
# 查看端口打开情况
firewall-cmd --permanent --zone=public --list-ports
# 重新加载firewall防火墙
firewall-cmd --reload
2.首先进入Docker的配置文件开发2375端口,这个端口是docker默认的端口,请不要改变他.
vim /usr/lib/systemd/system/docker.service
3. 在ExecStart=/usr/bin/dockerd后面加-H tcp://0.0.0.0:2375
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
4.重新读取配置文件
systemctl daemon-reload
5.重启docker
systemctl restart docker
IDEA设置
1.启动一个普通的容器实例
2.把java项目打包部署到docker上面
现在开始制作容器
- 导入依赖
<?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.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.capgemini</groupId>
<artifactId>docker-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>docker-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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>
<!-- 指定 Dockerfile 路径-->
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<!--jar 包所在的路径 此处配置的 即对应 target 目录-->
<directory>${project.build.directory}</directory>
<!-- 需要包含的 jar包 ,这里对应的是 Dockerfile中添加的文件名 -->
<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>
</project>
-
打包
-
建立Dockerfile文件(注意这个文件没有后缀)
# 基于java镜像
FROM java:8
# linux上面得挂载点为 /tmp
VOLUME /tmp
# 添加一个jar为本项目得jar 第一个是打包得jar 第二个是别名
ADD *.jar app.jar
# 暴露得端口号 application.yml端口号一样
EXPOSE 8989
# 入口点 执行java jvm命令:加快随机数生产过程,运行这个jar包
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
- 在/src/main下创建Dockerfile文件(结构)
- 增加配置
- 运行