IDEA 构建docker镜像到开发服务器
centos 7 服务器环境
-
docker 的安装和配置
-
docker 安装参考docker 安装
-
配置阿里云加速器
vi /etc/docker/daemon.json
-
开启docker远程访问
-
配置完成后执行以下命令重启docker服务
systemctl daemon-reload systemcyl restart docker
-
-
docker 仓库的安装docker 仓库的安装有官方提供的 registry以及harbor。本文中使用比较简单的repository.
-
启动一个registry容器,该容器用于提供私有仓库的服务。
docker run -d -p 5000:5000 --restart=always --name docker-registry -v /var/lib/registry:/var/lib/registry registry:latest
-
通过curl -X GET http://127.0.0.1:5000/v2/_catalog 验证 。 初始返回{“repositories”:[]}说明安装成功。
-
IDEA 环境
-
安装 Docker 插件
-
IDEA 中添加配置好的 docker 服务。
hello-word-docker 工程
-
创建一个spring boot web工程hello-word-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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.local</groupId> <artifactId>hello-world-docker</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.7.RELEASE</version> </dependency> </dependencies> <build> <finalName>${project.name}</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.1.7.RELEASE</version> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <target>1.8</target> <source>1.8</source> <encoding>UTF-8</encoding> <skip>true</skip> </configuration> </plugin> </plugins> </build> </project>
-
application.yml
server: port: 8888 spring: application: name: hello-world-docker
-
测试接口
package com.local.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @ProjectName: hello-world-docker * @Author: Qiao * @Description: * @Date: 2020/3/16 20:03 */ @RestController @RequestMapping("/hello") public class Controller { @GetMapping("/docker") public String test() { return "hello world docker"; } }
-
构建当前项目生成jar包
-
在hello-word-docker工程同级目录下创建 Dockerfile 文件
FROM anapsix/alpine-java:8_server-jre_unlimited MAINTAINER 1071635161@qq.com RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime RUN mkdir -p /hello-world-docker WORKDIR /hello-world-docker EXPOSE 8888 ADD ./target/hello-world-docker.jar ./ CMD sleep 120;java -Xms128m -Xmx256m -Djava.security.egd=file:/dev/./urandom -jar hello-world-docker.jar
-
创建并运行Dockerfile
-
点击运行 hello-world-docker构建镜像到开发服务器
验证
- docker images 查看镜像是否存在
- docker run -d --name hello-world-docker --restart always -p 8888:8888 hello-world-docker:latest
- 浏览器访问http:192.168.204.133:8888/hello/docker
结束语
有点累…