Manuel Santi
Manuel Santi

Reputation: 1132

Why docker-compose fail and docker run success with postgresql?

I am tring to start a postgreSQL docker container with my mac; i use OSX 10.11.16 El Capitan with Docker Toolbox 19.03.01.

If i run:

docker run --name my_postgres -v my_dbdata:/var/lib/postgresql/data -p 54320:5432 postgres:11

all was done and i get:

my_postgres | 2019-09-17 04:51:48.908 UTC [41] LOG: database system is ready to accept connections

but if i use an .yml file like this one:

docker-compose.yml:

version: "3"
services:
  db:
    image: "postgres:11"
    container_name: "my_postgres"
    ports:
      - "54320:5432"
    volumes:
      - my_dbdata:/var/lib/postgresql/data
volumes:
  my_dbdata:

and run

docker-compose up

i get instead:

my_postgres | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/* my_postgres | my_postgres | 2019-09-17 04:51:49.009 UTC [41] LOG: received fast shutdown request my_postgres | 2019-09-17 04:51:49.011 UTC [41] LOG: aborting any active transactions my_postgres | waiting for server to shut down....2019-09-17 04:51:49.087 UTC [41] LOG: background worker "logical replication launcher" (PID 48) exited with exit code 1 my_postgres | 2019-09-17 04:51:49.091 UTC [43] LOG: shutting down my_postgres | 2019-09-17 04:51:49.145 UTC [41] LOG: database system is shut down

Why the same think with docker-compose fail?

So many thanks in advance

Upvotes: 1

Views: 6469

Answers (3)

b0gusb
b0gusb

Reputation: 4731

The my_dbdata named volume is not the same for the two cases. docker run creates a volume named my_dbdata, instead docker-compose creates by default a volume called <dir>_my_dbdata

Run docker volume to list the volumes:

docker volume ls |grep my_dbdata 

I suspect the volume created by docker-compose has issues and as a consequence postgres doesn't start correctly. The initialization of the database in the my_postgres container is done only once.

Try to remove the container and the volume created by docker-compose:

docker rm my_postgres
docker volume rm <dir>_my_dbdata 

Hope it helps

Upvotes: 0

Prateek Naik
Prateek Naik

Reputation: 2802

Try the below one it worked for me

version: '3.1'
services:
 db:
  image: postgres
  restart: always
  environment:
   POSTGRES_PASSWORD: mypassword
  volumes:
    - ./postgres-data:/var/lib/postgresql/data
  ports:
    - 5432:5432

Then use docker-compose up to get the logs after using the previous command use docker-compose logs -f

Upvotes: 1

takacsmark
takacsmark

Reputation: 4451

If you are trying to access and existing volume on the host machine, you need to specify that the volume was created outside the Compose file with the external keyword like this:

version: "3.7"

services:
  db:
    image: postgres
    volumes:
      - data:/var/lib/postgresql/data

volumes:
  data:
    external: true

I took the example from the Compose file reference https://docs.docker.com/compose/compose-file/.

Also double check the contents of your external volume between runs, to see if it was overriden.

Please also double check your quotes, you don't need to put the image name in quotes, but I don't think that's the issue.

Upvotes: 0

Related Questions