【问题标题】:docker ERROR: for nginx Cannot start service nginx: driver failed programming external connectivity ondocker ERROR:对于 nginx 无法启动服务 nginx:驱动程序在编程外部连接时失败
【发布时间】:2018-11-12 20:54:11
【问题描述】:

我是 Docker 的新手,并使用 Docker 设置我的第一个 Django 应用程序

我的应用程序路径看起来像

app
 |- helloworld
    |- __init__.py
    |- manage.py
 |- static_cdn
    |- static_root
 |- config
    |- nginx
       |- nginx.conf
 |- Dockerfile
 |- docker-compose.yml
 |- requirements.txt
 |- start.sh

Docerfile的内容

FROM ubuntu:18.04

# -- Install Pipenv:
FROM python:3
ENV PYTHONUNBUFFERED 1

ENV LC_ALL C.UTF-8
ENV LANG C.UTF-8

# -- Install Application into container:
RUN set -ex && mkdir /app

WORKDIR /app
ADD requirements.txt /app/

RUN pip install -r requirements.txt

# -- Adding dependencies:
ADD . /app/

docker-compose.yml的内容

version: '3'

services:
  nginx:
    image: nginx:latest
    ports:
      - "9010:9010"
    volumes:
      - .:/app
      - ./config/nginx:/etc/nginx/conf.d
      - ./static_cdn:/static
    depends_on:
      - web
  web:
    build: .
    command: ./start.sh
    volumes:
      - .:/app
      - ./static_cdn:/static
    ports:
      - "9010:9010"
    depends_on:
      - db
    expose:
      - "9010"
  db:
    image: postgres

config/nginx/nginx.conf的内容

upstream web {
    ip_hash;
    server web:9010;
}

server {
    location /static {
        autoindex on;
        alias /static/
    }

    location / {
        proxy_pass http://127.0.0.1;
    }
    listen 9011;
    server_name localhost;
}

start.sh的内容

#!/usr/bin/env bash

# Start Gunicorn processes
echo --: Starting application build
echo --: Creating migration
exec python3 manage.py makemigrations
echo ------: makemigrations complete
echo --: Running migration
exec python3 manage.py migrate
echo ------: migrate complete
echo --: Running collectstatic
exec python3 manage.py collectstatic
echo ------: collectstatic complete
echo Starting Gunicorn.
exec gunicorn helloworld.wsgi:application \
    --bind 0.0.0.0:9010 \
    --workers 3

现在,当我使用 docker 构建时

docker-compose up --build

它给出了错误

错误:对于 nginx 无法启动服务 nginx:驱动程序失败 在端点 koober_nginx_1 上编程外部连接 (8ea5c084a7283a16afbf136a73dc4b27d9cae35fe14d735b83199ad5d0e03431): 绑定 0.0.0.0:9010 失败:端口已分配

我已经按照一些教程来创建这些 Docker 文件和 nginx conf 文件。

1.我该如何解决上述问题。
2.以上配置需要使用FROM ubuntu:18.04吗?

编辑 2

现在,它在从 start.sh 命令创建迁移后卡住了

【问题讨论】:

    标签: django docker nginx docker-compose dockerfile


    【解决方案1】:

    2021 年 12 月更新:

    杀死所有 nginx 进程:

    sudo killall nginx
    

    然后:

    docker-compose up --build
    

    【讨论】:

      【解决方案2】:

      您不能为这两种服务分配主机的 9010 端口。 这就是您在服务 nginx 和 web 声明的 ports 部分中所做的事情。

      此外,默认情况下,nginx 会为 https 监听端口 80 和 443。

      您可以保持这种状态并发布到主机上的不同端口。看看如何在 docker-compose 中使用 port 关键字:

      https://docs.docker.com/compose/compose-file/#ports

      也许你想要更多类似的东西:

      version: '3'
      
      services:
        nginx:
          image: nginx:latest
          ports:
            - "10080:80"
            - "10443:443"
          volumes:
            - .:/app
            - ./config/nginx:/etc/nginx/conf.d
            - ./static_cdn:/static
         depends_on:
            - web
      
        web:
          build: .
          command: ./start.sh
          container_name: "web-app"
          volumes:
            - .:/app
            - ./static_cdn:/static
          expose:
            - "9010"
          depends_on:
            - db
      
        db: 
          image: postgres
      

      config/nginx/nginx.conf的内容

      upstream web {
        ip_hash;
        server web-app:9010;
      }
      
      server {
          location /static {
              autoindex on;
              alias /static/
           }
      
      location / {
          proxy_pass http://web;
      }
      
      listen 80;
      server_name localhost;
      }
      

      关于您的最后一个问题,您可以从 Docker hub Python repository 获取官方 Python 映像,或者从任何其他基础映像(例如 Debian official repository 的 debian:jessie-slim)开始,或者保留 Ubuntu 18.04 映像

      【讨论】:

      • 谢谢,我做了与您的回答相同的更改,但现在在来自start.sh 的命令后卡住了。
      • 很难用你的日志来判断,因为我们只能看到 postresql 没问题,你的应用程序没有启动。你不能有更多的日志吗?
      • 我发现start.sh 文件只执行第一个exec 命令,然后停止。 .bash 文件有什么问题吗?我认为这将通过仅更改 start.sh 文件来解决。
      • 你为什么在最后一行使用exec? (exec gunicorn helloworld.wsgi:application \ --bind 0.0.0.0:9010 \ --workers 3) 而且不要只打电话给独角兽?
      猜你喜欢
      • 2021-10-25
      • 1970-01-01
      • 2018-04-20
      • 2021-06-27
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      相关资源
      最近更新 更多