zoukankan      html  css  js  c++  java
  • docker-java的使用

    1. docker java 的api需要证书的认证

    在/home/hett文件下创建certs证书

    生成服务器私钥,命令如下:

    $openssl genrsa -out server-key.pem 4096

    再用私钥生成服务器公钥请求文件,也就是证书,命令如下:

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

    这里的192.168.99.101同样是Docker所在服务器的IP,你不要照抄,用自己的Docker服务器替换上去。

    root@ros-OptiPlex-3050:~# cd /home/hett/certs/
    root@ros-OptiPlex-3050:/home/hett/certs# ll
    total 24
    drwxr-xr-x  2 root root 4096 9月   3 11:14 ./
    drwxr-xr-x 18 hett hett 4096 9月  13 10:51 ../
    -rw-r--r--  1 root root 3326 9月   3 11:10 ca-key.pem
    -rw-r--r--  1 root root 2090 9月   3 11:11 ca.pem
    -rw-r--r--  1 root root 1590 9月   3 11:14 server.csr
    -rw-r--r--  1 root root 3243 9月   3 11:14 server-key.pem
    root@ros-OptiPlex-3050:/home/hett/certs# openssl x509 -req -days 365 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out cert.pem -extfile extfile.cnferror loading the config file 'extfile.cnf'
    root@ros-OptiPlex-3050:/home/hett/certs# ll
    total 24
    drwxr-xr-x  2 root root 4096 9月   3 11:14 ./
    drwxr-xr-x 18 hett hett 4096 9月  13 10:51 ../
    -rw-r--r--  1 root root 3326 9月   3 11:10 ca-key.pem
    -rw-r--r--  1 root root 2090 9月   3 11:11 ca.pem
    -rw-r--r--  1 root root 1590 9月   3 11:14 server.csr
    -rw-r--r--  1 root root 3243 9月   3 11:14 server-key.pem
    root@ros-OptiPlex-3050:/home/hett/certs# nano extfile.cnf

    root@ros-OptiPlex-3050:/home/hett/certs# cat extfile.cnf
     subjectAltName = DNS:192.168.30.74, IP: 192.168.30.71, IP: 192.168.30.39, IP:0.0.0.0, IP:127.0.0.1
    添加访问的ip列表
    root@ros-OptiPlex-3050:/home/hett/certs# openssl x509 -req -days 365 -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem -extfile extfile.cnf
    Signature ok
    subject=/CN=192.168.30.240
    Getting CA Private Key
    Enter pass phrase for ca-key.pem:
    root@ros-OptiPlex-3050:/home/hett/certs# openssl genrsa -out key.pem 4096
    Generating RSA private key, 4096 bit long modulus
    ..........................................................................................++
    ............................................................................................................................................................................................................................................................................................................++
    e is 65537 (0x10001)
    root@ros-OptiPlex-3050:/home/hett/certs# ll
    total 40
    drwxr-xr-x  2 root root 4096 9月  13 13:54 ./
    drwxr-xr-x 18 hett hett 4096 9月  13 10:51 ../
    -rw-r--r--  1 root root 3326 9月   3 11:10 ca-key.pem
    -rw-r--r--  1 root root 2090 9月   3 11:11 ca.pem
    -rw-r--r--  1 root root   17 9月  13 13:53 ca.srl
    -rw-r--r--  1 root root  100 9月  13 13:53 extfile.cnf
    -rw-r--r--  1 root root 3243 9月  13 13:54 key.pem
    -rw-r--r--  1 root root 1907 9月  13 13:53 server-cert.pem
    -rw-r--r--  1 root root 1590 9月   3 11:14 server.csr
    -rw-r--r--  1 root root 3243 9月   3 11:14 server-key.pem
    root@ros-OptiPlex-3050:/home/hett/certs# openssl req -subj '/CN=client' -new -key key.pem -out client.csr
    root@ros-OptiPlex-3050:/home/hett/certs# openssl x509 -req -days 365 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out cert.pem -extfile extfile.cnfSignature ok
    subject=/CN=client
    Getting CA Private Key
    Enter pass phrase for ca-key.pem:
    root@ros-OptiPlex-3050:/home/hett/certs# ll
    total 48
    drwxr-xr-x  2 root root 4096 9月  13 13:54 ./
    drwxr-xr-x 18 hett hett 4096 9月  13 10:51 ../
    -rw-r--r--  1 root root 3326 9月   3 11:10 ca-key.pem
    -rw-r--r--  1 root root 2090 9月   3 11:11 ca.pem
    -rw-r--r--  1 root root   17 9月  13 13:54 ca.srl
    -rw-r--r--  1 root root 1895 9月  13 13:54 cert.pem
    -rw-r--r--  1 root root 1582 9月  13 13:54 client.csr
    -rw-r--r--  1 root root  100 9月  13 13:53 extfile.cnf
    -rw-r--r--  1 root root 3243 9月  13 13:54 key.pem
    -rw-r--r--  1 root root 1907 9月  13 13:53 server-cert.pem
    -rw-r--r--  1 root root 1590 9月   3 11:14 server.csr
    -rw-r--r--  1 root root 3243 9月   3 11:14 server-key.pem
    root@ros-OptiPlex-3050:/home/hett/certs#

    重启

    root@ros-OptiPlex-3050:/home/hett/certs# systemctl daemon-reload
    root@ros-OptiPlex-3050:/home/hett/certs# service docker restart
    root@ros-OptiPlex-3050:/home/hett/certs#

    配置证书完成

    2. 通过api调用

    项目中添加<!-- https://mvnrepository.com/artifact/com.github.docker-java/docker-java -->
    <dependency>
    <groupId>com.github.docker-java</groupId>
    <artifactId>docker-java</artifactId>
    <version>3.0.14</version>
    </dependency>

    public class DockerUtil {
    /**
    * docker的客户端
    */
    private static DockerClient dockerClient;

    public static void main(String[] args) {
    DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder().withDockerTlsVerify(true).
    withDockerCertPath("F:/cert").withDockerHost("tcp://192.168.30.240").withDockerConfig("F:/cert").
    withApiVersion("3.0.14").withRegistryEmail("1771084007@qq.com").withRegistryPassword("hett").
    withRegistryUsername("hett").build();
    dockerClient = DockerClientBuilder.getInstance(config).build();
    List<Container> containers = dockerClient.listContainersCmd().exec();//获取创建的容器
    //通过for循环实现
    for (Container container: containers){

    }

    }
  • 相关阅读:
    拒绝喝酒理由1
    动态调用事件,事件的方法
    c#发送图片
    c#截屏功能的实现
    devexpress中文讨论论坛
    解析javascript变量
    devexpress_PivotGrid说明
    python学习之老男孩python全栈第九期_day022知识点总结——初识面向对象
    python学习之老男孩python全栈第九期_day022作业
    员工信息表
  • 原文地址:https://www.cnblogs.com/youran-he/p/9640325.html
Copyright © 2011-2022 走看看