要让Docker应用通过域名访问,通常需要以下几个步骤:
安装Nginx:
docker run --name nginx-proxy -d -p 80:80 -p 443:443 -v /var/run/docker.sock:/tmp/docker.sock:ro nginxproxy/nginx-proxy
启动你的应用容器,并添加环境变量:
docker run -d --name your-app -e VIRTUAL_HOST=yourdomain.com your-app-image
运行容器时绑定到主机的80或443端口:
docker run -d -p 80:80 --name your-app your-app-image
然后在DNS服务商处将域名解析到服务器IP
version: '3'
services:
app:
image: your-app-image
environment:
- VIRTUAL_HOST=yourdomain.com
- LETSENCRYPT_HOST=yourdomain.com
- LETSENCRYPT_EMAIL=your@email.com
networks:
- proxy-tier
nginx-proxy:
image: jwilder/nginx-proxy
ports:
- "80:80"
- "443:443"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- ./certs:/etc/nginx/certs
networks:
- proxy-tier
letsencrypt:
image: jrcs/letsencrypt-nginx-proxy-companion
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./certs:/etc/nginx/certs
depends_on:
- nginx-proxy
networks:
- proxy-tier
networks:
proxy-tier:
driver: bridge
version: '3'
services:
traefik:
image: traefik:v2.5
command:
- --api.insecure=true
- --providers.docker
- --entrypoints.web.address=:80
- --entrypoints.websecure.address=:443
- --certificatesresolvers.myresolver.acme.tlschallenge=true
- --certificatesresolvers.myresolver.acme.email=your@email.com
- --certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./letsencrypt:/letsencrypt
your-app:
image: your-app-image
labels:
- "traefik.enable=true"
- "traefik.http.routers.your-app.rule=Host(`yourdomain.com`)"
- "traefik.http.routers.your-app.entrypoints=websecure"
- "traefik.http.routers.your-app.tls.certresolver=myresolver"
域名无法访问:
HTTPS证书问题:
Nginx 502错误:
希望以上信息能帮助你配置Docker应用的域名访问。根据你的具体需求选择合适的方案。