1

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

1
  • what is inside "my_dbdata" , I think it is empty Commented Sep 18, 2019 at 8:36

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

i get:db_1 | PostgreSQL init process complete; ready for start up. db_1 | db_1 | 2019-09-17 05:05:14.746 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 db_1 | 2019-09-17 05:05:14.749 UTC [1] LOG: listening on IPv6 address "::", port 5432 db_1 | 2019-09-17 05:05:14.751 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" db_1 | 2019-09-17 05:05:14.902 UTC [49] LOG: database system was shut down at 2019-09-17 05:05:14 UTC
db_1 | 2019-09-17 05:05:14.995 UTC [1] LOG: database system is ready to accept connections db_1 | 2019-09-17 05:06:39.504 UTC [1] LOG: received smart shutdown request db_1 | 2019-09-17 05:06:39.608 UTC [1] LOG: background worker "logical replication launcher" (PID 55) exited with exit code 1 db_1 | 2019-09-17 05:06:39.618 UTC [50] LOG: shutting down db_1 | 2019-09-17 05:06:39.662 UTC [1] LOG: database system is shut down testpost_db_1 exited with code 0
0

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.

1 Comment

(Without external: true Compose will include the directory name as part of the volume name, so the two forms in the question reference different volumes.)
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.