如何通过域名访问云服务器上的 Docker 服务

核心流程是:域名解析 → 服务器安全组放行 → Docker 端口映射 → Nginx 反向代理(推荐)。下面以 yhmh.top 为例,一步步带你完成配置。

一、前期准备

  1. 已购买域名:yhmh.top
  2. 云服务器(已安装 Docker),并记录公网 IP(如 123.45.67.89
  3. 确保服务器已开放 80/443 端口(HTTP/HTTPS),以及 Docker 服务端口(如 8080)

二、步骤 1:配置域名 DNS 解析(指向服务器 IP)

在域名注册商(如阿里云 / 腾讯云 / GoDaddy)的域名解析页面,添加 A 记录

表格

记录类型主机记录记录值(服务器公网 IP)TTL
A@123.45.67.89600
Awww123.45.67.89600
  • @ 对应 yhmh.top
  • www 对应 www.yhmh.top
  • 保存后等待 5–10 分钟 生效(可本地 ping yhmh.top 验证)

三、步骤 2:放行服务器安全组 / 防火墙

1. 云服务器安全组(阿里云 / 腾讯云等)

在控制台安全组规则中,放行:

  • 入方向:80、443、8080(Docker 端口) 协议 TCP,来源 0.0.0.0/0

2. 服务器本地防火墙(如 firewalld/ufw)

bash

运行

# CentOS/RHEL
sudo firewall-cmd --add-port=80/tcp --permanent
sudo firewall-cmd --add-port=443/tcp --permanent
sudo firewall-cmd --add-port=8080/tcp --permanent
sudo firewall-cmd --reload

# Ubuntu/Debian
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw allow 8080/tcp
sudo ufw reload

四、步骤 3:启动 Docker 容器(端口映射)

以一个运行在 8080 端口 的 Web 容器为例(如 Nginx/Node.js/Java):

bash

运行

# 示例:启动一个 Nginx 容器,映射宿主机 8080 → 容器 80
docker run -d \
  --name my-web \
  -p 8080:80 \
  --restart=always \
  nginx:alpine
  • 验证:浏览器访问 http://服务器IP:8080,能打开页面即成功

五、步骤 4:配置 Nginx 反向代理(推荐)

用 Nginx 把 yhmh.top 转发到 localhost:8080,实现域名直接访问。

1. 安装 Nginx(宿主机)

bash

运行

# CentOS/RHEL
sudo yum install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx

# Ubuntu/Debian
sudo apt update
sudo apt install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx

2. 创建 Nginx 配置文件

bash

运行

sudo nano /etc/nginx/conf.d/yhmh.top.conf

写入以下配置(替换你的域名和端口):

nginx

server {
    listen 80;
    server_name yhmh.top www.yhmh.top;

    location / {
        # 转发到 Docker 容器地址
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

3. 测试并重启 Nginx

bash

运行

sudo nginx -t  # 检查配置是否正确
sudo systemctl reload nginx

六、验证访问

现在直接在浏览器输入:

  • http://yhmh.top
  • http://www.yhmh.top

即可访问到你的 Docker 服务。


七、可选:配置 HTTPS(推荐)

1. 申请免费 SSL 证书(Let’s Encrypt)

bash

运行

# 安装 certbot
sudo apt install certbot python3-certbot-nginx -y  # Ubuntu
# 或
sudo yum install certbot python3-certbot-nginx -y  # CentOS

# 一键申请并配置 HTTPS
sudo certbot --nginx -d yhmh.top -d www.yhmh.top

按提示输入邮箱、同意协议,Certbot 会自动修改 Nginx 配置并启用 HTTPS。

2. 验证

访问 https://yhmh.top,地址栏出现锁图标即成功。


八、常见问题排查

  1. 域名访问 502 / 无法打开
    • 检查 DNS 解析是否生效:ping yhmh.top
    • 检查安全组 / 防火墙是否放行 80/443/8080
    • 检查 Nginx 配置:sudo nginx -t
    • 检查 Docker 容器是否正常运行:docker ps
  2. IP + 端口能访问,域名不行
    • 大概率是 Nginx 反向代理配置错误或未 reload
    • 查看 Nginx 日志:sudo tail -f /var/log/nginx/error.log

九、Docker Compose 一键部署(进阶)

如果你用 docker-compose,可以把 Nginx 和业务容器一起编排,示例 docker-compose.yml

yaml

version: '3'
services:
  web:
    image: nginx:alpine
    volumes:
      - ./html:/usr/share/nginx/html
    networks:
      - app-net

  nginx-proxy:
    image: nginx:alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./nginx/ssl:/etc/nginx/ssl
    depends_on:
      - web
    networks:
      - app-net

networks:
  app-net:
    driver: bridge

启动:docker-compose up -d