首先看一下我的文件目录
konsy@Konsy-MBP kafka-base-producer % tree
.
├── Dockerfile
├── kafka-base-producer.iml
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── kafka
│ │ │ ├── KafkaProducerApplication.java
│ │ │ ├── config
│ │ │ │ └── KafkaConfig.java
│ │ │ └── producer
│ │ │ ├── controller
│ │ │ │ └── KafkaProducerController.java
│ │ │ └── services
│ │ │ └── KafkaProducerSender.java
│ │ └── resources
│ │ ├── application-docker.yml
│ │ ├── application-local.yml
│ │ └── application.yml
│ └── test
│ └── java
└── target
├── classes
│ ├── application-docker.yml
│ ├── application-local.yml
│ ├── application.yml
│ └── kafka
│ ├── KafkaProducerApplication.class
│ ├── config
│ │ └── KafkaConfig.class
│ └── producer
│ ├── controller
│ │ └── KafkaProducerController.class
│ └── services
│ └── KafkaProducerSender.class
├── generated-sources
│ └── annotations
├── generated-test-sources
│ └── test-annotations
├── kafka-base-producer-1.0-SNAPSHOT.jar
├── kafka-base-producer-1.0-SNAPSHOT.jar.original
├── maven-archiver
│ └── pom.properties
├── maven-status
│ └── maven-compiler-plugin
│ ├── compile
│ │ └── default-compile
│ │ ├── createdFiles.lst
│ │ └── inputFiles.lst
│ └── testCompile
│ └── default-testCompile
│ ├── createdFiles.lst
│ └── inputFiles.lst
└── test-classes
30 directories, 24 files
- maven 打包
pom.xml文件增加
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
执行 mvn install
- 创建Dockerfile
# 基础镜像
FROM java:8
# 作者
MAINTAINER konsy
# 把可执行jar包复制到基础镜像的根目录下
ADD ./target/kafka-base-producer-1.0-SNAPSHOT.jar /kafka-base-producer-1.0-SNAPSHOT.jar
# 镜像要暴露的端口,如要使用端口,在执行docker run命令时使用-p生效
EXPOSE 9001
# 在镜像运行为容器后执行的命令
ENTRYPOINT ["java","-jar","/kafka-base-producer-1.0-SNAPSHOT.jar"]
- 打包镜像
#由于我是M1 mbp,所以需要打包成arm64
docker buildx build --platform linux/arm64 -t kafka-base-producer:latest .
- 编写docker compose文件,创建文件
kafka-base-producer.yml
version: '2'
services:
kafka-base-producer:
image: kafka-base-producer:latest
restart: always
hostname: kafka-base-producer
container_name: kafka-base-producer
ports:
- 9001:9001
networks:
docker-net:
ipv4_address: 172.20.10.51 #由于我设置了docker的sub-net为172.20.10.0/16,上一篇文章中有提到,为了能访问部署在docker中的kafka
networks:
docker-net:
external:
name: docker-net
- 部署至docker
docker compose -f ./kafka-base-producer.yml up -d
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)