Самый простой файл docker compose для image с PostgreSQL:
1 2 3 4 5 6 7 8 9 10 11 12 |
version: '3' services: db: image: library/postgres:9.4.17 ports: - "5432:5432" restart: always network_mode: 'host' environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: root POSTGRES_DB: ustaxi |
Пытаемся запустить:
1 2 3 4 5 6 |
$ docker-compose up local_db_1 is up-to-date Attaching to local_db_1 db_1 | FATAL: "/var/lib/postgresql/data" is not a valid data directory db_1 | DETAIL: File "/var/lib/postgresql/data/PG_VERSION" does not contain valid data. db_1 | HINT: You might need to initdb. |
Довольно странная ошибка, почему-то контейнер не инициализировал PostgreSQL. Я довольно долго искал решение в Google и нашёл.
Файл «docker-compose.yaml»:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
version: '3' services: postgres: build: context: ./ dockerfile: Dockerfile.postgres ports: - "65432:5432" environment: - PGDATA=/var/lib/postgresql/data volumes: - pgdata:/var/lib/postgresql/data volumes: pgdata: driver: local |
Файл «Dockerfile.postgres»:
1 2 3 |
FROM postgres:9.4 RUN mkdir -p "$PGDATA" && chmod 700 "$PGDATA" |
Запускаем, и всё работает:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
$ docker-compose up Creating volume "local_pgdata" with local driver WARNING: Found orphan containers (local_db_1) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up. Building postgres Step 1/2 : FROM postgres:9.4 9.4: Pulling from library/postgres Digest: sha256:8b2ad7cd303751246f8b681614e362c89128c47440fe3efe833b4b9a983cdc93 Status: Downloaded newer image for postgres:9.4 ---> ed5db6e669ff Step 2/2 : RUN mkdir -p "$PGDATA" && chmod 700 "$PGDATA" ---> Running in c1077bc44e48 Removing intermediate container c1077bc44e48 ---> 59a56ef80e5f Successfully built 59a56ef80e5f Successfully tagged local_postgres:latest WARNING: Image for service postgres was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`. Creating local_postgres_1 ... done Attaching to local_postgres_1 postgres_1 | The files belonging to this database system will be owned by user "postgres". postgres_1 | This user must also own the server process. postgres_1 | postgres_1 | The database cluster will be initialized with locale "en_US.utf8". postgres_1 | The default database encoding has accordingly been set to "UTF8". postgres_1 | The default text search configuration will be set to "english". postgres_1 | postgres_1 | Data page checksums are disabled. postgres_1 | postgres_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok postgres_1 | creating subdirectories ... ok postgres_1 | selecting default max_connections ... 100 postgres_1 | selecting default shared_buffers ... 128MB postgres_1 | selecting dynamic shared memory implementation ... posix postgres_1 | creating configuration files ... ok postgres_1 | creating template1 database in /var/lib/postgresql/data/base/1 ... ok postgres_1 | initializing pg_authid ... ok postgres_1 | initializing dependencies ... ok postgres_1 | creating system views ... ok postgres_1 | loading system objects' descriptions ... ok postgres_1 | creating collations ... ok postgres_1 | creating conversions ... ok postgres_1 | creating dictionaries ... ok postgres_1 | setting privileges on built-in objects ... ok postgres_1 | creating information schema ... ok postgres_1 | loading PL/pgSQL server-side language ... ok postgres_1 | vacuuming database template1 ... ok postgres_1 | copying template1 to template0 ... ok postgres_1 | copying template1 to postgres ... ok postgres_1 | syncing data to disk ... ok postgres_1 | postgres_1 | Success. You can now start the database server using: postgres_1 | postgres_1 | postgres -D /var/lib/postgresql/data postgres_1 | or postgres_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start postgres_1 | postgres_1 | postgres_1 | WARNING: enabling "trust" authentication for local connections postgres_1 | You can change this by editing pg_hba.conf or using the option -A, or postgres_1 | --auth-local and --auth-host, the next time you run initdb. postgres_1 | **************************************************** postgres_1 | WARNING: No password has been set for the database. postgres_1 | This will allow anyone with access to the postgres_1 | Postgres port to access your database. In postgres_1 | Docker's default configuration, this is postgres_1 | effectively any other container on the same postgres_1 | system. postgres_1 | postgres_1 | Use "-e POSTGRES_PASSWORD=password" to set postgres_1 | it in "docker run". postgres_1 | **************************************************** postgres_1 | waiting for server to start....LOG: could not bind IPv6 socket: Cannot assign requested address postgres_1 | HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry. postgres_1 | LOG: database system was shut down at 2018-04-05 08:15:29 UTC postgres_1 | LOG: MultiXact member wraparound protections are now enabled postgres_1 | LOG: database system is ready to accept connections postgres_1 | LOG: autovacuum launcher started postgres_1 | done postgres_1 | server started postgres_1 | ALTER ROLE postgres_1 | postgres_1 | postgres_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/* postgres_1 | postgres_1 | LOG: received fast shutdown request postgres_1 | LOG: aborting any active transactions postgres_1 | waiting for server to shut down....LOG: autovacuum launcher shutting down postgres_1 | LOG: shutting down postgres_1 | LOG: database system is shut down postgres_1 | done postgres_1 | server stopped postgres_1 | postgres_1 | PostgreSQL init process complete; ready for start up. postgres_1 | postgres_1 | LOG: database system was shut down at 2018-04-05 08:15:30 UTC postgres_1 | LOG: MultiXact member wraparound protections are now enabled postgres_1 | LOG: database system is ready to accept connections postgres_1 | LOG: autovacuum launcher started |
Также в подобных случаях может помочь попытка удалить существующий контейнер docker-а, чтобы создать его заново:
1 2 3 4 5 6 7 8 9 |
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 76527874829c postgres:9.6 "docker-entrypoint.s…" 7 minutes ago Restarting (1) 16 seconds ago local_db_1 $ docker rm 76527874829c Error response from daemon: You cannot remove a restarting container 76527874829cb0956b4e01d4fbf3a5deb4474a6bc34f3a7e024fa61e5009e6ff. Stop the container before attempting removal or force remove $ docker stop 76527874829c 76527874829c $ docker rm 76527874829c 76527874829c |