docker无法从文件连接到redis

时间:2016-10-29 16:07:40

标签: python docker redis docker-compose

我正在使用docker-compose运行docker。

在我试图访问使用以下内容创建的redis的文件中

 #include<iostream>
    #include<fstream>

    using namespace std;
    bool read1();
    void write()
    {
        ofstream fout("sentData.txt",ios::out);

        char sentence[1000]={'\0'};

        cout<<"Enter a sentence to check whether it contains \"good\" or \"great\" : "<<endl;
        cin.getline(sentence,1000);

        fout<<sentence;
    }

    int read()
    {
        ifstream fin("sentData.txt", ios::in);

        char sentence[100] = { '\0' };
        char good[5] = { 'g', 'o', 'o', 'd', '\0' };
        int count;
        bool check=0;

        while (!fin.eof())
        {
            fin.getline(sentence, 150);

            for (int i = 0; sentence[i] != '\0'; i++)
            {
                int k = 0;
                 count = 0;

    for (int j = i; sentence[j] != 32; j++)
            {
                if (sentence[j] == good[k])
                        {count++;

                        }
                    k++;
            }

            if(count==4)
               {
                  check=1;
                  break;
               }
        }
        if(count==4)
               {

                  break;
               }
    }
    fin.close();
    bool x=read1();

    if(check==1 || x==1)
        cout<<"Positive sentence."<<endl;

    }

    bool read1()
    {
        ifstream in("sentData.txt", ios::in);

        char sentence[100] = { '\0' };
        char great[6]={'g','r','e','a','t','\0'};

        int count;

    while(!in.eof())
    {
            in.getline(sentence,100);

                for (int i = 0; sentence[i] != '\0'; i++)
            {
                int k = 0;

                 count=0;

                for (int j = i; sentence[j] != 32; j++)
                {

                    if (sentence[j] == great[k])
                        {count++;

                        }

                    k++;

                }
                if(count==5)
                {
                    return 1;
                    break;
                }

            }

    }

    }


    int main()
    {
    write();
    read();

    return 0;
    }

这是我的docker-compose.yml redis部分:

import redis

pool = redis.ConnectionPool(host='redis', port=6379, db=0)
r = redis.Redis(connection_pool=pool)
r.flushall()

跑完后:

web:
  build: .
  ports:
   - "8000:8000"
  links:
   - db
   - redis
  environment:
    - DATABASE_URL=postgres://user:openhouse2016@db:5432/chat
    - REDIS_URL=redis://redis:6379/1
  volumes:
    - .:/opt/app
db:
  image: praiskup/postgresql:APIv1.0.1-fedora23
#  volumes:
#   - ./db:/var/lib/pgsql/data
  environment:
   - POSTGRESQL_DATABASE=chat
   - POSTGRESQL_USER=user
   - POSTGRESQL_PASSWORD=openhouse2016
   - POSTGRESQL_CONTAINER_OPTS=assert_external_data = false
worker:
  build: .
  environment:
    - DATABASE_URL=postgres://user:openhouse2016@db:5432/chat
    - REDIS_URL=redis://redis:6379/1
  links:
   - db
   - redis
  volumes:
    - .:/opt/app
  # we need to wait for database setup
  command: bash -c "sleep 7 && exec python /opt/app/channels-example/manage.py runworker -v3"
redis:
  image: redis
  ports:
        - "6379:6379"
migrator:
  build: .
  environment:
   - DATABASE_URL=postgres://user:openhouse2016@db:5432/chat
   - REDIS_URL=redis://redis:6379/1
  links:
   - db
  # it indeed takes this long to start the database
  command: bash -c "sleep 5 && exec python /opt/app/channels-example/manage.py migrate"

一切运行正常,redis表示已准备好接受端口6379上的连接。

但是,上面的python代码会生成:

docker-compose down
docker-compose build
docker-compose up

如何从python文件连接到redis?

编辑:

redis.exceptions.ConnectionError: Error -5 connecting to redis:6379. No address associated with hostname.

redis显示:

docker ps -a

1 个答案:

答案 0 :(得分:1)

迁移器未链接到redis。这修好了它:

migrator:
  build: .
  environment:
   - DATABASE_URL=postgres://user:openhouse2016@db:5432/chat
   - REDIS_URL=redis://redis:6379/1
  links:
   - db
   - redis #this line here was missing
  # it indeed takes this long to start the database
  command: bash -c "sleep 5 && exec python /opt/app/channels-example/manage.py migrate"