Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection refused between docker container of clair and pgsql #134

Closed
JessCL opened this issue Apr 5, 2016 · 20 comments · Fixed by #140
Closed

Connection refused between docker container of clair and pgsql #134

JessCL opened this issue Apr 5, 2016 · 20 comments · Fixed by #140
Labels
kind/question something that couldn't be answered in the docs

Comments

@JessCL
Copy link

JessCL commented Apr 5, 2016

When I try to run the docker container of clair, there is something wrong with the connection to my pgsql. The docker logs are quoted as follows.

2016-04-05 10:27:47.078096 I | pgsql: running database migrations
2016-04-05 10:27:47.079614 E | pgsql: dial tcp [::1]:5432: getsockopt: connection refused
2016-04-05 10:27:47.079655 C | main: database: could not open database

I have tried several ways and on different machines and the issue still exists.

@liangchenye
Copy link
Contributor

Did you fill the pgsql setting in your config.yaml file and started with it?

  database:
    # PostgreSQL Connection string
    # http://www.postgresql.org/docs/9.4/static/libpq-connect.html
    source:

@JessCL
Copy link
Author

JessCL commented Apr 5, 2016

yes,like this
source: postgresql://mypgsql:1234@localhost:5432/myDB?sslmode=disable

and I run the postgresql container with

sudo docker run --name postgresql -itd --restart always \
  --publish 5432:5432 \
  --volume /srv/docker/postgresql:/var/lib/postgresql \
  --env 'PG_TRUST_LOCALNET=true' \
  --env 'PG_PASSWORD=1234' \
  --env 'DB_USER=mypgsql' --env 'DB_PASS=1234' \
  --env 'DB_NAME=myDB' \
  sameersbn/postgresql:9.4-18

@liangchenye
Copy link
Contributor

source: postgresql://mypgsql:1234@localhost:5432/myDB?sslmode=disable
I think the IP should not be localhost

@JessCL
Copy link
Author

JessCL commented Apr 5, 2016

I run the two container on the same host machine.

@candita
Copy link

candita commented Apr 5, 2016

I run both successfully on the same machine with DockerCompose and changed source to
postgresql://postgres:password@postgres:5432?sslmode=disable.

Try not specifying a database name.

@JessCL
Copy link
Author

JessCL commented Apr 5, 2016

I use the following to run the Clair container, referring to clair/README.md https://github.com/coreos/clair/blob/master/README.md

$ mkdir $HOME/clair_config
$ curl -L https://raw.githubusercontent.com/coreos/clair/master/config.example.yaml -o $HOME/clair_config/config.yaml
$ $EDITOR $HOME/clair_config/config.yaml # Add the URI for your postgres database
$ docker run -d -p 6060-6061:6060-6061 -v $HOME/clair_config:/config quay.io/coreos/clair -config=/config/config.yaml

And I also tried to run postgresql directly on the host machine, but get the same err as mentioned above.

@candita
Copy link

candita commented Apr 5, 2016

It is more important how the postgres container is running. Instead of localhost, use the alias "postgres". If you run "docker inspect" on your postgres container, you should see the alias "postgres", and that port 5432 is open for tcp. If you don't see alias "postgres" or port 5432 open for tcp, then you need to use the host or alias and port that is open for you to connect.

@JessCL
Copy link
Author

JessCL commented Apr 5, 2016

And now the logs change to

2016-04-05 13:39:00.291380 I | pgsql: running database migrations
2016-04-05 13:39:00.309188 E | pgsql: dial tcp: lookup postgres on 10.239.27.228:53: server misbehaving
2016-04-05 13:39:00.309353 C | main: database: could not open database

@candita
Copy link

candita commented Apr 5, 2016

"docker inspect" the postgres container. If you don't see alias "postgres" or port 5432 open for tcp, then you need to use the host or alias and port that is open for connection. The error shows dns can't find the alias. The DockerCompose instructions in the README.md will set up a postgres container for you with the right alias, I recommend it for testing.

@Quentin-M Quentin-M added the kind/question something that couldn't be answered in the docs label Apr 5, 2016
@JessCL
Copy link
Author

JessCL commented Apr 6, 2016

Thanks for answering my questions and helping me.

If I check the netstat, I can see the following result.

jun@kube-node-01:~$ sudo netstat -ntlp | grep 5432
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      27229/postgres
tcp6       0      0 :::5432                 :::*                    LISTEN      27229/postgres

I will try DockerCompose, but I still wonder if there is any solution for the docker method?

@Quentin-M
Copy link
Contributor

Hi,

I started PostgreSQL using the command you shared above. Note that I am using docker-machine.
Now, either with a local Clair binary or using the Docker container, I use the following database source in the config:

host=192.168.99.100 sslmode=disable dbname=postgres user=postgres password=1234 statement_timeout=60000

Host may vary depending on your Docker setup.

@jgsqware
Copy link
Contributor

Same here, even with the fully decoupled config proposed by @Quentin-M. Works with Clair RC1

@Quentin-M
Copy link
Contributor

Quentin-M commented Apr 15, 2016

Actually, could you show me your entire config file please? You must have a clair root key now in your config: https://github.com/coreos/clair/blob/master/config.example.yaml#L16

@jgsqware
Copy link
Contributor

@Quentin-M for that's the problem. Fixed with the root key.

Wrong config loading should output something isn't it?

jgsqware pushed a commit to jgsqware/clair that referenced this issue Apr 15, 2016
When a wrong config file was used, it result in a panic.
Adding some check condition to validate the Unmarshaled configuration
before using it

fixes quay#134
@jgsqware
Copy link
Contributor

I've opened a #140 to return an understandable error when config is not loaded properly.

@Quentin-M
Copy link
Contributor

@GabrielleLC See my answer above, I believe that it's going to fix your issue.

Quentin-M pushed a commit that referenced this issue Apr 18, 2016
* config: not properly loaded error

When a wrong config file was used, it result in a panic.
Adding some check condition to validate the Unmarshaled configuration
before using it

fixes #134

* check if datasource is set

* move error locally instead of utils/errors
@Quentin-M
Copy link
Contributor

Closed by the merge above. Feel free to re-open if necessary.

@wgpshashank
Copy link

wgpshashank commented Sep 19, 2018

I am facing the same issue

Creating network "go_default" with the default driver
Creating go_postgres_1 ... done
Creating go_app_1 ... done
Attaching to go_postgres_1, go_app_1
postgres_1 | The files belonging to this database system will be owned by user "postgres".
postgres_1 | This user must also own the server process.
postgres_1 |

    1. app_1 | 2018/09/19 21:32:46 dial tcp 127.0.0.1:5432: connect: connection refused

I don't have any other postgres running on 5432.

my docker-compose.yml below

version: '2'
services:
    # Application container
    app:
        build: .
        ports:
            - "8080:8080" 
        links:
              - postgres

        environment:
            DEBUG: 'true'
            PORT: '8080'        
    

    postgres:
         image: onjin/alpine-postgres:9.5 #10.5 didn't work out
         restart: unless-stopped
         ports:
             - "5432:5432"
         environment:
             LC_ALL: C.UTF-8
             POSTGRES_USER: test
             POSTGRES_PASSWORD: test
             POSTGRES_DB: test

Any luck ?

@nsandalov
Copy link

Faced the same problem. Figured out that connection URL to the database should not be localhost or 127.0.0.1. It should be URL to your container with the Postgres. So the easiest way to do it is to define something like container_name: clair_postgres in the docker-compose.yml and use it as a connection string postgresql://postgres:password@clair_postgres:5432?sslmode=disable

@danchengash
Copy link

Faced the same problem. Figured out that connection URL to the database should not be localhost or 127.0.0.1. It should be URL to your container with the Postgres. So the easiest way to do it is to define something like container_name: clair_postgres in the docker-compose.yml and use it as a connection string postgresql://postgres:password@clair_postgres:5432?sslmode=disable

This one worked..after several hours of struggle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/question something that couldn't be answered in the docs
Development

Successfully merging a pull request may close this issue.

8 participants