Docker 个容器遇到套接字问题(单独的 Flask + Nginx 容器)

Docker containers experiencing socket issue (separate Flask + Nginx containers)

我在 运行 docker-compose build && docker-compose up 后尝试设置多容器 Docker 时遇到以下错误索引页:

[crit] 8#8: *1 connect() to unix:/tmp/uwsgi.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.99.1, server: localhost, request: "GET / HTTP/1.1", upstream: "uwsgi://unix:/tmp/uwsgi.sock:", host: "192.168.99.100"

这是我的 docker-compose.yml:

web:
  restart: always
  build: ./web-app
  expose:
    - "8000"
  command: /usr/local/bin/uwsgi --ini sample-uwsgi.ini

nginx:
  restart: always
  build: ./nginx/
  ports:
    - "80:80"
  links:
    - web:web

nginx/Dockerfile

FROM nginx
RUN rm /etc/nginx/conf.d/default.conf
ADD sample-nginx.conf /etc/nginx/conf.d/

nginx/sample-nginx.conf

server {

    listen 80;
    server_name localhost;
    charset utf-8;
    client_max_body_size 75M;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/uwsgi.sock;
    }
}

web-app/Dockerfile

FROM ansible/ubuntu14.04-ansible:stable

WORKDIR /root
ADD application.py application.py
ADD requirements.txt requirements.txt
ADD sample-uwsgi.ini sample-uwsgi.ini

ADD ansible /srv/ansible
WORKDIR /srv/ansible

RUN ansible-playbook container-bootstrap.yml -c local

web-app/sample-uswgi.ini

[uwsgi]

module = application
callable = app

master = true
processes = 5

socket = /tmp/uwsgi.sock

chown-socket = www-data:www-data

vacuum = true
enable-threads=True
die-on-term = true

更新

根据@kryten 的建议,我将使用 TCP/IP

已更新 nginx.conf:

server {

    listen 80;
    server_name localhost;
    charset utf-8;
    client_max_body_size 75M;

    location / {
        uwsgi_pass localhost:8000;
        include uwsgi_params;
    }
}

已更新uwsgi.ini:

[uwsgi]

module = application
callable = app

master = true
processes = 5

socket = localhost:8000

chown-socket = www-data:www-data

vacuum = true
enable-threads=True
die-on-term = true

现在正在追寻以下错误:

[error] 8#8: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.99.1, server: localhost, request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:8000", host: "192.168.99.100"

您的某个应用程序似乎正在尝试通过 Unix socket 而不是 TCP/IP 连接。

这不适用于不同的容器,因为一个容器(套接字所在的容器)中的文件系统在另一个容器中不可访问。

解决方案是重新配置您的应用程序以通过 TCP/IP 而不是 Unix 套接字进行连接。

可能可以通过将套接字所在的文件系统中的位置暴露给另一个容器来进行连接,但我从未尝试过这个并且不知道是否它会起作用。

由于webnginx是独立的容器,nginx需要通过TCP连接到另一台计算机Linking the containers 已经完成了大部分工作,您只需将上游指向 web:8000 而不是本地主机。