Related
I've been attempting to make a docker-compose file work, and so far it's been closer and closer, but not quite there. Maybe asking the questions will be useful to someone else in the future. Anyway, the previous thread was here:
Docker-Compose Postgresql Init Entrypoint
From which I learned,
between runs a docker-compose file should have docker compose volumes --down and
if the POSTGRES_DATABASE isn't specified, then the POSTGRES_NAME will replicate to the POSTGRES_DATABASE.
another suggestion from Discord has been to rewrite some of the volume signature to include the file path to init.sql as in docker-entrypoint-initdb.d/init.sql
So if I docker-compose down --volume and then sudo docker-compose build && docker-compose up with
version: "3.9"
services:
db:
restart: always
image: postgres
volumes:
# - ./data/db:/var/lib/postgresql/data
- ./init.sql:/docker-entrypoint-initdb.d/init.db
environment:
- POSTGRES_NAME=dev-postgres
- POSTGRES_USER=pixel
- POSTGRES_DATABASE=lightchan
- POSTGRES_PASSWORD=stardust
web:
build: .
restart: always
command: sh -c "./waitfor.sh db:5432 -- python3 manage.py runserver"
volumes:
- .:/code
ports:
- "8001:8001"
environment:
- POSTGRES_NAME=dev-postgres
- POSTGRES_USER=pixel
- POSTGRES_DATABASE=lightchan
- POSTGRES_PASSWORD=stardust
depends_on:
- db
Then the following is output:
Attaching to lightchan-db-1, lightchan-web-1
lightchan-db-1 | The files belonging to this database system will be owned by user "postgres".
lightchan-db-1 | This user must also own the server process.
lightchan-db-1 |
lightchan-db-1 | The database cluster will be initialized with locale "en_US.utf8".
lightchan-db-1 | The default database encoding has accordingly been set to "UTF8".
lightchan-db-1 | The default text search configuration will be set to "english".
lightchan-db-1 |
lightchan-db-1 | Data page checksums are disabled.
lightchan-db-1 |
lightchan-db-1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
lightchan-db-1 | creating subdirectories ... ok
lightchan-db-1 | selecting dynamic shared memory implementation ... posix
lightchan-db-1 | selecting default max_connections ... 100
lightchan-db-1 | selecting default shared_buffers ... 128MB
lightchan-db-1 | selecting default time zone ... Etc/UTC
lightchan-db-1 | creating configuration files ... ok
lightchan-db-1 | running bootstrap script ... ok
lightchan-db-1 | performing post-bootstrap initialization ... ok
lightchan-db-1 | syncing data to disk ... ok
lightchan-db-1 |
lightchan-db-1 | initdb: warning: enabling "trust" authentication for local connections
lightchan-db-1 | You can change this by editing pg_hba.conf or using the option -A, or
lightchan-db-1 | --auth-local and --auth-host, the next time you run initdb.
lightchan-db-1 |
lightchan-db-1 | Success. You can now start the database server using:
lightchan-db-1 |
lightchan-db-1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
lightchan-db-1 |
lightchan-db-1 | waiting for server to start....2022-03-02 02:08:59.604 UTC [49] LOG: starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
lightchan-db-1 | 2022-03-02 02:08:59.605 UTC [49] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
lightchan-db-1 | 2022-03-02 02:08:59.611 UTC [50] LOG: database system was shut down at 2022-03-02 02:08:59 UTC
lightchan-db-1 | 2022-03-02 02:08:59.617 UTC [49] LOG: database system is ready to accept connections
lightchan-db-1 | done
lightchan-db-1 | server started
lightchan-db-1 | CREATE DATABASE
lightchan-db-1 |
lightchan-db-1 |
lightchan-db-1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
lightchan-db-1 | psql:/docker-entrypoint-initdb.d/init.sql: error: could not read from input file: Is a directory
lightchan-db-1 exited with code 1
lightchan-db-1 | 2022-03-02 02:09:00.594 UTC [28] LOG: database system was not properly shut down; automatic recovery in progress
lightchan-db-1 | 2022-03-02 02:09:00.596 UTC [28] LOG: redo starts at 0/16FB5C8
lightchan-db-1 | 2022-03-02 02:09:00.596 UTC [28] LOG: invalid record length at 0/16FB6C0: wanted 24, got 0
lightchan-db-1 | 2022-03-02 02:09:00.596 UTC [28] LOG: redo done at 0/16FB678 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
lightchan-db-1 | 2022-03-02 02:09:00.604 UTC [1] LOG: database system is ready to accept connections
lightchan-web-1 | Watching for file changes with StatReloader
lightchan-db-1 | 2022-03-02 02:09:01.480 UTC [36] FATAL: database "lightchan" does not exist
So the database can't be seen on the web side but PSQL says it created it. If I uncomment out the data volume on PSQL then I get:
Attaching to lightchan-db-1, lightchan-web-1
lightchan-db-1 |
lightchan-db-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
lightchan-db-1 |
lightchan-db-1 | 2022-03-02 02:11:29.346 UTC [1] LOG: starting PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
lightchan-db-1 | 2022-03-02 02:11:29.346 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
lightchan-db-1 | 2022-03-02 02:11:29.346 UTC [1] LOG: listening on IPv6 address "::", port 5432
lightchan-db-1 | 2022-03-02 02:11:29.359 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
lightchan-db-1 | 2022-03-02 02:11:29.420 UTC [28] LOG: database system was interrupted; last known up at 2022-03-02 02:11:06 UTC
lightchan-web-1 | Watching for file changes with StatReloader
lightchan-db-1 | 2022-03-02 02:11:30.835 UTC [30] FATAL: the database system is starting up
lightchan-web-1 | Exception in thread django-main-thread:
lightchan-web-1 | Traceback (most recent call last):
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
lightchan-web-1 | self.connect()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 25, in inner
lightchan-web-1 | return func(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 211, in connect
lightchan-web-1 | self.connection = self.get_new_connection(conn_params)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 25, in inner
lightchan-web-1 | return func(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 199, in get_new_connection
lightchan-web-1 | connection = Database.connect(**conn_params)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 122, in connect
lightchan-web-1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
lightchan-web-1 | psycopg2.OperationalError: connection to server at "db" (192.168.96.2), port 5432 failed: FATAL: the database system is starting up
lightchan-web-1 |
lightchan-web-1 |
lightchan-web-1 | The above exception was the direct cause of the following exception:
lightchan-web-1 |
lightchan-web-1 | Traceback (most recent call last):
lightchan-web-1 | File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner
lightchan-web-1 | self.run()
lightchan-web-1 | File "/usr/local/lib/python3.8/threading.py", line 870, in run
lightchan-web-1 | self._target(*self._args, **self._kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
lightchan-web-1 | fn(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 127, in inner_run
lightchan-web-1 | self.check_migrations()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 505, in check_migrations
lightchan-web-1 | executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
lightchan-web-1 | self.loader = MigrationLoader(self.connection)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__
lightchan-web-1 | self.build_graph()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 223, in build_graph
lightchan-web-1 | self.applied_migrations = recorder.applied_migrations()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
lightchan-web-1 | if self.has_table():
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
lightchan-web-1 | with self.connection.cursor() as cursor:
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 25, in inner
lightchan-web-1 | return func(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 270, in cursor
lightchan-web-1 | return self._cursor()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 246, in _cursor
lightchan-web-1 | self.ensure_connection()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 25, in inner
lightchan-web-1 | return func(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
lightchan-web-1 | self.connect()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
lightchan-web-1 | raise dj_exc_value.with_traceback(traceback) from exc_value
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection
lightchan-web-1 | self.connect()
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 25, in inner
lightchan-web-1 | return func(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 211, in connect
lightchan-web-1 | self.connection = self.get_new_connection(conn_params)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 25, in inner
lightchan-web-1 | return func(*args, **kwargs)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 199, in get_new_connection
lightchan-web-1 | connection = Database.connect(**conn_params)
lightchan-web-1 | File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 122, in connect
lightchan-web-1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
lightchan-web-1 | django.db.utils.OperationalError: connection to server at "db" (192.168.96.2), port 5432 failed: FATAL: the database system is starting up
lightchan-web-1 |
lightchan-db-1 | 2022-03-02 02:11:32.337 UTC [28] LOG: database system was not properly shut down; automatic recovery in progress
lightchan-db-1 | 2022-03-02 02:11:32.340 UTC [28] LOG: invalid record length at 0/16FF138: wanted 24, got 0
lightchan-db-1 | 2022-03-02 02:11:32.340 UTC [28] LOG: redo is not required
lightchan-db-1 | 2022-03-02 02:11:32.368 UTC [1] LOG: database system is ready to accept connections
Again I used docker-compose volumes --down to clean the errors from the previous thread, but apparently this may cause either the waitfor.sh script to run improperly when the data volumes are included or there's some other error for some reason.
Any ideas anyone? I'm out of debugging options that I know of.
Try opening ports in your db services:
db:
restart: always
image: postgres
ports:
- 5432:5432
I am trying to docker-compose up my containers, one for backend and another one for the database (postgis). If I docker-compose up db, I see db_1 | 2021-11-23 10:36:02.123 UTC [1] LOG: database system is ready to accept connections, so, it works.
But if I docker-compose up the whole project, I get
django.db.utils.OperationalError: could not connect to server: Connection refused
web_1 | Is the server running on host "db" (172.23.0.2) and accepting
web_1 | TCP/IP connections on port 5432?
As far as I know, it means that my backend image does not waiting for until db becomes available, and throws an error. If this idea is correct (is it?), the one of solutions could be:
to add some code to force backend image to wait for db, like it described here: 1Docker-compose up do not start backend after database .
I tried to implement solutions using while loop (see commented lines in docker-compose.yaml), but in my case it doesn't work, and, to be honest, I do not quite understand the "anatomy" of these commands.
I have two subquestions now:
Do I understand my problem correctly?
How to solve it?
Many thanks in advance to anybody who try to help me!
My files are below:
docker-compose.yaml
version: "3.9"
services:
db:
image: postgis/postgis
volumes:
- ./data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgis
- POSTGRES_USER=postgis
- POSTGRES_PASSWORD=postgis
ports:
- 5432:5432
#postgres: 5432
web:
build: .
#command: /wait-for-it.sh db:5432
#something like command: ["./wait-for-it.sh", "db:5432", "--", "./start.sh"]
command: python manage.py runserver 0.0.0.0:8000
volumes:
- ./:/usr/src/[projectname-backend]/
ports:
- "8000:8000"
env_file:
- ./.env.dev
depends_on:
- db
volumes:
db:
Dockerfile
FROM python:3.8.3-alpine
WORKDIR /usr/src/[projectname-backend]
RUN apk update && apk upgrade \
&& apk add postgresql-dev \
gcc \
python3-dev \
musl-dev \
libffi-dev \
&& apk add --repository http://dl-cdn.alpinelinux.org/alpine/edge/testing \
gdal-dev \
geos-dev \
proj-dev \
&& pip install pipenv
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
COPY . .
Logs
% docker-compose down && docker-compose build && docker-compose up
WARNING: Found orphan containers (lista-backend_nginx_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.
Removing lista-backend_web_1 ... done
Removing lista-backend_db_1 ... done
Removing network lista-backend_default
db uses an image, skipping
Building web
[+] Building 7.8s (18/18) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 37B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> resolve image config for docker.io/docker/dockerfile:1 2.5s
=> [auth] docker/dockerfile:pull token for registry-1.docker.io 0.0s
=> CACHED docker-image://docker.io/docker/dockerfile:1#sha256:42399d4635eddd7a9b8a24be879d2f9a930d0ed040a61324cfdf5 0.0s
=> [internal] load .dockerignore 0.0s
=> [internal] load build definition from Dockerfile 0.0s
=> [internal] load metadata for docker.io/library/python:3.8.3-alpine 1.3s
=> [auth] library/python:pull token for registry-1.docker.io 0.0s
=> [1/7] FROM docker.io/library/python:3.8.3-alpine#sha256:c5623df482648cacece4f9652a0ae04b51576c93773ccd43ad459e2a 0.0s
=> [internal] load build context 1.0s
=> => transferring context: 18.30MB 1.0s
=> CACHED [2/7] WORKDIR /usr/src/LISTA_backend 0.0s
=> CACHED [3/7] RUN apk update && apk upgrade && apk add postgresql-dev gcc python3-dev musl-dev 0.0s
=> CACHED [4/7] RUN pip install --upgrade pip 0.0s
=> CACHED [5/7] COPY ./requirements.txt . 0.0s
=> CACHED [6/7] RUN pip install -r requirements.txt 0.0s
=> [7/7] COPY . . 1.6s
=> exporting to image 0.9s
=> => exporting layers 0.9s
=> => writing image sha256:8a5e13ac74a6184b2be21da4269554fc98c677c9a0ee4c11a8989e9027903fec 0.0s
=> => naming to docker.io/library/lista-backend_web 0.0s
Creating network "lista-backend_default" with the default driver
WARNING: Found orphan containers (lista-backend_nginx_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.
Creating lista-backend_db_1 ... done
Creating lista-backend_web_1 ... done
Attaching to lista-backend_db_1, lista-backend_web_1
web_1 | Watching for file changes with StatReloader
web_1 | Performing system checks...
web_1 |
web_1 | System check identified some issues:
web_1 |
web_1 | WARNINGS:
web_1 | api.CustomUser: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
web_1 | HINT: Configure the DEFAULT_AUTO_FIELD setting or the ApiConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
web_1 | listings.Realty: (models.W042) Auto-created primary key used when not defining a primary key type, by default 'django.db.models.AutoField'.
web_1 | HINT: Configure the DEFAULT_AUTO_FIELD setting or the ListingsConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. 'django.db.models.BigAutoField'.
web_1 |
web_1 | System check identified 2 issues (0 silenced).
web_1 | Exception in thread django-main-thread:
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 33, in inner
web_1 | return func(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
web_1 | self.connection = self.get_new_connection(conn_params)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 33, in inner
web_1 | return func(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
web_1 | connection = Database.connect(**conn_params)
web_1 | File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
web_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
web_1 | psycopg2.OperationalError: could not connect to server: Connection refused
web_1 | Is the server running on host "db" (172.27.0.2) and accepting
web_1 | TCP/IP connections on port 5432?
web_1 |
web_1 |
web_1 | The above exception was the direct cause of the following exception:
web_1 |
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.8/threading.py", line 932, in _bootstrap_inner
web_1 | self.run()
web_1 | File "/usr/local/lib/python3.8/threading.py", line 870, in run
web_1 | self._target(*self._args, **self._kwargs)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 64, in wrapper
web_1 | fn(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
web_1 | self.check_migrations()
web_1 | File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 486, in check_migrations
web_1 | executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
web_1 | self.loader = MigrationLoader(self.connection)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__
web_1 | self.build_graph()
web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 220, in build_graph
web_1 | self.applied_migrations = recorder.applied_migrations()
web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
web_1 | if self.has_table():
web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
web_1 | with self.connection.cursor() as cursor:
web_1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 33, in inner
web_1 | return func(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
web_1 | return self._cursor()
web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor
web_1 | self.ensure_connection()
web_1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 33, in inner
web_1 | return func(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
web_1 | raise dj_exc_value.with_traceback(traceback) from exc_value
web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 33, in inner
web_1 | return func(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
web_1 | self.connection = self.get_new_connection(conn_params)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 33, in inner
web_1 | return func(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
web_1 | connection = Database.connect(**conn_params)
web_1 | File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
web_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
web_1 | django.db.utils.OperationalError: could not connect to server: Connection refused
web_1 | Is the server running on host "db" (172.27.0.2) and accepting
web_1 | TCP/IP connections on port 5432?
web_1 |
db_1 |
db_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1 |
db_1 | 2021-11-24 23:19:28.324 UTC [1] LOG: starting PostgreSQL 13.3 (Debian 13.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1 | 2021-11-24 23:19:28.328 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2021-11-24 23:19:28.329 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2021-11-24 23:19:28.336 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2021-11-24 23:19:28.364 UTC [66] LOG: database system was shut down at 2021-11-24 13:55:35 UTC
db_1 | 2021-11-24 23:19:28.400 UTC [1] LOG: database system is ready to accept connections
I faced the same issue and I have resolved it using custom management command
Add this management command on your app
<django_project>/<app_name>/management/commands/wait_for_db.py
"""
Django management command wait_for_database
"""
import sys
from time import sleep, time
from django.core.management.base import BaseCommand, CommandError
from django.db import DEFAULT_DB_ALIAS, connections
from django.db.utils import OperationalError
def wait_for_database(**opts):
"""
The main loop waiting for the database connection to come up.
"""
wait_for_db_seconds = opts['wait_when_down']
alive_check_delay = opts['wait_when_alive']
stable_for_seconds = opts['stable']
timeout_seconds = opts['timeout']
db_alias = opts['database']
conn_alive_start = None
connection = connections[db_alias]
start = time()
while True:
# loop until we have a database connection or we run into a timeout
while True:
try:
connection.cursor().execute('SELECT 1')
if not conn_alive_start:
conn_alive_start = time()
break
except OperationalError as err:
conn_alive_start = None
elapsed_time = int(time() - start)
if elapsed_time >= timeout_seconds:
raise TimeoutError(
'Could not establish database connection.'
) from err
err_message = str(err).strip()
print(f'Waiting for database (cause: {err_message}) ... '
f'{elapsed_time}s',
file=sys.stderr, flush=True)
sleep(wait_for_db_seconds)
uptime = int(time() - conn_alive_start)
print(f'Connection alive for > {uptime}s', flush=True)
if uptime >= stable_for_seconds:
break
sleep(alive_check_delay)
class Command(BaseCommand):
"""
A readiness probe you can use for Kubernetes.
If the database is ready, i.e. willing to accept connections
and handling requests, then this call will exit successfully. Otherwise
the command exits with an error status after reaching a timeout.
"""
help = 'Probes for database availability'
requires_system_checks = False
def add_arguments(self, parser):
parser.add_argument('--timeout', '-t', type=int, default=180,
metavar='SECONDS', action='store',
help='how long to wait for the database before '
'timing out (seconds), default: 180')
parser.add_argument('--stable', '-s', type=int, default=5,
metavar='SECONDS', action='store',
help='how long to observe whether connection '
'is stable (seconds), default: 5')
parser.add_argument('--wait-when-down', '-d', type=int, default=2,
metavar='SECONDS', action='store',
help='delay between checks when database is '
'down (seconds), default: 2')
parser.add_argument('--wait-when-alive', '-a', type=int, default=1,
metavar='SECONDS', action='store',
help='delay between checks when database is '
'up (seconds), default: 1')
parser.add_argument('--database', default=DEFAULT_DB_ALIAS,
action='store', dest='database',
help='which database of `settings.DATABASES` '
'to wait for. Defaults to the "default" '
'database.')
def handle(self, *args, **options):
"""
Wait for a database connection to come up. Exit with error
status when a timeout threshold is surpassed.
"""
try:
wait_for_database(**options)
except TimeoutError as err:
raise CommandError(err) from err
In your docker-compose.yml, run wait_for_db before migrate
command: bash -c "python manage.py wait_for_db; python manage.py migrate;
sample code docker-compose.yml
version: "3.8"
services:
db:
image: postgres
django:
build: django
command: bash -c "python manage.py wait_for_db; python manage.py migrate; daphne -b 0.0.0.0 -p 8001 demo.asgi:application"
volumes:
- ./django:/workdir
expose:
- 29000 # UWSGI application
- 8001 # ASGI application
depends_on:
- db
stdin_open: true
tty: true
My problem was: if I healthcheck my db container,
I also should not forget to add a condition to my depends_on like this:
depends_on:
db:
condition: service_healthy
As mentioned in discussion here. Now it works.
I have completed my Django Project using cookiecutter-django.
If I just locally run:
$ docker-compose -f local.yml build
$ docker-compose -f local.yml up
My project launches just fine in http://0.0.0.0:8000
Nonetheless, now I am trying to deploy it following this guide: https://realpython.com/development-and-deployment-of-cookiecutter-django-via-docker/
I have been able to create the local docker-machine with docker-machine create --driver virtualbox dev, activate it eval $(docker-machine env dev) and build the image, but if I run docker-compose -f local.yml up then I get the following error:
Attaching to innovacion_innsai_postgres_1, innovacion_innsai_django_1, innovacion_innsai_node_1
postgres_1 | 2020-03-12 09:14:42.686 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres_1 | 2020-03-12 09:14:42.686 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres_1 | 2020-03-12 09:14:42.688 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres_1 | 2020-03-12 09:14:42.702 UTC [21] LOG: database system was shut down at 2020-03-11 10:05:23 UTC
postgres_1 | 2020-03-12 09:14:42.732 UTC [1] LOG: database system is ready to accept connections
django_1 | PostgreSQL is available
django_1 | Traceback (most recent call last):
django_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
django_1 | return self.cursor.execute(sql, params)
django_1 | psycopg2.errors.UndefinedTable: relation "innovation_sector" does not exist
django_1 | LINE 1: ...n_sector"."id", "innovation_sector"."sector" FROM "innovatio...
django_1 | ^
django_1 |
django_1 |
django_1 | The above exception was the direct cause of the following exception:
django_1 |
django_1 | Traceback (most recent call last):
django_1 | File "manage.py", line 30, in <module>
django_1 | execute_from_command_line(sys.argv)
django_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
django_1 | utility.execute()
django_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
django_1 | self.fetch_command(subcommand).run_from_argv(self.argv)
django_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
django_1 | self.execute(*args, **cmd_options)
django_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 361, in execute
django_1 | self.check()
django_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 390, in check
django_1 | include_deployment_checks=include_deployment_checks,
django_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 65, in _run_checks
django_1 | issues.extend(super()._run_checks(**kwargs))
django_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 377, in _run_checks
django_1 | return checks.run_checks(**kwargs)
django_1 | File "/usr/local/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
django_1 | new_errors = check(app_configs=app_configs)
django_1 | File "/usr/local/lib/python3.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
django_1 | return check_resolver(resolver)
django_1 | File "/usr/local/lib/python3.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
django_1 | return check_method()
django_1 | File "/usr/local/lib/python3.7/site-packages/django/urls/resolvers.py", line 399, in check
django_1 | for pattern in self.url_patterns:
django_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
django_1 | res = instance.__dict__[self.name] = self.func(instance)
django_1 | File "/usr/local/lib/python3.7/site-packages/django/urls/resolvers.py", line 584, in url_patterns
django_1 | patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
django_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
django_1 | res = instance.__dict__[self.name] = self.func(instance)
django_1 | File "/usr/local/lib/python3.7/site-packages/django/urls/resolvers.py", line 577, in urlconf_module
django_1 | return import_module(self.urlconf_name)
django_1 | File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
django_1 | return _bootstrap._gcd_import(name[level:], package, level)
django_1 | File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
django_1 | File "<frozen importlib._bootstrap>", line 983, in _find_and_load
django_1 | File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
django_1 | File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
django_1 | File "<frozen importlib._bootstrap_external>", line 728, in exec_module
django_1 | File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
django_1 | File "/app/config/urls.py", line 18, in <module>
django_1 | path("", include("innovacion_innsai.innovation.urls", namespace="innovation")),
django_1 | File "/usr/local/lib/python3.7/site-packages/django/urls/conf.py", line 34, in include
django_1 | urlconf_module = import_module(urlconf_module)
django_1 | File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
django_1 | return _bootstrap._gcd_import(name[level:], package, level)
django_1 | File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
django_1 | File "<frozen importlib._bootstrap>", line 983, in _find_and_load
django_1 | File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
django_1 | File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
django_1 | File "<frozen importlib._bootstrap_external>", line 728, in exec_module
django_1 | File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
django_1 | File "/app/innovacion_innsai/innovation/urls.py", line 2, in <module>
django_1 | from innovacion_innsai.innovation import views
django_1 | File "/app/innovacion_innsai/innovation/views.py", line 9, in <module>
django_1 | from .analytics import alimentacion_cases, agro_cases, turismo_cases, movilidad_cases
django_1 | File "/app/innovacion_innsai/innovation/analytics.py", line 17, in <module>
django_1 | for case in Case.objects.filter(sector__sector=sectors[0]):
django_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 308, in __getitem__
django_1 | qs._fetch_all()
django_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 1242, in _fetch_all
django_1 | self._result_cache = list(self._iterable_class(self))
django_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 55, in __iter__
django_1 | results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
django_1 | File "/usr/local/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1133, in execute_sql
django_1 | cursor.execute(sql, params)
django_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 99, in execute
django_1 | return super().execute(sql, params)
django_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
django_1 | return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
django_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
django_1 | return executor(sql, params, many, context)
django_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
django_1 | return self.cursor.execute(sql, params)
django_1 | File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
django_1 | raise dj_exc_value.with_traceback(traceback) from exc_value
django_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
django_1 | return self.cursor.execute(sql, params)
django_1 | django.db.utils.ProgrammingError: relation "innovation_sector" does not exist
django_1 | LINE 1: ...n_sector"."id", "innovation_sector"."sector" FROM "innovatio...
django_1 | ^
django_1 |
innovacion_innsai_django_1 exited with code 1
node_1 |
node_1 | > innovacion_innsai#1.1.0 dev /app
node_1 | > gulp
node_1 |
node_1 | [09:14:48] Using gulpfile /app/gulpfile.js
node_1 | [09:14:48] Starting 'default'...
node_1 | [09:14:48] Starting 'styles'...
node_1 | [09:14:48] Starting 'scripts'...
node_1 | [09:14:48] Starting 'imgCompression'...
node_1 | [09:14:48] gulp-imagemin: Minified 0 images
node_1 | [09:14:48] Finished 'imgCompression' after 34 ms
node_1 | [09:14:48] Finished 'scripts' after 128 ms
node_1 | [09:14:49] Finished 'styles' after 1.09 s
node_1 | [09:14:49] Starting 'initBrowserSync'...
node_1 | [09:14:49] Starting 'watchPaths'...
node_1 | [Browsersync] Proxying: http://django:8000
node_1 | [Browsersync] Access URLs:
node_1 | -----------------------------------
node_1 | Local: http://localhost:3000
node_1 | External: http://172.20.0.4:3000
node_1 | -----------------------------------
node_1 | UI: http://localhost:3001
node_1 | UI External: http://localhost:3001
node_1 | -----------------------------------
In case it might be relevant, if I visit the docker-machine's IP adreess and port (http://192.168.99.101:2376) before launching docker-compose build o up, the page displays "Client sent an HTTP request to an HTTPS server."
Here is my 'local.yml':
version: '3'
volumes:
local_postgres_data: {}
local_postgres_data_backups: {}
services:
django:
build:
context: .
dockerfile: ./compose/local/django/Dockerfile
image: innovacion_innsai_local_django
depends_on:
- postgres
volumes:
- .:/app
env_file:
- ./.envs/.local/.django
- ./.envs/.local/.postgres
ports:
- "8000:8000"
command: /start
postgres:
build:
context: .
dockerfile: ./compose/production/postgres/Dockerfile
image: innovacion_innsai_production_postgres
volumes:
- local_postgres_data:/var/lib/postgresql/data
- local_postgres_data_backups:/backups
env_file:
- ./.envs/.local/.postgres
node:
build:
context: .
dockerfile: ./compose/local/node/Dockerfile
image: innovacion_innsai_local_node
depends_on:
- django
volumes:
- .:/app
# http://jdlm.info/articles/2016/03/06/lessons-building-node-app-docker.html
- /app/node_modules
command: npm run dev
ports:
- "3000:3000"
# Expose browsersync UI: https://www.browsersync.io/docs/options/#option-ui
- "3001:3001"
the result of running docker-machine ls:
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
dev - virtualbox Running tcp://192.168.99.101:2376 v19.03.5
My .postgresql in the .envs/.local folder is:
# PostgreSQL
# ------------------------------------------------------------------------------
POSTGRES_HOST=postgres
POSTGRES_PORT=5432
POSTGRES_DB=innovacion_innsai
POSTGRES_USER=debug
POSTGRES_PASSWORD=debug
and my Docker file for local:
FROM python:3.7-slim-buster
ENV PYTHONUNBUFFERED 1
RUN apt-get update \
# dependencies for building Python packages
&& apt-get install -y build-essential \
# psycopg2 dependencies
&& apt-get install -y libpq-dev \
# Translations dependencies
&& apt-get install -y gettext \
# cleaning up unused files
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& rm -rf /var/lib/apt/lists/*
# Requirements are installed here to ensure they will be cached.
COPY ./requirements /requirements
RUN pip install -r /requirements/local.txt
COPY ./compose/production/django/entrypoint /entrypoint
RUN sed -i 's/\r$//g' /entrypoint
RUN chmod +x /entrypoint
COPY ./compose/local/django/start /start
RUN sed -i 's/\r$//g' /start
RUN chmod +x /start
WORKDIR /app
ENTRYPOINT ["/entrypoint"]
Please, I am quite new with Django and Docker so excuse me if I did not explain myself properly. Also, please let me know if you would need any other script or information in order to take a proper look at the problem!
I dont know if you already figured this out and I don't know if I'm right but give this a try:
Instead of:
docker-compose -f local.yml up
Try:
docker-compose -f production.yml up
When you're deploying to production you should build and bring up the site with the production file not the local file.
Hi I am following the django tutorial (Quickstart: Compose and Django) of .. and I have this error:
SyntaxError: Generator expression must be parenthesized
traceback
root#localhost:~# docker-compose up
Starting root_db_1 ... done
Starting root_web_1 ... done
Attaching to root_db_1, root_web_1
db_1 | 2018-09-09 00:09:10.440 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2018-09-09 00:09:10.440 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2018-09-09 00:09:10.442 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2018-09-09 00:09:10.481 UTC [21] LOG: database system was interrupted; last known up at 2018-09-09 00:06:36 UTC
db_1 | 2018-09-09 00:09:10.597 UTC [21] LOG: database system was not properly shut down; automatic recovery in progress
db_1 | 2018-09-09 00:09:10.599 UTC [21] LOG: redo starts at 0/1633C88
db_1 | 2018-09-09 00:09:10.599 UTC [21] LOG: invalid record length at 0/1633CC0: wanted 24, got 0
db_1 | 2018-09-09 00:09:10.599 UTC [21] LOG: redo done at 0/1633C88
db_1 | 2018-09-09 00:09:10.632 UTC [1] LOG: database system is ready to accept connections
web_1 | Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f652d99d510>
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
web_1 | fn(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
web_1 | autoreload.raise_last_exception()
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 251, in raise_last_exception
web_1 | six.reraise(*_exception)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/six.py", line 685, in reraise
web_1 | raise value.with_traceback(tb)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
web_1 | fn(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/__init__.py", line 27, in setup
web_1 | apps.populate(settings.INSTALLED_APPS)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/apps/registry.py", line 85, in populate
web_1 | app_config = AppConfig.create(entry)
web_1 | File "/usr/local/lib/python3.7/site-packages/django/apps/config.py", line 94, in create
web_1 | module = import_module(entry)
web_1 | File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
web_1 | return _bootstrap._gcd_import(name[level:], package, level)
web_1 | File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
web_1 | File "<frozen importlib._bootstrap>", line 983, in _find_and_load
web_1 | File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
web_1 | File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
web_1 | File "<frozen importlib._bootstrap_external>", line 728, in exec_module
web_1 | File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
web_1 | File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/__init__.py", line 4, in <module>
web_1 | from django.contrib.admin.filters import (
web_1 | File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/filters.py", line 10, in <module>
web_1 | from django.contrib.admin.options import IncorrectLookupParameters
web_1 | File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/options.py", line 13, in <module>
web_1 | from django.contrib.admin import helpers, widgets
web_1 | File "/usr/local/lib/python3.7/site-packages/django/contrib/admin/widgets.py", line 152
web_1 | '%s=%s' % (k, v) for k, v in params.items(),
web_1 | ^
web_1 | SyntaxError: Generator expression must be parenthesized
^CGracefully stopping... (press Ctrl+C again to force)
Stopping root_web_1 ...
Stopping root_db_1 ...
Killing root_web_1 ... done
Killing root_db_1 ... done
Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
docker-compose.yml
version: '3'
services:
db:
image: postgres
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
First sudo service docker restart
And in your Dockerfile
FROM python:3.6
then sudo docker-compose up --build
or
sudo docker-compose run web python manage.py migrate and sudo docker-compose up --build
Update your Django version in requirements.txt file from Django>=1.8,<2.0 to Django==2.1.1
Run your image with rebuild by "docker-compose up --build" it will solve your problem.
I'm creating a docker-compose config for an django app, the Dockerfile builds successfully but when I compose them up, django return an issue -- cannot connect to posgres.
I run docker-compose run web bash, found redis and posgres both cannot be connected.
My docker-compose.yml file:
db:
image: postgres:9.1
environment:
- POSTGRES_PASSWORD=mysecretpassword
redis:
image: redis:2.8
web:
links:
- db
- redis
build: .
volumes:
- .:/workspace
ports:
- "8000:8000“
command: python /workspace/BreadTripServer/webapps/manage.py runserver 0.0.0.0:8000 --settings=configs.local_default
Error info when I do docker-compose up:
sudo docker-compose up
Recreating breadtrip_db_1...
Recreating breadtrip_redis_1...
Recreating breadtrip_web_1...
Attaching to breadtrip_redis_1, breadtrip_web_1
redis_1 | [1] 06 May 06:07:30.469 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
...
redis_1 | [1] 06 May 06:07:30.490 # Server started, Redis version 2.8.19
redis_1 | [1] 06 May 06:07:30.490 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1 | [1] 06 May 06:07:30.490 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1 | [1] 06 May 06:07:30.491 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1 | [1] 06 May 06:07:30.491 * DB loaded from disk: 0.000 seconds
redis_1 | [1] 06 May 06:07:30.491 * The server is now ready to accept connections on port 6379
web_1 | Traceback (most recent call last):
web_1 | File "/workspace/BreadTripServer/webapps/manage.py", line 14, in <module>
web_1 | execute_manager(settings)
web_1 | File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 438, in execute_manager
web_1 | utility.execute()
web_1 | File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 379, in execute
web_1 | self.fetch_command(subcommand).run_from_argv(self.argv)
web_1 | File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 191, in run_from_argv
web_1 | self.execute(*args, **options.__dict__)
web_1 | File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 209, in execute
web_1 | translation.activate('en-us')
web_1 | File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py", line 100, in activate
web_1 | return _trans.activate(language)
web_1 | File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 202, in activate
web_1 | _active.value = translation(language)
web_1 | File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 185, in translation
web_1 | default_translation = _fetch(settings.LANGUAGE_CODE)
web_1 | File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 162, in _fetch
web_1 | app = import_module(appname)
web_1 | File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
web_1 | __import__(name)
web_1 | File "/workspace/BreadTripServer/webapps/lib/haystack/__init__.py", line 83, in <module>
web_1 | backend = load_backend(settings.HAYSTACK_SEARCH_ENGINE)
web_1 | File "/workspace/BreadTripServer/webapps/lib/haystack/__init__.py", line 57, in load_backend
web_1 | return importlib.import_module('haystack.backends.%s_backend' % backend_name)
web_1 | File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
web_1 | __import__(name)
web_1 | File "/workspace/BreadTripServer/webapps/lib/haystack/backends/__init__.py", line 6, in <module>
web_1 | from django.db.models import Q
web_1 | File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 78, in <module>
web_1 | connection = connections[DEFAULT_DB_ALIAS]
web_1 | File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 94, in __getitem__
web_1 | conn = backend.DatabaseWrapper(db, alias)
web_1 | File "/usr/local/lib/python2.7/dist-packages/django/contrib/gis/db/backends/postgis/base.py", line 11, in __init__
web_1 | self.ops = PostGISOperations(self)
web_1 | File "/usr/local/lib/python2.7/dist-packages/django/contrib/gis/db/backends/postgis/operations.py", line 91, in __init__
web_1 | vtup = self.postgis_version_tuple()
web_1 | File "/usr/local/lib/python2.7/dist-packages/django/contrib/gis/db/backends/postgis/operations.py", line 445, in postgis_version_tuple
web_1 | version = self.postgis_lib_version()
web_1 | File "/usr/local/lib/python2.7/dist-packages/django/contrib/gis/db/backends/postgis/operations.py", line 425, in postgis_lib_version
web_1 | return self._get_postgis_func('postgis_lib_version')
web_1 | File "/usr/local/lib/python2.7/dist-packages/django/contrib/gis/db/backends/postgis/operations.py", line 406, in _get_postgis_func
web_1 | cursor = self.connection._cursor()
web_1 | File "/usr/local/lib/python2.7/dist-packages/django/db/backends/postgresql_psycopg2/base.py", line 140, in _cursor
web_1 | self.connection = Database.connect(**conn_params)
web_1 | File "/usr/local/lib/python2.7/dist-packages/psycopg2/__init__.py", line 179, in connect
web_1 | connection_factory=connection_factory, async=async)
web_1 | psycopg2.OperationalError: could not connect to server: Connection refused
web_1 | Is the server running on host "localhost" (::1) and accepting
web_1 | TCP/IP connections on port 5432?
web_1 | could not connect to server: Connection refused
web_1 | Is the server running on host "localhost" (127.0.0.1) and accepting
web_1 | TCP/IP connections on port 5432?
web_1 |
breadtrip_web_1 exited with code 1
Update:
Cause I set a link to other image, docker linked pg on host 172.17.0.67, I need to set the pg host to that address which not mentioned on docker's official docs. (This address would change every time, but can be gotten by env)
Now, I got another issue:
web_1 | Unknown command: 'runserver'
web_1 | Type 'manage.py help' for usage.
breadtrip_web_1 exited with code 1
If I change the command python /workspace/BreadTripServer/webapps/manage.py runserver 0.0.0.0:8000 --settings=configs.local_default to python /workspace/BreadTripServer/webapps/manage.py runserver, it works OK except I cannot reach the web page.
It looks like your application is looking for the Database on localhost. It should be looking for the db at the host db (the host name will have been added to /etc/hosts by the link argument).
somewhere in your BUILD tree there is probably a file called settings.py. you can find this by typing the command :
find . -name settings.py
once found, you need to edit that file. It will look something like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'mydb', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'myuser',
'PASSWORD': 'password',
'HOST': 'localhost', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
see the 'localhost' ? Change that to 'db' (As #Adrian suggested) and you will have this solved. (well, you will move on to the next problem :-) ) You should give Adrian credit for the answer.
I had this issue and was grasping at straws as I came across this SO answer: django.db.utils.OperationalError Could not connect to server.
The solution, which worked for me, was to kill all postgres processes using
sudo pkill postgres
I had this issue and It was only because I was using a VPN. If you are using something like sshuttle just turn it off.