Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use volume in docker compose for postgres?

Here is the image I am using.

I named it posgres_test

If I run the image individually

docker run -i -t -v="test_volume:/var/lib/postgresql" -p 5432:5432 posgres_test 

I can access it with

psql -h 192.168.99.100 -p 5432 -U pguser -W pgdb 

Or I can access it with my golang app

// host is set to postgres db, err := sql.Open("postgres", "postgres://pguser:pguser@postgres:5432/pgdb") // table test_db is manually created. rows, err := db.Query("SELECT name FROM test_db WHERE) 

However if I use docker compose

docker-compose.yml

version: "2" services:   postgres:     image: my_image/postgresql:9.3     volumes:       - test_volume:/var/lib/postgresql     ports:       - "5432:5432"   web:     image: my-golang-app4     ports:       - "8080:8080" volumes:   test_volume: {} 

I get the following

pguser@pgdb ERROR:  relation "test_db" does not exist at character 15 

I know for sure test_db exist in test_volume since

docker run -i -t -v="test_volume:/var/lib/postgresql" -p 5432:5432 posgres_test psql -h 192.168.99.100 -p 5432 -U pguser -W pgdb  \dt 

will show the table I created

But it seems like my app in docker compose cannot find it

Can someone help me out?

like image 667
Kevin Avatar asked Feb 08 '17 07:02

Kevin


People also ask

What is postgres volume?

The Postgres official image, however, comes with a VOLUME predefined in its image description. This means that when you run a PostgreSQL image as a container, it creates a volume for itself and stores data in there.

How does docker integrate with PostgreSQL?

Fill the port value as 5432 that runs the Docker PostgreSQL Container and provide the name of the database as postgres. Then, fill the username and password fields with the credentials you created while running the PGAdmin container. After providing all required details, click on the “Save” button.

Can docker compose create volumes?

We can also create a volume with Docker compose service or also specify existing volumes. For example, the following screenshot shows a 'docker-compose' file that creates a docker-compose service with a volume. As a result of the above command, a volume with the name 'myvolume' gets created.


2 Answers

About your docker-compose file

  1. Firstly I thought it's because you don't use 'links' option to link your postgres container to web container - it's good practice if you don't expand ports - but you expand postgres port.

  2. If you want use inheritance from the image you posted Instead of using this line:

my_image/postgresql:9.3

use:

docker/postgres 

and create path docker/postgres and there place Dockerfile with inharitance from container you want.

  1. I always use sharing volumes in docker-compose.yml like this:

    .:/var/www/html

where . is my project path where I place my code files.

Image I created to test this case

I don't have your all docker files structure to reproduce this error and fix it, so I created docker-compose which should match your needs or help to fix your issue:

version: '2' services:   web:     build: docker/web     ports:       - "8080:8080"     links:       - dbpostgres      volumes:       - .:/var/www/html   # I will share my code so I map this path   dbpostgres:     image: postgres     volumes:       - /private/var/lib/postgresql:/var/lib/postgresql     ports:       - "5432:5432"     environment:       POSTGRES_USER: pguser       POSTGRES_PASSWORD: pguser       POSTGRES_DB: pgdb 

Notes:

  1. I will recommend use official postgres image

  2. I left comments next to the lines.

How I made connection:

host=dbpostgres port=5432 dbname=pgdb user=pguser password=pguser 

Because my web container know host dbpostgres(image name and domain name) now - I link them using links.

If you need database from existing container

If you need database from your existing container just use option docker cp to copy database localy:

docker cp posgres_test:/var/lib/postgresql /private/var/lib/postgresql 

where /private/var/lib/postgresql is path on your localhost. You need also change credentials to db in docker-compose to your old credentials. You have to do it before run docker-compose because if db doesn't exist, will be crated.

Any questions, let me know.

like image 170
Arkowsky Avatar answered Oct 07 '22 17:10

Arkowsky


If the volume is external and already existing before the use of docker-compose you should declare it external, or else docker compose will create a new volume with the project name as prefix.

volumes:   test_volume:    external: true 

Docs for external using compose v3 (mostly similar to v2): https://docs.docker.com/compose/compose-file/compose-file-v3/#external

like image 28
gprivitera Avatar answered Oct 07 '22 19:10

gprivitera