Docker Community Forums

Share and learn in the Docker community.

How to connect application to postgres in docker environment


(Rafiqul) #1

My scenario is :
application container name: c_sys
application has a database conf file. what command I need to write in conf file so that it connects ci_psql container postgres database. Example that is given in postgres repo info:
docker run --name some-app --link some-postgres:postgres -d application-that-uses-postgres. Both containers are in the same Amazone instance.
I dont know what is “some-app” here!!! I thought of: docker run –name some-app --link ci_psql:postgres -d c_sys. where is password, ip, port option???

please advise.

postgres container name: ci_psql
ci_psql container has database named postgres and user is postgres.


(Jeff Anderson) #2

some-app would just be the name that you assign to your application container that you are starting using the c_sys image.

When you use --link, you get a bunch of environment variables that have information about the think you are linking to. If you have username/password information in the environment on the postgres image, that will become visible to the newly launched container. Some goes for the port.

If you run docker run --rm --link ci_psql:postgres busybox env, you’ll see all the environment variables that are available.

Cheers!


(Rafiqul) #3

thanks Jeff for the reply!!
what this does imply then: application-that-uses-postgres in (docker run --name some-app --link some-postgres:postgres -d application-that-uses-postgres). I am bit confused. Could you please help me how to make database connectivity with application in docker container environment?

I have my postgres container up and running with database and tables set for my app.

I have my playframework app folder which i have to mount while creating container from ingensi/play-framwork image. app folder has a configuration file where DB connection string has to set up. what command should i write in configuration file? or is there a way to connect my postgres container database while creating container from ingensi/play-framwork ?

please help.


(Jeff Anderson) #4

The official redmine image can use a postgresql database using --link

The first step is to fire up the database container itself:

docker run -d --name some-postgres -e POSTGRES_PASSWORD=secret -e POSTGRES_USER=redmine postgres

Next, I fire up redmine, and pass in my postgres container using --link:

docker run -d --name some-redmine --link some-postgres:postgres redmine

Now, I can docker exec into my application container and take a look:

$ docker exec -it some-redmine bash
root@9d8fa03bb2f0:/usr/src/redmine# env | grep POSTGR
POSTGRES_ENV_POSTGRES_USER=redmine
POSTGRES_PORT=tcp://172.17.0.17:5432
POSTGRES_ENV_POSTGRES_PASSWORD=secret
POSTGRES_ENV_LANG=en_US.utf8
POSTGRES_ENV_PG_MAJOR=9.4
POSTGRES_PORT_5432_TCP_PORT=5432
POSTGRES_PORT_5432_TCP_ADDR=172.17.0.17
POSTGRES_ENV_PGDATA=/var/lib/postgresql/data
POSTGRES_NAME=/some-redmine/postgres
POSTGRES_PORT_5432_TCP=tcp://172.17.0.17:5432
POSTGRES_PORT_5432_TCP_PROTO=tcp
POSTGRES_ENV_PG_VERSION=9.4.5-1.pgdg80+1
root@9d8fa03bb2f0:/usr/src/redmine# cat /etc/hosts | grep postgres
172.17.0.17	postgres 1897d495ade3 some-postgres

My redmine application can connect to the postgres database with the information provided in the environment thanks to the --link. It can also connect to the host postgres by name due to /etc/hosts containing an entry for it.

The some-redmine and some-postgres names in this example just refer to the names of the containers involved. I can change those to be pretty much anything I want in a real installation.

Hopefully this gives some perspective on how --link works.

Cheers!

see also: https://hub.docker.com/_/redmine/


(Deshverm) #5

Hi jeff,
I m trying to link two docker images where one is postgresql9.3 official image and other one is my custom image that is my_app.
i wanted to use postgres in my image and tried bellow steps:

step1 .docker run -d --name some-postgres -e POSTGRES_PASSWORD=secret -e
POSTGRES_USER=user_name postgres

step2. docker run --name app --link some-postgres:postgres my_app

getting error in step2
psycopg2.OperationalError: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?

How can i solve this problem .
please help


(Wovo) #6

I already had running postgres on host machine and didn’t want to allow connections from network, so I did run temporary postgres instance in container and created database in just two lines:

# Run PostgreSQL
docker run --name postgres-container -e POSTGRES_PASSWORD=password -it -p 5433:5432 postgres

# Create database
docker exec -it postgres-container createdb -U postgres my-db

With Regards,
Xovo https://showbox.run/
https://kodi.software/


(Rjsargent) #7

And if I want to connect to postgres running somewhere-not-dockered do I just proceed as per normal (i.e. supply the connection criteria etc) and get on with life?