前提条件
- 服务器已安装Docker
- 本地已安装 Docker Desktop 或其他工具
- 远程服务器需开放 Docker 远程连接的端口。
- 本地能通过网络访问远程服务器(如通过公网 IP、局域网 IP 或 VPN)。
登录远程服务器配置 Docker 守护进程
默认情况下,Docker 守护进程仅监听本地 Unix 套接字(/var/run/docker.sock),不允许远程连接,需修改配置开启远程访问。
编辑 Docker 配置文件
根据服务器系统不同,配置文件路径可能不同,示例为Linux服务器
编辑 /etc/docker/daemon.json(若不存在则创建):
{
"hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2375"]
}JSONunix:///var/run/docker.sock:保留本地套接字访问(不影响服务器本地操作)。tcp://0.0.0.0:2375:允许所有 IP 通过 2375 端口远程访问(若需限制来源,可替换0.0.0.0为指定 IP)
配置 TLS 加密(可选,主要是安全访问)
注意:Docker over TLS 应在 TCP 端口 2376 上运行。
本文采用TLS加密配置,如果选择直接暴露 Docker TCP 端口:使用2375端口可跳过这步。
参考 Docker 官方文档生成 TLS 证书
1.在服务器生成 CA 私钥和公钥
首先确定好存放证书秘钥的目录
依次执行命令:
生成RSA私钥
openssl genrsa -aes256 -out ca-key.pem 4096Bash参数解释:
genrsa:生成RSA私钥-aes256:使用AES-256加密算法对私钥进行加密保护-out ca-key.pem:输出文件名为ca-key.pem4096:生成4096位的RSA密钥(安全性更高)
作用: 创建证书颁发机构(CA)的加密私钥文件,需要设置密码保护。
这里会要求你输入密码
生成自签名CA证书
openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.pemBash参数解释:
req:证书请求和生成工具-new:创建新的证书请求-x509:输出自签名证书而不是证书请求-days 365:证书有效期为365天-key ca-key.pem:使用前面生成的私钥文件-sha256:使用SHA-256哈希算法-out ca.pem:输出证书文件为ca.pem
作用: 创建自签名的根证书颁发机构(CA)证书。
这里会要求你输入之前的密码以及证书的识别信息:
- Country Name (2 letter code) – 国家代码(2字母)
- 例如:CN(中国)、US(美国)等
- State or Province Name – 州或省名称
- 例如:Beijing、California 等
- Locality Name – 城市名称
- 例如:Beijing、Shanghai 等
- Organization Name – 组织名称
- 例如:My Company、Docker Inc 等
- Organizational Unit Name – 部门名称
- 例如:IT Department、DevOps 等
- Common Name – 重要:证书持有者的通用名称
- 对于服务器证书:通常是域名或IP地址
- 对于CA证书:可以任意命名
- Email Address – 邮箱地址
生成服务器证书
openssl genrsa -out server-key.pem 4096Bash参数解释:
genrsa:生成RSA私钥-out server-key.pem:输出服务器私钥文件4096:4096位密钥长度
作用: 创建服务器的非加密私钥文件,用于后续生成证书签名请求。
生成证书签名请求(CSR)
openssl req -subj "/CN=$HOST" -sha256 -new -key server-key.pem -out server.csrBash参数解释:
req:证书请求命令-subj "/CN=$HOST":重要 – 设置主题名称,其中:$HOST需要替换为实际的服务器主机名或IP地址CN(Common Name) 必须与客户端连接Docker时使用的主机名/IP一致
-sha256:使用SHA-256哈希算法-new:创建新的证书请求-key server-key.pem:使用前面生成的服务器私钥-out server.csr:输出证书签名请求文件
关于 $HOST 变量:在实际使用时,你需要将 $HOST 替换为具体值,即你服务器的域名或IP
创建扩展配置文件
echo subjectAltName = DNS:localhost,IP:127.0.0.1,IP:10.10.10.20 >> extfile.cnf
echo extendedKeyUsage = serverAuth >> extfile.cnfBash定义证书的扩展属性:
subjectAltName:主题备用名称,允许证书用于多个域名/IPDNS:域名IP:你的服务器IP
extendedKeyUsage = serverAuth:指定证书用于服务器身份验证
签署服务器证书
openssl x509 -req -days 365 -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem -extfile extfile.cnfBashx509 -req:处理证书请求-CA ca.pem -CAkey ca-key.pem:使用CA证书和私钥进行签名-CAcreateserial:创建序列号文件- server-cert.pem:最终生成的服务器证书
- -extfile extfile.cnf:上一步的扩展配置文件
生成客户端证书
openssl genrsa -out client-key.pem 4096Bash基本与服务器证书一致
生成客户端签名请求
openssl req -subj "/CN=client" -sha256 -new -key client-key.pem -out client.csrBash签发客户端证书
openssl x509 -req -days 365 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out client-cert.pem -extfile extfile-client.cnfBash过期证书的更新
重新执行签发步骤即可
文件权限设置(可选)
移除写入权限
chmod -v 0400 ca-key.pem client-key.pem server-key.pem
chmod -v 0444 ca.pem server-cert.pem client-cert.pemBash核心步骤:
- 在服务器生成 CA 根证书、服务器证书和密钥。
- 将 CA 根证书、客户端证书和密钥复制到本地(用于本地客户端验证)。
- 修改服务器
daemon.json配置 TLS 端口
{
"hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2376"],
"tlsverify": true,
"tlscacert": "/etc/docker/ca.pem",
"tlscert": "/etc/docker/server-cert.pem",
"tlskey": "/etc/docker/server-key.pem"
}JSON调整 Systemd 配置
编辑service文件
vi /lib/systemd/system/docker.serviceBash找到 ExecStart 行
例如,原配置可能为:
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sockINI修改为:
ExecStart=/usr/bin/dockerd --containerd=/run/containerd/containerd.sockINI重启Docker服务
systemctl daemon-reload && systemctl restart dockerBash遇到过端口配置冲突的问题, /etc/systemd/system/docker.service.d/override.conf中配置了 tcp://0.0.0.0:2375 和我的daemon.json配置中 2376端口冲突
[root@iZbp1hoo01cpn2z1ipoyi9Z certs]# systemctl status docker.service
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Drop-In: /etc/systemd/system/docker.service.d
└─override.conf
Active: failed (Result: exit-code) since Thu 2025-11-13 02:48:17 CST; 3s ago
Docs: https://docs.docker.com
Process: 712860 ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375 (code=exited, status=1/FAILURE)
Main PID: 712860 (code=exited, status=1/FAILURE)
Nov 13 02:48:14 iZbp1hoo01cpn2z1ipoyi9Z systemd[1]: docker.service: Main process exited, code=exited, status=1/FAILURE
Nov 13 02:48:14 iZbp1hoo01cpn2z1ipoyi9Z systemd[1]: docker.service: Failed with result 'exit-code'.
Nov 13 02:48:14 iZbp1hoo01cpn2z1ipoyi9Z systemd[1]: Failed to start Docker Application Container Engine.
Nov 13 02:48:17 iZbp1hoo01cpn2z1ipoyi9Z systemd[1]: docker.service: Service RestartSec=2s expired, scheduling restart.
Nov 13 02:48:17 iZbp1hoo01cpn2z1ipoyi9Z systemd[1]: docker.service: Scheduled restart job, restart counter is at 3.
Nov 13 02:48:17 iZbp1hoo01cpn2z1ipoyi9Z systemd[1]: Stopped Docker Application Container Engine.
Nov 13 02:48:17 iZbp1hoo01cpn2z1ipoyi9Z systemd[1]: docker.service: Start request repeated too quickly.
Nov 13 02:48:17 iZbp1hoo01cpn2z1ipoyi9Z systemd[1]: docker.service: Failed with result 'exit-code'.
Nov 13 02:48:17 iZbp1hoo01cpn2z1ipoyi9Z systemd[1]: Failed to start Docker Application Container Engine.
Bashoverride.conf
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2376Bash我的选择是,删掉override.conf文件中的内容,使用daemon.json 配置 TLS
查看端口是否被使用
[root@iZbp1hoo01cpn2z1ipoyi9Z certs]# lsof -i:2376
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
dockerd 717450 root 4u IPv6 54951424 0t0 TCP *:docker-s (LISTEN)Bash客户端配置
从服务器下载客户端证书文件
ca.pem– CA根证书client-cert.pem– 客户端证书client-key.pem– 客户端私钥
连接远程Docker演示


从上到下分别为CA证书,客户端证书和客户端私钥
I hate the feeling of being unwell, and a fungal infection, while
minor, was driving me crazy. I got a prescription for Lamisil and decided to use Medistorehub after a quick search.
I placed my order for Lamisil to feel like myself again and was thrilled
when it arrived so quickly. The treatment worked, and the annoying itch is gone.
I’m so grateful for this pharmacy.
small personal loans compare loan cash
advance now
I am genuinely pleased to glance at this website posts which carries
tons of useful data, thanks for providing these information.
My partner and I stumbled over here coming from a different web page and
thought I might check things out. I like what I see so now i am following you.
Look forward to looking into your web page again.
I’ve known Tessa since she was a nervous grad student presenting her research on network architecture.
Now she’s a confident expert helping thousands navigate online privacy.
Her first blog post at https://medium.com/@tessaxiang/why-i-built-festruover-an-engineers-quest-to-fix-your-privacy-e5088e1ff8ee explains why she
traded server rooms for content management.
I seriously love your site.. Pleasant colors &
theme. Did you create this amazing site yourself?
Please reply back as I’m looking to create my own personal site and would love
to find out where you got this from or just what the theme is
named. Appreciate it!
Hurrah, that’s what I was exploring for, what a stuff!
present here at this website, thanks admin of this site.