docker 错误:对于 nginx 无法启动服务 nginx:驱动程序无法编程外部连接

问题描述 投票:0回答:3

我是

Docker
的新手,并使用
Django
 设置我的第一个 
Docker

应用程序

我的申请路径是这样的

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
3个回答
5
投票

您不能为这两种服务分配主机的端口 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 存储库获取官方 Python 镜像,或者从任何其他基础镜像开始,例如来自 Debian 官方存储库的 debian:jessie-slim 或保留 Ubuntu 18.04 镜像


3
投票

对我来说,停止/重新启动 NGINX 服务器是有效的。

要停止 Nginx,请使用以下命令:

sudo systemctl stop nginx

要在 Nginx 停止时启动它,请使用以下命令:

sudo systemctl start nginx 

要重新启动 Nginx,请使用以下命令:

sudo systemctl restart nginx 

要在更改配置后重新加载 Nginx,请使用以下命令:

sudo systemctl reload nginx 

0
投票

升级到Ubuntu 22.04后,由于在升级过程中安装了apache2,我遇到了同样的问题。为了解决这个问题,我卸载了apache2,一切顺利。由于我使用的是 docker,因此 apache2 对我来说不是必需的。但是,如果您同时需要 apache2 和 docker,您可能需要考虑为 nginx 使用不同的端口,或者在使用 nginx 时停止 apache2。

以下是停止 apache2 的命令:

  • 停止apache2

    sudo 服务 apache2 停止

  • 卸载apache2

    sudo apt-get purge apache2 apache2-utils apache2.2-bin

    sudo rm -rf /etc/apache2

或者,您可以找到哪个服务正在使用端口 80 并使用以下命令停止它:

sudo ss -tuln | grep 80
© www.soinside.com 2019 - 2024. All rights reserved.