前言:
docker:容器化管理
maven:支持docker-maven的插件,通过 mvn clean -Dmaven.test.skip package dockerfile:build 打包命令和配置 Dockerfile 直接打出 docker 镜像
docker-compose:服务端容器编排,使得多个容器可进行批量发布等管理操作
一、集成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>
二、编写Dockerfile
在pom.xml同级目录下编写Dockerfile
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"]
三、执行mvn dockerfile打包命令
mvn clean -Dmaven.test.skip package dockerfile:build
四、推送到镜像仓库
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: [镜像版本号]
五、安装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
六、编写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 环境变量
links 链接其他容器
ports 端口映射
expose 暴露端口
volumes 挂载路径
volumes_from 从容器挂载
environment 环境变量
七、docker-compose命令启动容器
docker-compose -f xxx.yaml up -d
docker-compose up -d
更多docker-compose命令: