简体   繁体   English

docker-compose 不启动 postgres

[英]docker-compose does not start postgres

I m creating a docker-compose config for an django app, the Dockerfile builds successfully but when I compose them up, django return an issue -- cannot connect to postgres.我正在为 django 应用程序创建 docker-compose 配置,Dockerfile 构建成功,但是当我组合它们时,django 返回一个问题 - 无法连接到 postgres。

I run docker-compose run web bash, found redis and posgres both cannot be connected.我运行 docker-compose run web bash,发现 redis 和 posgres 都无法连接。

My docker-compose.yml file我的 docker-compose.yml 文件

web:
 build: .
  ports:
    - "8000:8000"
environment:
  - 'DATABASE_HOST=db'
  - 'DATABASE_NAME=mydb'
  - 'DATABASE_USER=root'
  - 'DATABASE_PASSWORD=root'
links:
  - db
db:
  image: postgres:9.1

when running sudo docker-compose up i got the following error.运行 sudo docker-compose up 时出现以下错误。

web_1 |   File "/usr/local/lib/python2.7/site packages/django/db/backends/postgresql/base.py", line 175, in  get_new_connection

web_1 |     connection = Database.connect(**conn_params)
web_1 |   File "/usr/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
web_1 |     conn = _connect(dsn, connection_factory=connection_factory, async=async)
web_1 | django.db.utils.OperationalError: could not connect to server: Connection refused
web_1 |     Is the server running on host "localhost" (::1) and accepting
web_1 |     TCP/IP connections on port 5432?
web_1 | could not connect to server: Connection refused
web_1 |     Is the server running on host "localhost" (127.0.0.1) and accepting
web_1 |     TCP/IP connections on port 5432?

I also built a clustering with docker-compose , it probably will help you and answer your problem (here is the repo ).我还使用docker-compose构建了一个集群,它可能会帮助您并回答您的问题(这是repo )。 You can see the docker-compose.yml file, and the django settings file (I marked the lines you need).您可以看到docker-compose.yml文件和django 设置文件(我标记了您需要的行)。

You can also clone this repo and get django , angular2 , postgresql , and nginx containers, all link together already.你也可以克隆这个 repo 并获得djangoangular2postgresqlnginx容器,它们都已经链接在一起了。

You are linking your web container with the postgres container but you are not defining database name, password and user.您正在将您的 Web 容器与 postgres 容器链接,但您没有定义数据库名称、密码和用户。

  web:
   build: .
   ports:
    - "8000:8000"
   links:
    - db
  db:
    restart: always
    image: postgres:9.1
    ports:
      - "5432:5432"
    volumes:
      - pgvolume:/var/lib/postgresql/data
    environment:
      - POSTGRES_PASSWORD=root
      - POSTGRES_DB= aiotadb
      - POSTGRES_USER=root
   data:
    restart: always
    image: postgres:9.1
    volumes:
      - /var/lib/postgresql
    command: tail -f /dev/null

Also, if you already define your database options in your settings file, you don't need to declare it as env variables in web container.此外,如果您已经在设置文件中定义了数据库选项,则无需在 Web 容器中将其声明为环境变量。

version: '3'
services:
  basicproject:
    build: .
    container_name: basicproject-container
    depends_on:
      - postgres
    ports:
      - "8000:8000"

  postgres:
    image: postgres:9.4
    ports:
    - "5432"
    environment:
      - POSTGRES_USER=test
      - POSTGRES_PASSWORD=testing
      - POSTGRES_DB=test_db

Add dependency in your 'web' service like below:在您的“网络”服务中添加依赖项,如下所示:

depends_on:
  - db

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM