docker composer up-d获取错误postgres:0.0.0:5432的绑定失败:端口已分配

docker composer up-d获取错误postgres:0.0.0:5432的绑定失败:端口已分配,docker,docker-compose,Docker,Docker Compose,我在Mac上安装了docker安装2.0.0.3版(31259) docker compose up-d Removing ab-insight_postgres_1 Starting ab-insight_data_1 ... done Recreating 31d36fb9c48a_ab-insight_postgres_1 ... error ERROR: for 31d36fb9c48a_ab-insight_postgres_1 Cannot start service postg

我在Mac上安装了docker安装
2.0.0.3版(31259)

docker compose up-d

Removing ab-insight_postgres_1
Starting ab-insight_data_1 ... done
Recreating 31d36fb9c48a_ab-insight_postgres_1 ... error

ERROR: for 31d36fb9c48a_ab-insight_postgres_1  Cannot start service postgres: b'driver failed programming external connectivity on endpoint ab-insight_postgres_1 (5ed1c634dd3a43c2cd988ff7f14b5c1f3cde848e375c2915cf92420f819e21ac): Error starting userland proxy: Bind for 0.0.0.0:5432 failed: port is already allocated'

ERROR: for postgres  Cannot start service postgres: b'driver failed programming external connectivity on endpoint ab-insight_postgres_1 (5ed1c634dd3a43c2cd988ff7f14b5c1f3cde848e375c2915cf92420f819e21ac): Error starting userland proxy: Bind for 0.0.0.0:5432 failed: port is already allocated'
ERROR: Encountered errors while bringing up the project.
这是我的
docker compose.yml

version: '2'

services:
  web:
    restart: always
    build: ./web
    expose:
      - "8000"
    volumes:
      - /home/flask/app/web
    command: /usr/local/bin/gunicorn -w 2 -b :8000 project:app
    depends_on:
      - postgres

  nginx:
    restart: always
    build: ./nginx
    ports:
      - "80:80"
    volumes:
      - /www/static
    volumes_from:
      - web
    depends_on:
      - web

  data:
    image: postgres:11
    volumes:
      - /var/lib/postgresql
    command: "true"

  postgres:
    restart: always
    build: ./postgresql
    volumes_from:
      - data
    expose:
      - "5432"
这是我的
Dockerfile

FROM python:3.6.1
MAINTAINER Ka So <kanel.soeng@kso.com>

# Create the group and user to be used in this container
RUN groupadd flaskgroup && useradd -m -g flaskgroup -s /bin/bash flask

# Create the working directory (and set it as the working directory)
RUN mkdir -p /home/flask/app/web
WORKDIR /home/flask/app/web

# Install the package dependencies (this step is separated
# from copying all the source code to avoid having to
# re-install all python packages defined in requirements.txt
# whenever any source code change is made)
COPY requirements.txt /home/flask/app/web
RUN pip install --no-cache-dir -r requirements.txt

# Copy the source code into the container
COPY . /home/flask/app/web

RUN chown -R flask:flaskgroup /home/flask

USER flask

发生这种情况的原因是,在您的机器上本地运行的postges与docker-compose.yml for postges服务中提到的端口相同。 停止本地机器上运行的服务。(不推荐) 或者使用其他端口映射到docker的5432端口。为此,请更换

expose
  -5432
在postgresa服务中使用以下代码

 ports:
      - "5433:5432"
整个docker compose文件如下所示:

version: '2'

services:
  web:
    restart: always
    build: ./web
    expose:
      - "8000"
    volumes:
      - /home/flask/app/web
    command: /usr/local/bin/gunicorn -w 2 -b :8000 project:app
    depends_on:
      - postgres

  nginx:
    restart: always
    build: ./nginx
    ports:
      - "80:80"
    volumes:
      - /www/static
    volumes_from:
      - web
    depends_on:
      - web

  data:
    image: postgres:11
    volumes:
      - /var/lib/postgresql
    command: "true"

  postgres:
    restart: always
    build: ./postgresql
    volumes_from:
      - data
    ports:
      - "5433:5432"

我得到了这个
错误:合成文件'/docker Compose.yml'无效,因为:services.postgres.expose无效:应该是'PORT[/PROTOCOL]'格式。
当我替换expose'时,您是否键入了expose而不是ports
version: '2'

services:
  web:
    restart: always
    build: ./web
    expose:
      - "8000"
    volumes:
      - /home/flask/app/web
    command: /usr/local/bin/gunicorn -w 2 -b :8000 project:app
    depends_on:
      - postgres

  nginx:
    restart: always
    build: ./nginx
    ports:
      - "80:80"
    volumes:
      - /www/static
    volumes_from:
      - web
    depends_on:
      - web

  data:
    image: postgres:11
    volumes:
      - /var/lib/postgresql
    command: "true"

  postgres:
    restart: always
    build: ./postgresql
    volumes_from:
      - data
    ports:
      - "5433:5432"