Docker + Maven + Docker-compose

前言:

docker:容器化管理html

maven:支持docker-maven的插件,經過 mvn clean -Dmaven.test.skip package dockerfile:build 打包命令和配置 Dockerfile 直接打出 docker 鏡像java

docker-compose:服務端容器編排,使得多個容器可進行批量發佈等管理操做git

1、集成dockerfile-maven插件

<properties>
<java.version>1.8</java.version>
<guava.version>20.0</guava.version>
<dockerfile-maven-plugin.version>1.3.6</dockerfile-maven-plugin.version>
<docker-client.version>8.10.1</docker-client.version>
<!-- Docker鏡像倉庫 -->
<docker.registry>xxxx</docker.registry>
<docker.namespace>xxx</docker.namespace>
<docker.repo>xx</docker.repo>

<java.lib>${project.basedir}/src/main/resources/lib</java.lib>
</properties>

<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!--開啓本地引入JAR包--> <configuration> <includeSystemScope>true</includeSystemScope> </configuration> </plugin> <plugin>
          <!-- 這裏引入dockerfile-maven插件 --> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>${dockerfile-maven-plugin.version}</version> <dependencies> <dependency> <groupId>com.spotify</groupId> <artifactId>docker-client</artifactId> <version>${docker-client.version}</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>${guava.version}</version> </dependency> </dependencies> <configuration> <tag>${project.artifactId}-latest</tag> <repository>${docker.registry}/${docker.namespace}/${docker.repo}</repository> <buildArgs> <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE> </buildArgs> </configuration> </plugin> </plugins> </build>

2、編寫Dockerfile

在pom.xml同級目錄下編寫Dockerfilegithub

FROM java:8 VOLUME /tmp ARG JAR_FILE ADD ${JAR_FILE} app.jar ENTRYPOINT ["java","-Xmx192m", "-XX:+UseConcMarkSweepGC", "-Djava.security.egd=file:/dev/./urandom","-XX:+HeapDumpOnOutOfMemoryError","-XX:HeapDumpPath=/mnt/logs/heapdump_test.hprof","-jar","/test-1.0.0.RELEASE.jar"]

3、執行mvn dockerfile打包命令

mvn clean -Dmaven.test.skip package dockerfile:build

4、推送到鏡像倉庫

docker login --username=root registry.cn-xxx.aliyuncs.com docker tag [鏡像ID] registry.cn-xxx.aliyuncs.com/namespace/repo: [鏡像版本號] docker push registry.cn-chengdu.aliyuncs.com/kidcloud/kidcloud_repo: [鏡像版本號]

5、安裝docker-compose

curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose chmod +x /usr/local/bin/docker-compose ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose docker-compose –version

6、編寫compose的yaml文件

version: '1' services: demo: image: ${私服地址}/${命名空間}/${倉庫名}:${鏡像名稱版本號} environment: - server.port=9001
     - eureka.client.serviceUrl.defaultZone=http://discovery:8761/eureka/
     - JAVA_OPTS='-Xmx512m -Xms512m' ports: - "9001:9001" networks: - demoNetwork volumes: - /mnt/logs:/mnt/logs external_links: - eureka-server:discovery networks: demoNetwork: external: true
image 使用的鏡像
external_links 連接yaml編排以外的docker容器
links 連接其餘容器
ports 端口映射
expose 暴露端口
volumes 掛載路徑
volumes_from 從容器掛載
environment 環境變量

7、docker-compose命令啓動容器

docker-compose -f xxx.yaml up -d
docker-compose up -d

更多docker-compose命令:spring

http://www.javashuo.com/article/p-fhgclcrf-ce.htmldocker

相關文章
相關標籤/搜索