How to connect to a Postgres server in a Docker (1.12.0) container on a Mac? - macos

The latest release of Docker doesn't use a virtual machine anymore, instead using a hypervisor to connect to the containers. This means I can no longer login to postgres with psql:
➜ postgres git:(master) ✗ docker run -d -p 5433:5432 db postgres
<sha>
➜ postgres git:(master) ✗ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
111f3bed4c52 db "/docker-entrypoint.s" 17 minutes ago Up 17 minutes 0.0.0.0:5433->5432/tcp zen_hugle
➜ postgres git:(master) ✗ psql -p 5433 -U postgres
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5433"?
I have also tried specifying localhost as the host, but that results in a strange output:
➜ postgres git:(master) ✗ psql -h localhost -p 5433 -U postgres
psql: %
Does anyone know what to do in this case? Thank you.

With follwing command,
docker run -d -p 5433:5432 db postgres
You are exporting your docker's 5432 port to docker-engine's 5433 port. Not your host machine's 5433.
Fetch your docker-machine's IP address with following command (assuming your docker vm name is default)
docker-machine env default
This should give you result similar to following lines
> export DOCKER_TLS_VERIFY="1"
> export DOCKER_HOST="tcp://192.168.99.100:2376"
> export DOCKER_CERT_PATH="/Users/<your-user>/.docker/machine/machines/default"
> export DOCKER_MACHINE_NAME="default"
Use your docker-machine's IP address to connect to Postgres running in container
>psql -h 192.168.99.100 -p 5433 -U postgres
psql (9.5.0, server 9.5.5)
Type "help" for help.
postgres=#

You can connect over tcp by using an IP like psql -h 0.0.0.0 -p 5433 -U postgres (or 127.0.0.1, etc.).
Using the default or "localhost" will try using the local domain socket (although the version of Docker doesn't change this behavior, you will generally need to connect to a containerized db via tcp).

Related

windows redis-client connect to docker server failed

I am using windows10, redis-64bit, I started a redis container with command:
docker run --name myredis -d redis redis-server --appendonly yes
when I try to connect to this container using:
redis-cli -h 192.168.99.1 -p 6379
it shows:
Could not connect to Redis at 192.168.99.1:6379: Unknown error
here, 192.168.99.1 is my virtual machine ip address, anyone know how to solve this issue, thanks!
To connect to a redis container from a remote server you should do the following:
Start redis container on host (192.168.99.1):
docker run --name myredis -p 7000:6379 -d redis redis-server
Connect via remote server:
redis-cli -h 192.168.99.1 -p 7000

Gitlab CI cannot connect spring application to postgres

I have following stage in gitlab yml file.
integration-tests:
image: mydocker-hub-id/mvn-intergration-tests-image:latest
stage: test
services:
- postgres
script:
- export PGPASSWORD=$POSTGRES_PASSWORD
- psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 'OK' AS status;"
- cd source_code
- pg_isready
- mvn verify -P test-ci -DskipUTs=true
When job is executed for command pg_isready I have response 5432 no response.
When tests are executed from each intergration test I have error:
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Image used in this stage is built based on postgres image which has additionally installed maven and java 11.
What could be the issue that spring application cannot connect with postgres server. Command postgres --version displays proper version. Also command
"psql -h "postgres" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT 'OK' AS status;"
returns the OK result.
Spring db connection in app.properties is following:
spring.datasource.url=jdbc:postgresql://localhost:5432/test
I found in gitlab documentation details about postgres service configuration
https://docs.gitlab.com/ee/ci/services/postgres.html?fbclid=IwAR1eDlOQ4ACn6zqw1auIetB07JRcCGi1Pjl-hpfGBM45ujvkImm6fHSSbgg
The db url should contain the name of host postgres instead of localhost.
I found gitlab-runner is running the postgresql docker image as postgres and this is working changing jdbc:postgresql://localhost:5432/dbname into jdbc:postgresql://postgres:5432/dbname.

Failed login to Docker postgres image via localhost with published port, but accessible by external IP

I'm trying a simple setup with a docker postgres image, publishing a port for me to connect from the localhost.
docker run -p 5432:5432 --name postgres -e POSTGRES_PASSWORD=password -e POSTGRES_USER=postgres -d postgres
I'm able to connect just fine if I specify the docker external IP:
psql -h 192.1469.99.100 -p 5432 -U postgres -d postgres --password
However I get a password authentication failure when trying against localhost:
psql -h localhost -p 5432 -U postgres -d postgres --password
psql: FATAL: password authentication failed for user "postgres"
Do I need to set up some manual port forwarding manually? The weird thing is that it seems to connect to the postgres server just fine, it's just bizarrely telling me the password fails. I've done something wrong with the docker config perhaps?
The pg_hba.conf looks like:
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
The host machine is Windows 7.
Did you try another port than 5432? Try some free port so that there is no conflict between your local postgre and your docker postgre. To me it looks like your local postgre is having higher prio on localhost:5432, takes the traffic and so you are trying to login to your local postgre instead of your docker postgre, but obv. just a guess.
Did you check if you have this port allowed in the virtual machine or whatever you use for virtualization?
The container has its own network and its loopback address does not refer to the host and vice versa.

Docker need to access hosts's Postgres database

I have a Flask app that's running inside a Docker container, and I can run it using the following command.
docker run -e DB_HOST=<...> -e DB_PORT=<...> -e DB_NAME=<...> -e DB_USER=<...> -e DB_PASSWORD=<...> -p 8080:8080 <tag name>
Before the database was on AWS, now the database is running on a MAC laptop. So how can the Flask app from the docker container to connect to the hosts Postgres' database? What should be my DB_HOST?
For security reasons a Postgres server make it hard to connect to it. By default you can connect to the server from localhost and 127.0.0.1.
This does not work from within Docker since localhost refers to the running docker container not your laptop.
You have to connect from Docker to the IP address of your laptop.
You can find your laptop's IP on a Mac in System Preference > Network
Or you can use ifconfig | grep inet.
You need to modify 2 Postgres config file:
postgresql.conf and pg_hba.conf
For file: /usr/local/var/postgres/postgresql.conf
add this line to listen to everything:
listen_addresses = '*' # what IP address(es) to listen on;
Or add your specific laptop IP.
For file /usr/local/var/postgres/pg_hba.conf add a line like this with your IP:
host all all 192.168.1.3/32 md5
Restart the Postgres server when you are done.

Connecting to rethinkdb (or any other app running on an http port) from the Docker OS X beta

I've installed the Docker for Mac beta which allows you to use docker commands directly. I want to try to run rethinkdb through docker, so I've followed the instructions of the rethinkdb docker container docs and done the following:
docker run --name some-rethink -v "$PWD:/data" -d rethinkdb
This works, and I can see the container with docker ps and start shell with docker exec -it /bin/bash
However, I can't connect to the admin panel on my Mac directly with their suggestion
$BROWSER "http://$(docker inspect --format \
'{{ .NetworkSettings.IPAddress }}' some-rethink):8080"
This essentially amounts to google-chrome http://172.17.0.2:8080/, but this doesn't work. I asked around and was told
You can't use the docker private ip address space to access the ports
You have to forward them to the mac
However, I'm not sure how to do this as I don't have any port forwarding tools I'm familiar with such as ssh on the container itself. Using the suggested port forwarding command in the rethinkdb container docs ssh -fNTL ... but with localhost instead of remote does not work.
How can I connect to the rethinkdb admin panel through http with the docker beta on a Mac?
Try forwarding the container port using the -p flag in the docker run command, e.g.:
docker run -p 8080:8080 --name some-rethink -v "$PWD:/data" -d rethinkdb
and then it should be accessible on localhost,
google-chrome http://127.0.0.1:8080/
Relevant docker run docs: https://docs.docker.com/engine/reference/run/#/expose-incoming-ports

Resources