Docker环境搭建起来了,本文将介绍如何使用IDEA将一个工程部署到Docker。

 

环境:ECS服务器(CentOS7.4),InteliJ IDEA(本地开发)

 

1. 创建一个springboot工程,引入spring-boot-starter-web

1)pom.xml

<?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.kevin</groupId>
    <artifactId>spring-docker</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-docker</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!--
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.10</version>
                <configuration>
                    <repository>${project.artifactId}-${project.version}</repository>
                </configuration>
            </plugin>
            -->
        </plugins>
    </build>

</project>

2) application.properties

spring.application.name=spring-docker

server.port=8100

 

3) SpringDockerApplication.java

@SpringBootApplication

public class SpringDockerApplication {

    public static void main(String[] args) {

        SpringApplication.run(SpringDockerApplication.class, args);
    }

}

 

4) HelloController.java

package com.kevin.springdocker.controller;


import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;


@RestController

public class HelloController {


    @RequestMapping(value = "/sayHello", method = RequestMethod.GET)

    public String sayHello(){

        return "Hello Kevin";

    }

}

5) 测试

 

 

6)打包mvn clean package

 

7)项目根目录创建Dockerfile文件(Dockerfile-idea)

# 引入openjdk镜像 -alpine对应的jdk文件较小

FROM openjdk:8-jdk-alpine

# tomcat默认临时目录

VOLUME /tmp

# 复制target下jar文件并重命名

COPY target/*.jar spring-docker.jar

# 申明开放端口

EXPOSE 8100

# 启动jar

ENTRYPOINT ["java","-jar","/spring-docker.jar"]

 

2. IDEA 引入 Docker插件

1) File -> Settings -> Plugins输入“Docker”,显示对应的插件,Install安装即可

 

2) File -> Settings -> Build -> Docker,点击+号,输入Docker服务地址,提示连接成功后保存

 

3) 查看Docker控制台,可查看当前镜像和容器

 

4) Run -> Edit Configurations,添加Docker运行配置,选择Docker服务器 -> 选择Dockerfile -> 填写镜像名 -> 填写容器名 ->绑定端口映射,点击OK保存

如果希望build镜像前自动打包代替手动打包,可配置Maven Goal先打包jar

5)选择上述配置并运行, 镜像build成功后,会自动创建一个容器启动服务

 

 

 

6) 访问服务器的服务接口

 

3. 通过TLS访问Docker服务端

 

0)创建证书制作目录

$ mkdir -p /usr/local/ca

$ cd /usr/local/ca

1)生成CA,输入密码

$ openssl genrsa -aes256 -out ca-key.pem 4096

$ openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.pem

2)生成server key和签名证书,需要配置服务器域名或IP

$ openssl genrsa -out server-key.pem 4096

$ openssl req -subj "/CN=xxx.xxx.com" -sha256 -new -key server-key.pem -out server.csr

配置白名单,IP:0.0.0.0表示不限制;注意,如果证书是通过域名则配置DNS:xxx.xxx.com,如果证书是通过IP则配置IP:xxx.xxx.xxx.xxx

$ echo subjectAltName = DNS:xxx.xxx.com,IP:0.0.0.0 >> extfile.cnf

$ echo extendedKeyUsage = serverAuth >> extfile.cnf

$ openssl x509 -req -days 365 -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem -extfile extfile.cnf

3) 生成client key

$ openssl genrsa -out key.pem 4096

$ openssl req -subj '/CN=client' -new -key key.pem -out client.csr

$ echo extendedKeyUsage = clientAuth > extfile-client.cnf

$ openssl x509 -req -days 365 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out cert.pem -extfile extfile-client.cnf

4) 删除无用的文件,授权

$ rm -v client.csr server.csr extfile.cnf extfile-client.cnf

$ chmod -v 0400 ca-key.pem key.pem server-key.pem

$ chmod -v 0444 ca.pem server-cert.pem cert.pem

将服务端证书放到docker配置目录

$ cp server-*.pem /etc/docker/

$ cp ca.pem /etc/docker/

5)修改docker配置,并重启docker

$ vim /usr/lib/systemd/system/docker.service

将ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock改为

ExecStart=/usr/bin/dockerd --tlsverify --tlscacert=/etc/docker/ca.pem --tlscert=/etc/docker/server-cert.pem --tlskey=/etc/docker/server-key.pem -H fd:// -H=0.0.0.0:2376 --containerd=/run/containerd/containerd.sock
$ systemctl daemon-reload

$ systemctl restart docker

6)服务器开放2376端口(由于我的服务器是阿里的ECS,直接配置安全组即可)

7)下载客户端证书(使用FileZilla下载到本地)

 

8)IDEA使用证书访问

删除无CA证书访问的配置,重新加一个Docker配置,使用https://xxx.xxx.com:2376,证书目录选择上一步下载到本地的目录,提示连接成功。

 

 

 

参考:

https://spring.io/guides/topicals/spring-boot-docker/

https://docs.docker.com/engine/security/https/

https://blog.csdn.net/lovexiaotaozi/article/details/82797236

https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

 

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐