I try to setup a Postgresql db in my docker-compose and it worked as expected for a moment. But now, my migration sql scripts aren't runned anymore, even after a fresh new build (sudo docker-compose build or sudo docker-compose up --build) and after removing the persistent volume created locally (sudo rm -rf db-data).
$ sudo docker-compose up
[+] Running 14/14
⠿ db Pulled 10.7s
⠿ a2abf6c4d29d Pull complete 2.6s
⠿ e1769f49f910 Pull complete 2.8s
⠿ 33a59cfee47c Pull complete 2.9s
⠿ 461b2090c345 Pull complete 3.1s
⠿ 8ed8ab6290ac Pull complete 3.4s
⠿ 495e42c822a0 Pull complete 3.5s
⠿ 18e858c71c58 Pull complete 3.6s
⠿ 594792c80d5f Pull complete 3.7s
⠿ 794976979956 Pull complete 8.2s
⠿ eb5e1a73c3ca Pull complete 8.3s
⠿ 6d6360292cba Pull complete 8.3s
⠿ 131e916e1a28 Pull complete 8.4s
⠿ 757a73507e2e Pull complete 8.5s
[+] Running 2/2
⠿ Container db-1 Created 0.2s
⠿ Container app-1 Created 0.1s
Attaching to app-1, db-1
db-1 | The files belonging to this database system will be owned by user "postgres".
db-1 | This user must also own the server process.
db-1 |
db-1 | The database cluster will be initialized with locale "en_US.utf8".
db-1 | The default database encoding has accordingly been set to "UTF8".
db-1 | The default text search configuration will be set to "english".
db-1 |
db-1 | Data page checksums are disabled.
db-1 |
db-1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db-1 | creating subdirectories ... ok
db-1 | selecting dynamic shared memory implementation ... posix
db-1 | selecting default max_connections ... 100
db-1 | selecting default shared_buffers ... 128MB
db-1 | selecting default time zone ... Etc/UTC
db-1 | creating configuration files ... ok
db-1 | running bootstrap script ... ok
db-1 | performing post-bootstrap initialization ... ok
db-1 | syncing data to disk ... ok
db-1 |
db-1 |
db-1 | Success. You can now start the database server using:
db-1 |
db-1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
db-1 |
db-1 | initdb: warning: enabling "trust" authentication for local connections
db-1 | You can change this by editing pg_hba.conf or using the option -A, or
db-1 | --auth-local and --auth-host, the next time you run initdb.
db-1 | waiting for server to start....2022-01-26 14:57:29.498 UTC [49] LOG: starting PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
db-1 | 2022-01-26 14:57:29.506 UTC [49] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db-1 | 2022-01-26 14:57:29.529 UTC [50] LOG: database system was shut down at 2022-01-26 14:57:29 UTC
db-1 | 2022-01-26 14:57:29.542 UTC [49] LOG: database system is ready to accept connections
db-1 | done
db-1 | server started
db-1 | CREATE DATABASE
db-1 |
db-1 |
db-1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/00000000000000_diesel_initial_setup
db-1 |
db-1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/2021-10-21-140200_initial_schemes
db-1 |
db-1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/2022-01-24-120647_fill_tables
db-1 |
db-1 | waiting for server to shut down...2022-01-26 14:57:29.858 UTC [49] LOG: received fast shutdown request
db-1 | .2022-01-26 14:57:29.865 UTC [49] LOG: aborting any active transactions
db-1 | 2022-01-26 14:57:29.868 UTC [49] LOG: background worker "logical replication launcher" (PID 56) exited with exit code 1
db-1 | 2022-01-26 14:57:29.868 UTC [51] LOG: shutting down
db-1 | 2022-01-26 14:57:29.914 UTC [49] LOG: database system is shut down
db-1 | done
db-1 | server stopped
db-1 |
db-1 | PostgreSQL init process complete; ready for start up.
db-1 |
db-1 | 2022-01-26 14:57:29.993 UTC [1] LOG: starting PostgreSQL 14.1 (Debian 14.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
db-1 | 2022-01-26 14:57:29.994 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db-1 | 2022-01-26 14:57:29.994 UTC [1] LOG: listening on IPv6 address "::", port 5432
db-1 | 2022-01-26 14:57:30.008 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db-1 | 2022-01-26 14:57:30.026 UTC [63] LOG: database system was shut down at 2022-01-26 14:57:29 UTC
db-1 | 2022-01-26 14:57:30.038 UTC [1] LOG: database system is ready to accept connections
As you can see, my docker-entrypoint-initdb.d/* files are all being ignored. Notice that my 3 migrations scripts run fine on a local pg instance.
My docker-compose looks lik this:
version: '3.7'
services:
db:
image: postgres
restart: always
environment:
- POSTGRES_USER=***
- POSTGRES_PASSWORD=***
- POSTGRES_DB=***
ports:
- '5433:5432'
volumes:
- ./db-data:/var/lib/postgresql/data
- ./migrations:/docker-entrypoint-initdb.d/ # copy the sql migrations
[...]
volumes:
db-data:
According to poatgres image entrypoint only below file type executed:
*.sh
*.sql
*.sql.gz
*.sql.xz
Related
I'm having issues with postgres for 2 docker deployments on the same host. I need to deploy the same app with docker compose on the same host. I have one docker-compose file and two .env for each instance. The app is built with Ruby and Hanami web framework.
When deploying the first instance (production) it runs fine. With the second instance (staging) I get this error during docker-compose setup:
PostgreSQL Database directory appears to contain a database; Skipping initialization
.
.
.
2022-07-30 16:13:48.978 UTC [33] FATAL: password authentication failed for user "some_user"
postgres_1 | 2022-07-30 16:13:48.978 UTC [33] DETAIL: Role "some_user" does not exist.
I also see the port being used is 5432, which is the one used for production.
For staging, I've specified the other_user and a different port as you can see in the file below.
Here are the files in question with which I wanted to separate these DB instances:
docker-compose
version: '3'
services:
postgres:
image: postgres
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
ports:
- ${POSTGRES_PORT}:${POSTGRES_PORT}
volumes:
- postgres:/var/lib/postgresql/data
web:
build: .
command: >
bash -c "bundle exec hanami db migrate
&& bundle exec rake initial_settings:add_default_language
&& bundle exec rake initial_settings:add_session_validity
&& bundle exec rake import_user:create
&& bundle exec rake super_admin:create
&& bundle exec hanami assets precompile
&& bundle exec hanami server
&& cp -r apps/wordrocket/assets/webfonts public/webfonts
&& cp -r apps/wordrocket/assets/webfonts public/assets/webfonts
&& cp -r apps/wordrocket/assets/images/sort*.png public/assets
&& cp -r apps/wordrocket/assets/images/sort*.png public
&& cp -r apps/wordrocket/assets/images/ui-icons*.png public/assets/wordrocket
&& mkdir public/assets/images
&& cp -r apps/wordrocket/assets/images/sort*.png public/assets/images"
volumes:
- ./hanami_log/hanami_app.log:/usr/src/app/hanami_log/hanami_app.log
links:
- postgres
depends_on:
- postgres
nginx:
image: nginx:alpine
restart: unless-stopped
tty: true
ports:
- "${NGINX_PORT}:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./nginx_log:/var/log/nginx
depends_on:
- web
volumes:
postgres:
web:
nginx:
Production .env
POSTGRES_USER=some_user
POSTGRES_PASSWORD=some_password
POSTGRES_DB=app_production
NGINX_PORT=7080
COMPOSE_PROJECT_NAME=myAPP
POSTGRES_PORT=5432
Staging .env.staging
POSTGRES_USER=other_user
POSTGRES_PASSWORD=other_password
POSTGRES_DB=app_staging
NGINX_PORT=7081
COMPOSE_PROJECT_NAME=myAPP_staging
POSTGRES_PORT=5433
Makefile
production:
docker-compose -f docker-compose.yml --env-file .env up
staging:
docker-compose -f docker-compose.yml --env-file .env.staging up
I'm guessing I don't separate the postgres instances/dbs correctly, but I'm not sure. Also, is this a Docker or host problem?
+++ EDIT +++
I've made a separate docker-compose file for staging in order to differentiate between volumes.
production is the same:
volumes:
- postgres:/var/lib/postgresql/data
.
.
.
volumes:
postgres:
web:
nginx:
staging:
volumes:
- postgres_staging:/var/lib/postgresql/data
.
.
.
volumes:
postgres_staging:
web:
nginx:
This is the startup log up to the first error:
nginx_1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
nginx_1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
nginx_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
nginx_1 | 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
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 dynamic shared memory implementation ... posix
postgres_1 | selecting default max_connections ... 100
postgres_1 | selecting default shared_buffers ... 128MB
postgres_1 | selecting default time zone ... Etc/UTC
postgres_1 | creating configuration files ... ok
postgres_1 | running bootstrap script ... ok
postgres_1 | performing post-bootstrap initialization ... ok
nginx_1 | 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
nginx_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
nginx_1 | /docker-entrypoint.sh: Configuration complete; ready for start up
postgres_1 | syncing data to disk ... ok
postgres_1 |
postgres_1 |
postgres_1 | Success. You can now start the database server using:
postgres_1 |
postgres_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
postgres_1 |
postgres_1 | initdb: 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 | waiting for server to start....2022-07-31 10:03:12.510 UTC [47] LOG: starting PostgreSQL 14.4 (Debian 14.4-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
postgres_1 | 2022-07-31 10:03:12.513 UTC [47] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2022-07-31 10:03:12.524 UTC [48] LOG: database system was shut down at 2022-07-31 10:03:12 UTC
postgres_1 | 2022-07-31 10:03:12.529 UTC [47] LOG: database system is ready to accept connections
postgres_1 | done
postgres_1 | server started
postgres_1 | CREATE DATABASE
postgres_1 |
postgres_1 |
postgres_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
postgres_1 |
postgres_1 | waiting for server to shut down...2022-07-31 10:03:12.745 UTC [47] LOG: received fast shutdown request
postgres_1 | .2022-07-31 10:03:12.748 UTC [47] LOG: aborting any active transactions
postgres_1 | 2022-07-31 10:03:12.749 UTC [47] LOG: background worker "logical replication launcher" (PID 54) exited with exit code 1
postgres_1 | 2022-07-31 10:03:12.749 UTC [49] LOG: shutting down
postgres_1 | 2022-07-31 10:03:12.771 UTC [47] 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 | 2022-07-31 10:03:12.865 UTC [1] LOG: starting PostgreSQL 14.4 (Debian 14.4-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
postgres_1 | 2022-07-31 10:03:12.865 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2022-07-31 10:03:12.865 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2022-07-31 10:03:12.871 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2022-07-31 10:03:12.879 UTC [61] LOG: database system was shut down at 2022-07-31 10:03:12 UTC
postgres_1 | 2022-07-31 10:03:12.885 UTC [1] LOG: database system is ready to accept connections
postgres_1 | 2022-07-31 10:03:13.014 UTC [68] FATAL: password authentication failed for user "some_user"
Now this is the only instance being started. The production is not running even and it still tries to authenticate the production user.
Is it ignoring the .env.staging file?
+++ END EDIT +++
Best, Seba
I'm trying to up jhipsters postgres.yml but it seems db not running
postrgess.yml
services:
hellohipster-postgresql:
image: postgres:13.1
restart: always
# volumes:
# - /D/volume/:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=HelloHipster
- POSTGRES_PASSWORD=
- POSTGRES_HOST_AUTH_METHOD=trust
# If you want to expose these ports outside your dev PC,
# remove the "127.0.0.1:" prefix
ports:
- 127.0.0.1:5432:5432
docker-compose -f .\src\main\docker\postgresql.yml up
Creating network "docker_default" with the default driver
Creating docker_hellohipster-postgresql_1 ... done
Attaching to docker_hellohipster-postgresql_1
hellohipster-postgresql_1 | ********************************************************************************
hellohipster-postgresql_1 | WARNING: POSTGRES_HOST_AUTH_METHOD has been set to "trust". This will allow
hellohipster-postgresql_1 | anyone with access to the Postgres port to access your database without
hellohipster-postgresql_1 | a password, even if POSTGRES_PASSWORD is set. See PostgreSQL
hellohipster-postgresql_1 | documentation about "trust":
hellohipster-postgresql_1 | https://www.postgresql.org/docs/current/auth-trust.html
hellohipster-postgresql_1 | In Docker's default configuration, this is effectively any other
hellohipster-postgresql_1 | container on the same system.
hellohipster-postgresql_1 |
hellohipster-postgresql_1 | It is not recommended to use POSTGRES_HOST_AUTH_METHOD=trust. Replace
hellohipster-postgresql_1 | it with "-e POSTGRES_PASSWORD=password" instead to set a password in
hellohipster-postgresql_1 | "docker run".
hellohipster-postgresql_1 | ********************************************************************************
hellohipster-postgresql_1 | The files belonging to this database system will be owned by user "postgres".
hellohipster-postgresql_1 | This user must also own the server process.
hellohipster-postgresql_1 |
hellohipster-postgresql_1 | The database cluster will be initialized with locale "en_US.utf8".
hellohipster-postgresql_1 | The default database encoding has accordingly been set to "UTF8".
hellohipster-postgresql_1 | The default text search configuration will be set to "english".
hellohipster-postgresql_1 |
hellohipster-postgresql_1 | Data page checksums are disabled.
hellohipster-postgresql_1 |
hellohipster-postgresql_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
hellohipster-postgresql_1 | creating subdirectories ... ok
hellohipster-postgresql_1 | selecting dynamic shared memory implementation ... posix
hellohipster-postgresql_1 | selecting default max_connections ... 100
hellohipster-postgresql_1 | selecting default shared_buffers ... 128MB
hellohipster-postgresql_1 | selecting default time zone ... Etc/UTC
hellohipster-postgresql_1 | creating configuration files ... ok
hellohipster-postgresql_1 | running bootstrap script ... ok
hellohipster-postgresql_1 | performing post-bootstrap initialization ... ok
hellohipster-postgresql_1 | syncing data to disk ... ok
hellohipster-postgresql_1 |
hellohipster-postgresql_1 | initdb: warning: enabling "trust" authentication for local connections
hellohipster-postgresql_1 | You can change this by editing pg_hba.conf or using the option -A, or
hellohipster-postgresql_1 | --auth-local and --auth-host, the next time you run initdb.
hellohipster-postgresql_1 |
hellohipster-postgresql_1 | Success. You can now start the database server using:
hellohipster-postgresql_1 |
hellohipster-postgresql_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
hellohipster-postgresql_1 |
hellohipster-postgresql_1 | waiting for server to start....2021-02-14 05:57:29.163 UTC [46] LOG: starting PostgreSQL 13.1 (Debian 13.1-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
hellohipster-postgresql_1 | 2021-02-14 05:57:29.164 UTC [46] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
hellohipster-postgresql_1 | 2021-02-14 05:57:29.168 UTC [47] LOG: database system was shut down at 2021-02-14 05:57:28 UTC
hellohipster-postgresql_1 | 2021-02-14 05:57:29.173 UTC [46] LOG: database system is ready to accept connections
hellohipster-postgresql_1 | done
hellohipster-postgresql_1 | server started
hellohipster-postgresql_1 | CREATE DATABASE
hellohipster-postgresql_1 |
hellohipster-postgresql_1 |
hellohipster-postgresql_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
hellohipster-postgresql_1 |
hellohipster-postgresql_1 | 2021-02-14 05:57:29.417 UTC [46] LOG: received fast shutdown request
hellohipster-postgresql_1 | 2021-02-14 05:57:29.418 UTC [46] LOG: aborting any active transactions
hellohipster-postgresql_1 | 2021-02-14 05:57:29.422 UTC [46] LOG: background worker "logical replication launcher" (PID 53) exited with exit code 1
hellohipster-postgresql_1 | 2021-02-14 05:57:29.422 UTC [48] LOG: shutting down
hellohipster-postgresql_1 | waiting for server to shut down....2021-02-14 05:57:29.430 UTC [46] LOG: database system is shut down
hellohipster-postgresql_1 | done
hellohipster-postgresql_1 | server stopped
hellohipster-postgresql_1 |
hellohipster-postgresql_1 | PostgreSQL init process complete; ready for start up.
hellohipster-postgresql_1 |
hellohipster-postgresql_1 | 2021-02-14 05:57:29.544 UTC [1] LOG: starting PostgreSQL 13.1 (Debian 13.1-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
hellohipster-postgresql_1 | 2021-02-14 05:57:29.545 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
hellohipster-postgresql_1 | 2021-02-14 05:57:29.545 UTC [1] LOG: listening on IPv6 address "::", port 5432
hellohipster-postgresql_1 | 2021-02-14 05:57:29.546 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
hellohipster-postgresql_1 | 2021-02-14 05:57:29.549 UTC [74] LOG: database system was shut down at 2021-02-14 05:57:29 UTC
It s not accessable:
on localhost;
on 127.0.0.1 ;
on docker-machine ip default
192.168.99.100;
on this ip
docker inspect -f "{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}" 57293cc4fa40
172.20.0.2
I know that it is a typical issue, however I need a community help to resolve it. When I run docker-compose I get Connection refused: Is the server running on host "db" (172.21.0.2) and accepting web_1 TCP/IP connections on port 5432? Web portion of the docker-compose fails, but db runs. I can connect to the db using localhost:5432. It is not a wait issue, because tiangolo/uvicorn-gunicorn:python3.8-alpine3.10 implemented a wait mechanism - dockerize. Does anyone know where the issue is? Or perhaps could anyone just point me the right direction?
So my Dockerfile is
FROM tiangolo/uvicorn-gunicorn:python3.8-alpine3.10
# copy requirements file
COPY ./requirements.txt /usr/src/app/requirements.txt
# install dependencies
RUN set -eux \
&& apk add --no-cache --virtual .build-deps build-base \
libressl-dev libffi-dev gcc musl-dev python3-dev \
postgresql-dev openssl \
&& pip install --upgrade pip setuptools wheel \
&& pip install -r /usr/src/app/requirements.txt \
&& rm -rf /root/.cache/pip
# copy project
COPY . /app
My docker-compose is:
version: '3.7'
services:
db:
image: postgres:13-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
ports:
- 5432:5432
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=api
web:
build: ./src
ports:
- 80:80
depends_on:
- db
environment:
- DATABASE_URL=postgresql://postgres:postgres#db/api
volumes:
postgres_data:
I added a wait using dockerize, however the web portion fails:
Attaching to gunicorn_db_1, gunicorn_web_1
db_1 | The files belonging to this database system will be owned by user "postgres".
db_1 | This user must also own the server process.
db_1 |
db_1 | The database cluster will be initialized with locale "en_US.utf8".
db_1 | The default database encoding has accordingly been set to "UTF8".
db_1 | The default text search configuration will be set to "english".
db_1 |
db_1 | Data page checksums are disabled.
db_1 |
db_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1 | creating subdirectories ... ok
db_1 | selecting dynamic shared memory implementation ... posix
db_1 | selecting default max_connections ... 100
db_1 | selecting default shared_buffers ... 128MB
web_1 | 2020/08/30 15:33:51 Waiting for: tcp://db:5432
web_1 | 2020/08/30 15:33:51 Problem with dial: dial tcp 172.21.0.2:5432: connect: connection refused. Sleeping 30s
db_1 | selecting default time zone ... Etc/UTC
db_1 | creating configuration files ... ok
db_1 | running bootstrap script ... ok
db_1 | performing post-bootstrap initialization ... ok
db_1 | syncing data to disk ... initdb: warning: enabling "trust" authentication for local connections
db_1 | You can change this by editing pg_hba.conf or using the option -A, or
db_1 | --auth-local and --auth-host, the next time you run initdb.
db_1 | ok
db_1 |
db_1 |
db_1 | Success. You can now start the database server using:
db_1 |
db_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1 |
db_1 | waiting for server to start....2020-08-30 15:33:53.037 UTC [45] LOG: starting PostgreSQL 13beta3 (Debian 13~beta3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1 | 2020-08-30 15:33:53.044 UTC [45] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2020-08-30 15:33:53.059 UTC [46] LOG: database system was shut down at 2020-08-30 15:33:52 UTC
db_1 | 2020-08-30 15:33:53.066 UTC [45] LOG: database system is ready to accept connections
db_1 | done
db_1 | server started
db_1 | CREATE DATABASE
db_1 |
db_1 |
db_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1 |
db_1 | 2020-08-30 15:33:53.399 UTC [45] LOG: received fast shutdown request
db_1 | waiting for server to shut down....2020-08-30 15:33:53.404 UTC [45] LOG: aborting any active transactions
db_1 | 2020-08-30 15:33:53.407 UTC [45] LOG: background worker "logical replication launcher" (PID 52) exited with exit code 1
db_1 | 2020-08-30 15:33:53.409 UTC [47] LOG: shutting down
db_1 | 2020-08-30 15:33:53.438 UTC [45] LOG: database system is shut down
db_1 | done
db_1 | server stopped
db_1 |
db_1 | PostgreSQL init process complete; ready for start up.
db_1 |
db_1 | 2020-08-30 15:33:53.530 UTC [1] LOG: starting PostgreSQL 13beta3 (Debian 13~beta3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1 | 2020-08-30 15:33:53.531 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2020-08-30 15:33:53.531 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2020-08-30 15:33:53.539 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2020-08-30 15:33:53.549 UTC [63] LOG: database system was shut down at 2020-08-30 15:33:53 UTC
db_1 | 2020-08-30 15:33:53.559 UTC [1] LOG: database system is ready to accept connections
web_1 | 2020/08/30 15:34:21 Connected to tcp://db:5432
gunicorn_web_1 exited with code 0
In first look your both files seemed good to me, actually adding a depends on should fix this but, according to documentation that doesn't works that way. it express dependency between containers but that does not mean that a container will wait to other to be ready.
For older versions of Compose we were able to add a health check like this
healthcheck:
test: ["-U postgres"]
interval: 3s
timeout: 30s
retries: 1
But since it's not supported anymore, best option to add a manual wait/sleep to that service.
There is a widely used bash script (wait for it) that you can use to test and wait on the availability of a TCP host and port. It's also recommended solution from the Compose Documentation. You can copy wait for it into your files and starting using it right away.
web:
command: /wait-for-it.sh db:5432
Also you might want to check this out: Wait for it usage with docker
Just change the Version of your Docker-Compose to version: "3.5"
and that solved the problem
I'm starting postgresql 12.3 image via dokcer-compose
I extended the image to execute simple SQL (create schema, table) on start.
From the logs I can see that the SQL commands are executed but when I log to the DB the changes are missing.
My Dockerfile:
FROM postgres
ENV INITDB_DIR /docker-entrypoint-initdb.d
ENV POSTGRES_PASSWORD=...
ENV POSTGRES_DB mydb
COPY initdb.d ${INITDB_DIR}
My docker-compose.yml:
version: "3.7"
services:
postgres:
image: postgres
build: ./postgres
restart: on-failure
ports:
- "5432:5432"
networks:
- postgres
environment:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:...}
- POSTGRES_DB=mydb
networks:
postgres:
driver: bridge
create_schema.sql (located in ./postgres/initdb.d)
create schema "foo";
create table "foo"."message"(
"id" BIGINT NOT NULL,
"value" TEXT
);
alter table "foo"."message" add constraint "foo_message_pk" primary key("id");
And finally the logs produced when executing: docker-compose up --build postgres
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 dynamic shared memory implementation ... posix
postgres_1 | selecting default max_connections ... 100
postgres_1 | selecting default shared_buffers ... 128MB
postgres_1 | selecting default time zone ... Etc/UTC
postgres_1 | creating configuration files ... ok
postgres_1 | running bootstrap script ... ok
postgres_1 | performing post-bootstrap initialization ... ok
postgres_1 | syncing data to disk ... ok
postgres_1 |
postgres_1 |
postgres_1 | Success. You can now start the database server using:
postgres_1 |
postgres_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
postgres_1 |
postgres_1 | initdb: 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 | waiting for server to start....2020-05-27 07:34:00.098 UTC [47] LOG: starting PostgreSQL 12.3 (Debian 12.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
postgres_1 | 2020-05-27 07:34:00.112 UTC [47] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2020-05-27 07:34:00.143 UTC [48] LOG: database system was shut down at 2020-05-27 07:33:59 UTC
postgres_1 | 2020-05-27 07:34:00.151 UTC [47] LOG: database system is ready to accept connections
postgres_1 | done
postgres_1 | server started
postgres_1 | CREATE DATABASE
postgres_1 |
postgres_1 |
postgres_1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/create_schema.sql
postgres_1 | CREATE SCHEMA
postgres_1 | CREATE TABLE
postgres_1 | ALTER TABLE
postgres_1 |
postgres_1 |
postgres_1 | waiting for server to shut down....2020-05-27 07:34:00.588 UTC [47] LOG: received fast shutdown request
postgres_1 | 2020-05-27 07:34:00.593 UTC [47] LOG: aborting any active transactions
postgres_1 | 2020-05-27 07:34:00.595 UTC [47] LOG: background worker "logical replication launcher" (PID 54) exited with exit code 1
postgres_1 | 2020-05-27 07:34:00.595 UTC [49] LOG: shutting down
postgres_1 | 2020-05-27 07:34:00.643 UTC [47] 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 | 2020-05-27 07:34:00.708 UTC [1] LOG: starting PostgreSQL 12.3 (Debian 12.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
postgres_1 | 2020-05-27 07:34:00.709 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2020-05-27 07:34:00.709 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2020-05-27 07:34:00.719 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2020-05-27 07:34:00.744 UTC [74] LOG: database system was shut down at 2020-05-27 07:34:00 UTC
postgres_1 | 2020-05-27 07:34:00.753 UTC [1] LOG: database system is ready to accept connections
When I connect to the database only the default database (postgres) is present. i also tried the above without creating mydb database but still the schema or table were not present.
In one folder I have 4 files: base.py, Dockerfile, docker-compose.yml and wait-for-it.sh.
base.py:
import psycopg2
print("JJJJJJJJ")
conn = psycopg2.connect("dbname='base123' user='postgres' host='db' password='pw1234'")
cur = conn.cursor()
cur.execute("CREATE TABLE test(id serial PRIMARY KEY, num int);")
Dockerfile:
FROM ubuntu:16.04
RUN apt-get update
RUN apt-get -y install python-pip
RUN apt-get update
RUN pip install --upgrade pip
RUN pip install psycopg2-binary
COPY base.py base.py
COPY wait-for-it.sh wait-for-it.sh
RUN chmod +x /wait-for-it.sh
CMD ["python","base.py"]
docker-compose.yml
version: '3'
services:
db:
image: 'postgres:latest'
expose:
- "5432"
ports:
- "5559:5432"
environment:
POSTGRES_PASSWORD: pw1234
POSTGRES_DB: base123
aprrka:
build: .
depends_on:
- db
command: ["./wait-for-it.sh", "db:5432", "--", "python", "base.py"]
wait-fot-it.sh:
link to code in github
After docker-compose up I have this OUTPUT:
...
Creating postgres_db_1 ... done
Creating postgres_aprrka_1 ... done
Attaching to postgres_db_1, postgres_aprrka_1
db_1 | The files belonging to this database system will be owned by user "postgres".
db_1 | This user must also own the server process.
db_1 |
db_1 | The database cluster will be initialized with locale "en_US.utf8".
db_1 | The default database encoding has accordingly been set to "UTF8".
db_1 | The default text search configuration will be set to "english".
db_1 |
db_1 | Data page checksums are disabled.
db_1 |
db_1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1 | creating subdirectories ... ok
db_1 | selecting default max_connections ... 100
db_1 | selecting default shared_buffers ... 128MB
db_1 | selecting dynamic shared memory implementation ... posix
aprrka_1 | wait-for-it.sh: waiting 15 seconds for db:5432
db_1 | creating configuration files ... ok
db_1 | running bootstrap script ... ok
db_1 | performing post-bootstrap initialization ... ok
db_1 | syncing data to disk ... ok
db_1 |
db_1 | WARNING: enabling "trust" authentication for local connections
db_1 | You can change this by editing pg_hba.conf or using the option -A, or
db_1 | --auth-local and --auth-host, the next time you run initdb.
db_1 |
db_1 | Success. You can now start the database server using:
db_1 |
db_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1 |
db_1 | waiting for server to start....2018-08-10 09:59:13.529 UTC [38] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2018-08-10 09:59:13.582 UTC [39] LOG: database system was shut down at 2018-08-10 09:59:12 UTC
db_1 | 2018-08-10 09:59:13.599 UTC [38] LOG: database system is ready to accept connections
db_1 | done
db_1 | server started
db_1 | CREATE DATABASE
db_1 |
db_1 | ALTER ROLE
db_1 |
db_1 |
db_1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1 |
db_1 | 2018-08-10 09:59:14.585 UTC [38] LOG: received fast shutdown request
db_1 | waiting for server to shut down....2018-08-10 09:59:14.593 UTC [38] LOG: aborting any active transactions
db_1 | 2018-08-10 09:59:14.613 UTC [38] LOG: worker process: logical replication launcher (PID 45) exited with exit code 1
db_1 | 2018-08-10 09:59:14.613 UTC [40] LOG: shutting down
db_1 | 2018-08-10 09:59:14.660 UTC [38] LOG: database system is shut down
db_1 | done
db_1 | server stopped
db_1 |
db_1 | PostgreSQL init process complete; ready for start up.
db_1 |
db_1 | 2018-08-10 09:59:14.726 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2018-08-10 09:59:14.726 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2018-08-10 09:59:14.729 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2018-08-10 09:59:14.806 UTC [65] LOG: database system was shut down at 2018-08-10 09:59:14 UTC
db_1 | 2018-08-10 09:59:14.829 UTC [1] LOG: database system is ready to accept connections
db_1 | 2018-08-10 09:59:15.217 UTC [72] LOG: incomplete startup packet
aprrka_1 | wait-for-it.sh: db:5432 is available after 5 seconds
aprrka_1 | JJJJJJJJ
postgres_aprrka_1 exited with code 0
When I type command: psql -U postgres -h localhost -p 5559 in another terminal I can connect to base1234, but when type `\dt' i have no any relations, so my py file don't create table in postgres database. I think that no connection exist with postgres db. Please, help me
You need to close the cursor and commit all pending transactions to the database for the changes to be effective.
Add the following 2 lines at the end of your base.py:
cur.close()
conn.commit()