1.安装docker,请参考官网文档 centos下安装docker

2.安装完成应该可以使用docker的各种命令连接docker host。docker host运行在本机上,但与localhost不同。默认设置下,docker host(docker daemon)监听docker.sock。本机下应该有docker.sock文件,使得各种docker命令能够成功的在docker host上运行指令或者取回信息。下面将介绍如何修改默认的连接方式为tls方式。

3.openssl生成证书:

修改docker连接docker daemon连接方式为tls方式,需要前提条件是生成好的证书。证书可用openssl生成。建议新建一个文件夹用来存放将要生成的各种证书。CD到存放证书的目录

a.生成key和ca证书(生成key的时候输入的密码在后面生成证书的时候会多次用到,使用docker daemon host 的DNS名字代替下面的$HOST):

$ openssl genrsa -aes256 -out ca-key.pem 4096
Generating RSA private key, 4096 bit long modulus
............................................................................................................................................................................................++
........++
e is 65537 (0x10001)
Enter pass phrase for ca-key.pem:
Verifying - Enter pass phrase for ca-key.pem:
$ openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.pem
Enter pass phrase for ca-key.pem:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:SH
State or Province Name (full name) [Some-State]:ShangHai
Locality Name (eg, city) []:ShangHai
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Company Name
Organizational Unit Name (eg, section) []:Sales
Common Name (e.g. server FQDN or YOUR name) []:$HOST
Email Address []:example@xxx.com

b.生成server-key和和csr文件(使用docker daemon host 的DNS名字代替下面的$HOST)

$ openssl genrsa -out server-key.pem
Generating RSA private key, bit long modulus
.....................................................................++
.................................................................................................++
e is (0x10001)
$ openssl req -subj "/CN=$HOST" -sha256 -new -key server-key.pem -out server.csr

c.使你的tls连接能通过ip地址方式,绑定本机IP(使用本机IP代替下面的$LOCALIP)

$ echo subjectAltName = IP:$LOCALIP,IP:127.0.0.1 > extfile.cnf

$ openssl x509 -req -days  -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem -extfile extfile.cnf
Signature ok
subject=/CN=your.host.com
Getting CA Private Key
Enter pass phrase for ca-key.pem:

d.生成客户端访问需要的key和证书等文件

$ openssl genrsa -out key.pem
Generating RSA private key, bit long modulus
.........................................................++
................++
e is (0x10001)
$ openssl req -subj '/CN=client' -new -key key.pem -out client.csr

c.生成客户端证书配置文件

$ echo extendedKeyUsage = clientAuth > extfile.cnf

d.注册key

$ openssl x509 -req -days  -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out cert.pem -extfile extfile.cnf
Signature ok
subject=/CN=client
Getting CA Private Key
Enter pass phrase for ca-key.pem:

4.将生成的证书添加的docker的配置文件中,centos下docker的配置文件是/etc/sysconfig/docker,编辑配置文件

vi /etc/sysconfig/docker

5.修改配置文件OPTIONS配置(下面的证书地址换成你生成的对应证书的位置)

OPTIONS='--selinux-enabled --tlsverify --tlscacert=/etc/docker/ca.pem --tlscert=/var/docker/server-cert.pem --tlskey=/var/docker/server-key.pem -H tcp://0.0.0.0:2376'

6.保存退出,重启docker服务,输入docker images查看镜像

docker images
Get http://10.32.173.215:2376/v1.20/images/json: malformed HTTP response "\x15\x03\x01\x00\x02\x02".
* Are you trying to connect to a TLS-enabled daemon without TLS?
* Is your docker daemon up and running?

显示不能连接到docker daemon host。虽然docker daemon已经配置好并且重新启动了,但是相当于服务端更改了配置。所以客户端连接到docker daemon host的配置也需要修改。

7.修改docker连接配置

a.在root目录下创建目录.docker

mkdir ~/.docker

b.将客户端证书文件copy至.docker目录

cp -cv {ca,cert,key}.pem ~/.docker/

c.添加环境变量DOCKER_HOST和DOCKER_TLS_VERIFY

vi /etc/profile

d.在打开的文件中最后加上新的环境变量($YOURIP替换成docker daemon host的IP即本机IP)

export DOCKER_HOST=tcp://$YOURIP:2376
export DOCKER_TLS_VERIFY=

8.配置完成,输入docker images可连接docker daemon host查看镜像。至此,docker daemon host连接方式已经修改为tls方式。调用远程API的时候需要使用的证书就是~/.docker文件夹中的证书。

  

centos下修改docker连接docker_host默认方式为tls方式的更多相关文章

  1. Linux/CentOS下修改MAC地址

    Linux/CentOS下修改MAC地址 摘自:https://blog.csdn.net/qq_33233768/article/details/64906265 2017年03月22日 11:06 ...

  2. Centos下安装Docker,并配置国内docker源

    Centos下安装Docker 替换为国内的yum源(可选): mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.re ...

  3. centos下修改文件后如何保存退出

    centos下修改文件后如何保存退出 保存命令 按ESC键 跳到命令模式,然后: :w 保存文件但不退出vi :w file 将修改另外保存到file中,不退出vi :w! 强制保存,不推出vi :w ...

  4. CentOS下利用Docker部署Surging

    原文:CentOS下利用Docker部署Surging 1. 安装Centos, 配置固定ip配置文件地址vi /etc/sysconfig/network-scripts/ifcfg-ens33`` ...

  5. docker学习之路-centos下安装docker

    前言 我要在云服务器上做一个asp.net core的webapi应用,使用docker来部署应用,中间用到的任何组件包括nginx和sqlserver 2017都是用docker来装载运行,所以,这 ...

  6. centos 下修改mysql 默认字符集

    解决办法: CentOS 7下修改MySQL数据库字符编码为UTF-8,UTF-8包含全世界所有国家需要用到的字符,是国际编码. 具体操作: 1.进入MySQL控制台 mysql  -u root - ...

  7. Centos系统修改docker默认网络参数

    刚Yum装完发现是没有网上所说的/etc/default/docker文件的,自己vim后其实也是不生效的. 因为Docker的systemd启动脚本(/usr/lib/systemd/system/ ...

  8. 在centos下部署docker内网私服

    Docker内网私服:docker-registry with nginx & ssl on centos docker-registry既然也是软件应用,自然最简单的方法就是使用官方提供的已 ...

  9. Centos下安装Docker集群管理工具Shipyard

    一. Docker Shipyard是什么 ? shipyard是一个开源的docker管理平台,其特性主要包括: 支持镜像管理.容器管理. 支持控制台命令 容器资源消耗监控 支持集群swarm,可以 ...

随机推荐

  1. 1105 Spiral Matrix(25 分)

    This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...

  2. fastJson Gson对比及java序列化问题

    一个案例 POJO没有set方法, 造成反序列化时出现NPE问题.实际场景:POJO是第三方提供的,final public class XJSONTest { public static void ...

  3. about 字节

    关于由于赋值导致字节的截断.字节扩展及数据类型的提升: 一.字节截断:int----->char 当一个字节(8位)放不下时,出现截断,直接取(最后一个字节)最后面面8位. 例如:1000000 ...

  4. JAVA多线程之线程池的使用

    合理利用线程池能够带来三个好处. 第一:降低资源消耗.通过重复利用已创建的线程降低线程创建和销毁造成的消耗. 第二:提高响应速度.当任务到达时,任务可以不需要等到线程创建就能立即执行. 第三:提高线程 ...

  5. 系统启动时,BIOS与影子内存_5

    问题:“当我们按下电源开关时,电源开始供电,芯片组撤去RESET信号,CPU马上就从地址FFFF0H处开始执行指令,这个地址在系统BIOS的地址范围内,无论是Award BIOS还是AMI BIOS, ...

  6. SQL判断经纬度在矩形内

    1,将城市地图拆分等距拆分为矩形 数据结构如图: 2.查看高德JS API (点是否在多边形内)核心代码: a=[114.069564,22.545774]; b=[ [114.067595,22.5 ...

  7. C++中的swap函数

    最通用的模板交换函数模式:创建临时对象,调用对象的赋值操作符 template <class T> void swap ( T& a, T& b ) { T c(a); a ...

  8. UICollectionView笔记2

    WWDC 2012 Session笔记——219 Advanced Collection Views and Building Custom Layouts 这是博主的WWDC2012笔记系列中的一篇 ...

  9. Java中的do-while循环——通过示例学习Java编程(11)

    作者:CHAITANYA SINGH 来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid=22&cid=0 在上一篇教程中,我们讨论了w ...

  10. Java项目—嗖嗖移动业务大厅

    嗖嗖移动业务大厅包类(如下图): SosoMgr: package cn.biz; import java.util.Scanner; import cn.common.Common; import ...