我正在尝试设置一个 nginx 容器,该容器充当我已设置的另一个容器的代理。我想自动化这个设置,因为我需要在多台服务器上部署类似的设置。为此,我正在使用 Ansible。
这是我的nginx.conf
:
events {
worker_connections 1024;
}
http {
server {
listen 8080;
location / {
proxy_pass http://192.168.1.14:9000;
}
}
}
这是我的 Ansible YAML 文件的相关部分:
- name: Install Nginx
docker:
name: nginx
image: nginx
detach: True
ports:
- 8080:8080
volumes:
- /etc/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
当我第一次运行我的剧本时,nginx 正在运行但未绑定(bind)到
8080
如此处所示:6a4f610e86d nginx "nginx -g 'daemon off" 35 minutes ago Up Less than a second 80/tcp, 443/tcp nginx
但是,如果我直接运行 nginx 容器:
docker run -d -v /etc/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro -p 8080:8080 nginx
nginx 和我的代理按预期运行并正在监听
8080
:c3a46421045c nginx "nginx -g 'daemon off" 2 seconds ago Up 1 seconds 80/tcp, 443/tcp, 0.0.0.0:8080->8080/tcp determined_swanson
知道为什么它以一种方式工作而不是另一种吗?
更新
根据所选答案中给出的指导,我更新了我的 YAML 文件:
- name: Install Nginx
docker:
name: nginx
image: nginx
detach: True
ports:
- 8080:8080
expose:
- 8080
volumes:
- /etc/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
最佳答案
首先,你需要确保你的 nginx 镜像 EXPOSE
端口 8080,你可以直接在你的 ansible yaml 文件中指定:
expose
(added in 1.5)
List of additional container ports to expose for port mappings or links. If the port is already exposed using EXPOSE in a Dockerfile, you don't need to expose it again.
然后,我在考虑 Ansible docker module 时看到的唯一其他区别是端口在双引号内:
ports:
- "8080:9000"
此外,如果您想在同一个 docker 守护进程中预传递到另一个容器,您可能需要使用链接而不是固定 IP 地址。
links:
- "myredis:aliasedredis"
这样,您的 nginx.conf 包含一个固定规则:
proxy_pass http://aliasedredis:9000;
关于nginx - 使用 Ansible 在 Docker 中设置 Nginx 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34236598/