Slow exception report in Kemal - crystal-lang

I use crystal 32.1 and Kemal. In case I have runtime exception it take 21 seconds to print backtrace. Any idea why it take so long?
500 GET /chats/by_sale/1 21280.52ms
Backtrace:
web_1 | I, [2020-02-07 17:57:33 +00:00 #1] INFO -- : [1]
web_1 | Exception: Expected string for object name (JSON::Error)
web_1 | from /usr/share/crystal/src/json/builder.cr:308:16 in 'start_scalar'
web_1 | from /usr/share/crystal/src/json/builder.cr:295:11 in 'start_scalar'
web_1 | from /usr/share/crystal/src/json/builder.cr:194:5 in 'start_object'
web_1 | from src/service/json.cr:220:5 in 'object'
web_1 | from src/controller/chat.cr:37:5 in '->'
web_1 | from lib/kemal/src/kemal/route.cr:255:3 in '->'
web_1 | from lib/kemal/src/kemal/route_handler.cr:255:3 in 'process_request'
web_1 | from lib/kemal/src/kemal/route_handler.cr:17:7 in 'call'
web_1 | from /usr/share/crystal/src/http/server/handler.cr:26:7 in 'call_next'
web_1 | from lib/kemal/src/kemal/websocket_handler.cr:13:14 in 'call'
web_1 | from /usr/share/crystal/src/http/server/handler.cr:26:7 in 'call_next'
web_1 | from lib/kemal/src/kemal/filter_handler.cr:21:7 in 'call'
web_1 | from /usr/share/crystal/src/http/server/handler.cr:26:7 in 'call_next'
web_1 | from src/middleware/authenticate.cr:17:7 in 'call'
web_1 | from /usr/share/crystal/src/http/server/handler.cr:26:7 in 'call_next'
web_1 | from lib/kemal/src/kemal/static_file_handler.cr:68:9 in 'call'
web_1 | from /usr/share/crystal/src/http/server/handler.cr:26:7 in 'call_next'
web_1 | from lib/kemal/src/kemal/exception_handler.cr:8:7 in 'call'
web_1 | from /usr/share/crystal/src/http/server/handler.cr:26:7 in 'call_next'
web_1 | from lib/kemal/src/kemal/log_handler.cr:8:37 in 'call'
web_1 | from /usr/share/crystal/src/http/server/handler.cr:26:7 in 'call_next'
web_1 | from lib/kemal/src/kemal/init_handler.cr:12:7 in 'call'
web_1 | from /usr/share/crystal/src/http/server/request_processor.cr:48:11 in 'process'
web_1 | from /usr/share/crystal/src/http/server/request_processor.cr:22:3 in 'process'
web_1 | from /usr/share/crystal/src/http/server.cr:497:5 in 'handle_client'
web_1 | from /usr/share/crystal/src/http/server.cr:463:13 in '->'
web_1 | from /usr/share/crystal/src/fiber.cr:255:3 in 'run'
web_1 | from /usr/share/crystal/src/fiber.cr:47:34 in '->'
web_1 | from ???
web_1 |
web_1 | 2020-02-07 17:57:54 UTC 500 GET /chats/by_sale/1 21280.52ms

Large for a comment so making it an answer.
How I'd handle this is by running your server in gdb, then in the "middle of the 21 second pause" you hit ctrl+c in your server. It'll drop you into gdb. Then run gdb backtrace command and see if it tells you anything. Another option might be to run a profiler on it, there are a few mentioned here: or even the gdb "poor man's profiler" which also works for crystal (in OS X the sample command does something similar FWIW), and run it during the slowdown in question.

Related

docker compose ignores that a service depends on another [duplicate]

This question already has answers here:
Docker Compose wait for container X before starting Y
(20 answers)
Closed last month.
Here's the current docker-compose.yml
version: '3'
services:
db:
image: postgres
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=my_app
ports:
- '5432:5432'
web:
build: .
image: my-app
ports:
- "8000:8000"
depends_on:
- db
command: sh -c "python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
volumes:
- .:/code
environment:
- DB_USER=postgres
- DB_PASSWORD=postgres
- DB_HOST=db
- DB_NAME=my_app
When I run the app for the first time, this happens:
% docker compose build && docker compose up
[+] Building 2.5s (10/10) FINISHED
=> [internal] load build definition from Dockerfile 0.1s
=> => transferring dockerfile: 189B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 150B 0.0s
=> [internal] load metadata for docker.io/library/python:3.11.1-bullseye 2.0s
=> [1/5] FROM docker.io/library/python:3.11.1-bullseye#sha256:cc4910af48 0.0s
=> [internal] load build context 0.2s
=> => transferring context: 1.21MB 0.2s
=> CACHED [2/5] COPY requirements.txt requirements.txt 0.0s
=> CACHED [3/5] RUN pip install -r requirements.txt 0.0s
=> CACHED [4/5] COPY . /app 0.0s
=> CACHED [5/5] WORKDIR /app 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:d7b4a64b01b9de03dec4a0732eaf975b7bc68f1daefb4 0.0s
=> => naming to docker.io/library/my-app 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 3/3
⠿ Network my_app_default Created 0.2s
⠿ Container my_app-db-1 Created 0.1s
⠿ Container my_app-web-1 Created 0.2s
Attaching to my_app-db-1, my_app-web-1
my_app-db-1 | The files belonging to this database system will be owned by user "postgres".
my_app-db-1 | This user must also own the server process.
my_app-db-1 |
my_app-db-1 | The database cluster will be initialized with locale "en_US.utf8".
my_app-db-1 | The default database encoding has accordingly been set to "UTF8".
my_app-db-1 | The default text search configuration will be set to "english".
my_app-db-1 |
my_app-db-1 | Data page checksums are disabled.
my_app-db-1 |
my_app-db-1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
my_app-db-1 | creating subdirectories ... ok
my_app-db-1 | selecting dynamic shared memory implementation ... posix
my_app-db-1 | selecting default max_connections ... 100
my_app-db-1 | selecting default shared_buffers ... 128MB
my_app-db-1 | selecting default time zone ... Etc/UTC
my_app-db-1 | creating configuration files ... ok
my_app-db-1 | running bootstrap script ... ok
my_app-db-1 | performing post-bootstrap initialization ... ok
my_app-db-1 | syncing data to disk ... ok
my_app-db-1 |
my_app-db-1 |
my_app-db-1 | Success. You can now start the database server using:
my_app-db-1 |
my_app-db-1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
my_app-db-1 |
my_app-db-1 | initdb: warning: enabling "trust" authentication for local connections
my_app-db-1 | initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.
my_app-web-1 | Traceback (most recent call last):
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 282, in ensure_connection
my_app-web-1 | self.connect()
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
my_app-web-1 | return func(*args, **kwargs)
my_app-web-1 | ^^^^^^^^^^^^^^^^^^^^^
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 263, in connect
my_app-web-1 | self.connection = self.get_new_connection(conn_params)
my_app-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
my_app-web-1 | return func(*args, **kwargs)
my_app-web-1 | ^^^^^^^^^^^^^^^^^^^^^
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/db/backends/postgresql/base.py", line 215, in get_new_connection
my_app-web-1 | connection = Database.connect(**conn_params)
my_app-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/psycopg2/__init__.py", line 122, in connect
my_app-web-1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
my_app-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
my_app-web-1 | psycopg2.OperationalError: could not connect to server: Connection refused
my_app-web-1 | Is the server running on host "db" (172.24.0.2) and accepting
my_app-web-1 | TCP/IP connections on port 5432?
my_app-web-1 |
my_app-web-1 |
my_app-web-1 | The above exception was the direct cause of the following exception:
my_app-web-1 |
my_app-web-1 | Traceback (most recent call last):
my_app-web-1 | File "/app/manage.py", line 22, in <module>
my_app-web-1 | main()
my_app-web-1 | File "/app/manage.py", line 18, in main
my_app-web-1 | execute_from_command_line(sys.argv)
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
my_app-web-1 | utility.execute()
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/core/management/__init__.py", line 440, in execute
my_app-web-1 | self.fetch_command(subcommand).run_from_argv(self.argv)
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 402, in run_from_argv
my_app-web-1 | self.execute(*args, **cmd_options)
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 448, in execute
my_app-web-1 | output = self.handle(*args, **options)
my_app-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/core/management/base.py", line 96, in wrapped
my_app-web-1 | res = handle_func(*args, **kwargs)
my_app-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/core/management/commands/migrate.py", line 114, in handle
my_app-web-1 | executor = MigrationExecutor(connection, self.migration_progress_callback)
my_app-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/db/migrations/executor.py", line 18, in __init__
my_app-web-1 | self.loader = MigrationLoader(self.connection)
my_app-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/db/migrations/loader.py", line 58, in __init__
my_app-web-1 | self.build_graph()
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/db/migrations/loader.py", line 235, in build_graph
my_app-web-1 | self.applied_migrations = recorder.applied_migrations()
my_app-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/db/migrations/recorder.py", line 81, in applied_migrations
my_app-web-1 | if self.has_table():
my_app-web-1 | ^^^^^^^^^^^^^^^^
my_app-db-1 | waiting for server to start....2023-01-08 12:43:30.440 UTC [47] LOG: starting PostgreSQL 15.1 (Debian 15.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/db/migrations/recorder.py", line 57, in has_table
my_app-web-1 | with self.connection.cursor() as cursor:
my_app-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
my_app-web-1 | return func(*args, **kwargs)
my_app-web-1 | ^^^^^^^^^^^^^^^^^^^^^
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 323, in cursor
my_app-web-1 | return self._cursor()
my_app-web-1 | ^^^^^^^^^^^^^^
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 299, in _cursor
my_app-web-1 | self.ensure_connection()
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
my_app-web-1 | return func(*args, **kwargs)
my_app-web-1 | ^^^^^^^^^^^^^^^^^^^^^
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 281, in ensure_connection
my_app-web-1 | with self.wrap_database_errors:
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__
my_app-web-1 | raise dj_exc_value.with_traceback(traceback) from exc_value
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 282, in ensure_connection
my_app-web-1 | self.connect()
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
my_app-web-1 | return func(*args, **kwargs)
my_app-web-1 | ^^^^^^^^^^^^^^^^^^^^^
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/db/backends/base/base.py", line 263, in connect
my_app-web-1 | self.connection = self.get_new_connection(conn_params)
my_app-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/utils/asyncio.py", line 26, in inner
my_app-web-1 | return func(*args, **kwargs)
my_app-web-1 | ^^^^^^^^^^^^^^^^^^^^^
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/django/db/backends/postgresql/base.py", line 215, in get_new_connection
my_app-web-1 | connection = Database.connect(**conn_params)
my_app-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
my_app-web-1 | File "/usr/local/lib/python3.11/site-packages/psycopg2/__init__.py", line 122, in connect
my_app-web-1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
my_app-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
my_app-db-1 | 2023-01-08 12:43:30.457 UTC [47] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
my_app-web-1 | django.db.utils.OperationalError: could not connect to server: Connection refused
my_app-web-1 | Is the server running on host "db" (172.24.0.2) and accepting
my_app-web-1 | TCP/IP connections on port 5432?
my_app-web-1 |
my_app-db-1 | 2023-01-08 12:43:30.476 UTC [50] LOG: database system was shut down at 2023-01-08 12:43:29 UTC
my_app-db-1 | 2023-01-08 12:43:30.490 UTC [47] LOG: database system is ready to accept connections
my_app-db-1 | done
my_app-db-1 | server started
my_app-db-1 | CREATE DATABASE
my_app-db-1 |
my_app-db-1 |
my_app-db-1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
my_app-db-1 |
my_app-web-1 exited with code 1
my_app-db-1 | 2023-01-08 12:43:31.286 UTC [47] LOG: received fast shutdown request
my_app-db-1 | waiting for server to shut down....2023-01-08 12:43:31.288 UTC [47] LOG: aborting any active transactions
my_app-db-1 | 2023-01-08 12:43:31.319 UTC [47] LOG: background worker "logical replication launcher" (PID 53) exited with exit code 1
my_app-db-1 | 2023-01-08 12:43:31.320 UTC [48] LOG: shutting down
my_app-db-1 | 2023-01-08 12:43:31.326 UTC [48] LOG: checkpoint starting: shutdown immediate
my_app-db-1 | 2023-01-08 12:43:31.469 UTC [48] LOG: checkpoint complete: wrote 918 buffers (5.6%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.054 s, sync=0.076 s, total=0.149 s; sync files=250, longest=0.023 s, average=0.001 s; distance=4217 kB, estimate=4217 kB
my_app-db-1 | 2023-01-08 12:43:31.488 UTC [47] LOG: database system is shut down
my_app-db-1 | done
my_app-db-1 | server stopped
my_app-db-1 |
my_app-db-1 | PostgreSQL init process complete; ready for start up.
my_app-db-1 |
my_app-db-1 | 2023-01-08 12:43:31.692 UTC [1] LOG: starting PostgreSQL 15.1 (Debian 15.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
my_app-db-1 | 2023-01-08 12:43:31.695 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
my_app-db-1 | 2023-01-08 12:43:31.695 UTC [1] LOG: listening on IPv6 address "::", port 5432
my_app-db-1 | 2023-01-08 12:43:31.701 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
my_app-db-1 | 2023-01-08 12:43:31.708 UTC [63] LOG: database system was shut down at 2023-01-08 12:43:31 UTC
my_app-db-1 | 2023-01-08 12:43:31.720 UTC [1] LOG: database system is ready to accept connections
It fails to connect to db because the service hasn't started yet depite being specified as a dependency. Here's the 2nd time:
% docker compose build && docker compose up
[+] Building 4.8s (11/11) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 32B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 34B 0.0s
=> [internal] load metadata for docker.io/library/python:3.11.1-bullseye 4.2s
=> [auth] library/python:pull token for registry-1.docker.io 0.0s
=> [1/5] FROM docker.io/library/python:3.11.1-bullseye#sha256:cc4910af48 0.0s
=> [internal] load build context 0.1s
=> => transferring context: 8.42kB 0.1s
=> CACHED [2/5] COPY requirements.txt requirements.txt 0.0s
=> CACHED [3/5] RUN pip install -r requirements.txt 0.0s
=> CACHED [4/5] COPY . /app 0.0s
=> CACHED [5/5] WORKDIR /app 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:d7b4a64b01b9de03dec4a0732eaf975b7bc68f1daefb4 0.0s
=> => naming to docker.io/library/my-app 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 2/0
⠿ Container my_app-db-1 Created 0.0s
⠿ Container my_app-web-1 Created 0.0s
Attaching to my_app-db-1, my_app-web-1
my_app-db-1 |
my_app-db-1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
my_app-db-1 |
my_app-db-1 | 2023-01-08 12:48:54.148 UTC [1] LOG: starting PostgreSQL 15.1 (Debian 15.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
my_app-db-1 | 2023-01-08 12:48:54.152 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
my_app-db-1 | 2023-01-08 12:48:54.153 UTC [1] LOG: listening on IPv6 address "::", port 5432
my_app-db-1 | 2023-01-08 12:48:54.157 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
my_app-db-1 | 2023-01-08 12:48:54.171 UTC [28] LOG: database system was shut down at 2023-01-08 12:48:30 UTC
my_app-db-1 | 2023-01-08 12:48:54.187 UTC [1] LOG: database system is ready to accept connections
my_app-web-1 | Operations to perform:
my_app-web-1 | Apply all migrations: admin, auth, contenttypes, core, sessions
my_app-web-1 | Running migrations:
my_app-web-1 | Applying contenttypes.0001_initial... OK
my_app-web-1 | Applying auth.0001_initial... OK
my_app-web-1 | Applying admin.0001_initial... OK
my_app-web-1 | Applying admin.0002_logentry_remove_auto_add... OK
my_app-web-1 | Applying admin.0003_logentry_add_action_flag_choices... OK
my_app-web-1 | Applying contenttypes.0002_remove_content_type_name... OK
my_app-web-1 | Applying auth.0002_alter_permission_name_max_length... OK
my_app-web-1 | Applying auth.0003_alter_user_email_max_length... OK
my_app-web-1 | Applying auth.0004_alter_user_username_opts... OK
my_app-web-1 | Applying auth.0005_alter_user_last_login_null... OK
my_app-web-1 | Applying auth.0006_require_contenttypes_0002... OK
my_app-web-1 | Applying auth.0007_alter_validators_add_error_messages... OK
my_app-web-1 | Applying auth.0008_alter_user_username_max_length... OK
my_app-web-1 | Applying auth.0009_alter_user_last_name_max_length... OK
my_app-web-1 | Applying auth.0010_alter_group_name_max_length... OK
my_app-web-1 | Applying auth.0011_update_proxy_permissions... OK
my_app-web-1 | Applying auth.0012_alter_user_first_name_max_length... OK
my_app-web-1 | Applying sessions.0001_initial... OK
my_app-web-1 | Watching for file changes with StatReloader
my_app-web-1 | Performing system checks...
my_app-web-1 |
my_app-web-1 | System check identified no issues (0 silenced).
my_app-web-1 | January 08, 2023 - 12:49:01
my_app-web-1 | Django version 4.1.5, using settings 'my_app.settings'
my_app-web-1 | Starting development server at http://0.0.0.0:8000/
my_app-web-1 | Quit the server with CONTROL-C.
It runs as it should've had in the first time.
According to the docker compose documentation, depends_on it does not wait until the service is ready, only that it has started. In my experience starting a postgres container usually takes a few seconds and since you need to wait until postgres is ready to recieve connections, it is recommended to use some form of control startup.
You could use the example Docker provides or do this yourself in Django, here is an example from a similar stackoverflow thread.

Docker-Compose Init Postgres Failing

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

django runserver command erro

OS: Windows 10
Now, for the project where the runserver command works I get this:
Here is my project structure. i have install all the requirements for this project but still getting this error. I've found something where there were errors with the run server command, but none were satisfying. If this is a duplicate, I do apologies, but I'm pretty sure it isn't. If there's a need for other files and snippets of code, I'll put here everything. Thank you so much.
D:.
| main.py
|
| README.md
|
+---BLL
| | asgi.py
| | requirements.txt
| | settings.py
| | urls.py
| | wsgi.py
| | __init__.py
| |
| \---__pycache__
| settings.cpython-37.pyc
| settings.cpython-39.pyc
| urls.cpython-37.pyc
| urls.cpython-39.pyc
| __init__.cpython-37.pyc
| __init__.cpython-39.pyc
|
+---DAL
| | db.sqlite3
| | __init__.py
| |
| +---account
| | | admin.py
| | | apps.py
| | | forms.py
| | | models.py
| | | tests.py
| | | views.py
| | | __init__.py
| | |
| | +---migrations
| | | | 0001_initial.py
| | | | __init__.py
| | | |
| | | \---__pycache__
| | | 0001_initial.cpython-38.pyc
| | | __init__.cpython-38.pyc
| | |
| | \---__pycache__
| | admin.cpython-37.pyc
| | admin.cpython-38.pyc
| | admin.cpython-39.pyc
| | apps.cpython-37.pyc
| | apps.cpython-38.pyc
| | apps.cpython-39.pyc
| | forms.cpython-38.pyc
| | models.cpython-37.pyc
| | models.cpython-38.pyc
| | models.cpython-39.pyc
| | views.cpython-38.pyc
| | __init__.cpython-37.pyc
| | __init__.cpython-38.pyc
| | __init__.cpython-39.pyc
| |
| \---chatbot
| | admin.py
| | apps.py
| | data.pickle
| | loadAI.py
| | models.py
| | session.json
| | tests.py
| | trainAI.py
| | views.py
| | __init__.py
| |
| +---migrations
| | 0001_initial.py
| | __init__.py
| |
| +---model
| | testmodel.h5
| |
| \---__pycache__
| admin.cpython-37.pyc
| admin.cpython-39.pyc
| apps.cpython-37.pyc
| apps.cpython-39.pyc
| models.cpython-37.pyc
| models.cpython-39.pyc
| trainAI.cpython-37.pyc
| trainAI.cpython-39.pyc
| views.cpython-37.pyc
| views.cpython-39.pyc
| __init__.cpython-37.pyc
| __init__.cpython-39.pyc
|
+---TESTS
| test.py
|
\---UI
| admin.py
| apps.py
| models.py
| tests.py
| urls.py
| views.py
| __init__.py
|
+---images
| 1.PNG
| 10.PNG
| 2.PNG
| 3.PNG
| 4.PNG
| 5.PNG
| 6.PNG
| 7.PNG
| 8.PNG
| 9.PNG
| chatbot.GIF
|
+---static
| \---UI
| +---audio
| | audio0.mp3
| | audio1.mp3
| | audio2.mp3
| | audio3.mp3
| |
| +---css
| | css-main.css
| | css-responsive.css
| |
| \---images
| bg.jpg
| landscape1.jpg
| logo.ico
| logo.jpg
| user.png
|
+---templates
| \---UI
| | base.html
| | index.html
| |
| +---errors
| | 404.html
| | 500.html
| |
| \---pages
| about.html
| account.html
| changepass-done.html
| changepass.html
| chatbot.html
| FAQs.html
| login.html
| signup.html
| update.html
|
\---__pycache__
admin.cpython-37.pyc
admin.cpython-39.pyc
apps.cpython-37.pyc
apps.cpython-39.pyc
models.cpython-37.pyc
models.cpython-39.pyc
urls.cpython-37.pyc
urls.cpython-39.pyc
views.cpython-37.pyc
views.cpython-39.pyc
__init__.cpython-37.pyc
__init__.cpython-39.pyc
I have clone this project from git hub when i use python main.py runserver command it givs me this error:
D:\Covid19Chatbot-master>python main.py runserver
Watching for file changes with StatReloader
Performing system checks...
2021-01-22 17:20:47.257952: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2021-01-22 17:20:47.263540: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
[nltk_data] Downloading package punkt to
[nltk_data] C:\Users\mehra\AppData\Roaming\nltk_data...
[nltk_data] Package punkt is already up-to-date!
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\threading.py", line 926, in _bootstrap_inner
self.run()
File "C:\ProgramData\Anaconda3\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\management\base.py", line 395, in check
include_deployment_checks=include_deployment_checks,
File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\management\base.py", line 382, in _run_checks
return checks.run_checks(**kwargs)
File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\checks\registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\ProgramData\Anaconda3\lib\site-packages\django\urls\resolvers.py", line 407, in check
for pattern in self.url_patterns:
File "C:\ProgramData\Anaconda3\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\ProgramData\Anaconda3\lib\site-packages\django\urls\resolvers.py", line 588, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\ProgramData\Anaconda3\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\ProgramData\Anaconda3\lib\site-packages\django\urls\resolvers.py", line 581, in urlconf_module
return import_module(self.urlconf_name)
File "C:\ProgramData\Anaconda3\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "D:\Covid19Chatbot-master\BLL\urls.py", line 21, in <module>
path('', include('UI.urls')),
File "C:\ProgramData\Anaconda3\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\ProgramData\Anaconda3\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "D:\Covid19Chatbot-master\UI\urls.py", line 4, in <module>
from chatbot.views import (
File "D:\Covid19Chatbot-master\DAL\chatbot\views.py", line 6, in <module>
ai = TrainAI()
File "D:\Covid19Chatbot-master\DAL\chatbot\trainAI.py", line 24, in __init__
jsonContent = json.loads(res.content.decode())
File "C:\ProgramData\Anaconda3\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\ProgramData\Anaconda3\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\ProgramData\Anaconda3\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
The Error is from Lines 22-24 in trainAI
website = r"https://coronavirus-ph-api.herokuapp.com/total/"
res = requests.get(website)
jsonContent = json.loads(res.content.decode())
this website has no content and returns no json, thats why it cannot iterate through it and throws an error

How to build docker image to refer to the python library in venv

I am trying to dockerise my Django web-application, which run on virtual environment venv.
This is my config for my Dockerfile:
FROM ppc64le/python:2.7
ENV PYTHONUNBUFFERED 1
ENV DJANGO_SETTINGS_MODULE=agricultureProj.settings.deploy
ENV UWSGI_VIRTUALENV=/venv
UWSGI_WSGI_FILE=agricultureProj/wsgi.py
UWSGI_HTTP=:8005
UWSGI_MASTER=1
UWSGI_WORKERS=2
UWSGI_THREADS=8
UWSGI_UID=1000
UWSGI_GID=2000
UWSGI_LAZY_APPS=1
UWSGI_WSGI_ENV_BEHAVIOR=holy
RUN virtualenv venv
RUN source /venv/bin/activate
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
EXPOSE 8005
COPY ./docker-entrypoint.sh /
RUN chmod uog+rx ./docker-entrypoint.sh
ENTRYPOINT ["sh","/docker-entrypoint.sh"]
CMD ["/venv/bin/activate", "--http-auto-chunked", "--http-keepalive"
I would like to know how should I define my Dockerfile so that i can directly refer to the existing Python library and successful build and compose run my docker image for this.
**Append:
This is the error I have encountered after the successful docker image built.
Error:
Recreating agricultureproj_db_1
Recreating agricultureproj_web_1
Attaching to agricultureproj_db_1, agricultureproj_web_1
db_1 | /docker-entrypoint.sh: 3: /docker-entrypoint.sh: source: not found
web_1 | ----------
db_1 | ----------
web_1 | ['/home/ppt/Elephas2/agricultureProj_v6_723/agricultureProj',
db_1 | ['/home/ppt/Elephas2/agricultureProj_v6_723/agricultureProj',
web_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7',
db_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7',
web_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/plat-powerpc64le-linux-gnu',
db_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/plat-powerpc64le-linux-gnu',
web_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-tk',
db_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-tk',
web_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-old',
db_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-old',
web_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-dynload',
db_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-dynload',
web_1 | '/usr/lib/python2.7',
db_1 | '/usr/lib/python2.7',
web_1 | '/usr/lib/python2.7/plat-powerpc64le-linux-gnu',
db_1 | '/usr/lib/python2.7/plat-powerpc64le-linux-gnu',
web_1 | '/usr/lib/python2.7/lib-tk',
db_1 | '/usr/lib/python2.7/lib-tk',
web_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/local/lib/python2.7/site-packages',
db_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/local/lib/python2.7/site-packages',
web_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/site-packages',
db_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/site-packages',
web_1 | '/usr/lib/python2.7/dist-packages',
db_1 | '/usr/lib/python2.7/dist-packages',
web_1 | '/usr/lib/python2.7/dist-packages/cv2.powerpc64le-linux-gnu.so']
db_1 | '/usr/lib/python2.7/dist-packages/cv2.powerpc64le-linux-gnu.so']
web_1 | ----------
db_1 | ----------
web_1 | Traceback (most recent call last):
web_1 | File "manage.py", line 37, in <module>
db_1 | Traceback (most recent call last):
web_1 | "Couldn't import Django. Are you sure it's installed and "
db_1 | File "manage.py", line 37, in <module>
web_1 | ImportError: Couldn't import Django. Are you sure it's installed and
available on your PYTHONPATH environment variable? Did you forget to activate
a virtual environment?
db_1 | "Couldn't import Django. Are you sure it's installed and "
web_1 | ----------
db_1 | ImportError: Couldn't import Django. Are you sure it's installed and
available on your PYTHONPATH environment variable? Did you forget to activate
a virtual environment?
web_1 | ['/home/ppt/Elephas2/agricultureProj_v6_723/agricultureProj',
web_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7',
web_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/plat-powerpc64le-linux-gnu',
db_1 | ----------
web_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-tk',
web_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-old',
db_1 | ['/home/ppt/Elephas2/agricultureProj_v6_723/agricultureProj',
web_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-dynload',
web_1 | '/usr/lib/python2.7',
db_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7',
web_1 | '/usr/lib/python2.7/plat-powerpc64le-linux-gnu',
web_1 | '/usr/lib/python2.7/lib-tk',
db_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/plat-powerpc64le-linux-gnu',
web_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/local/lib/python2.7/site-packages',
db_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-tk',
web_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/site-packages',
db_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-old',
web_1 | '/usr/lib/python2.7/dist-packages',
web_1 | '/usr/lib/python2.7/dist-packages/cv2.powerpc64le-linux-gnu.so']
db_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/lib-dynload',
web_1 | ----------
db_1 | '/usr/lib/python2.7',
db_1 | '/usr/lib/python2.7/plat-powerpc64le-linux-gnu',
web_1 | Traceback (most recent call last):
web_1 | File "manage.py", line 37, in <module>
web_1 | "Couldn't import Django. Are you sure it's installed and "
db_1 | '/usr/lib/python2.7/lib-tk',
web_1 | ImportError: Couldn't import Django. Are you sure it's installed and
available on your PYTHONPATH environment variable? Did you forget to activate
a virtual environment?
db_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/local/lib/python2.7/site-packages',
db_1 | '/home/ppt/Elephas2/agricultureProj_v6_723
/agricultureProj/venv/lib/python2.7/site-packages',
db_1 | '/usr/lib/python2.7/dist-packages',
db_1 | '/usr/lib/python2.7/dist-packages/cv2.powerpc64le-linux-gnu.so']
db_1 | ----------
db_1 | Traceback (most recent call last):
db_1 | File "manage.py", line 37, in <module>
db_1 | "Couldn't import Django. Are you sure it's installed and "
db_1 | ImportError: Couldn't import Django. Are you sure it's installed and
available on your PYTHONPATH environment variable? Did you forget to activate
a virtual environment?
agricultureproj_web_1 exited with code 1
agricultureproj_db_1 exited with code 1
ppt#server:~/Elephas2/agricultureProj_v6_723/agricultureProj$ File
"manage.py", line 37, in <module>
-bash: syntax error near unexpected token `newline'
ppt#server:~/Elephas2/agricultureProj_v6_723/agricultureProj$ db_1 |
"Couldn't import Django. Are you sure it's installed and "
db_1: command not found
Couldn't import Django. Are you sure it's installed and : command not found
ppt#server:~/Elephas2/agricultureProj_v6_723/agricultureProj$ db_1 |
ImportError: Couldn't import Django. Are you sure it's installed and
available on
your PYTHONPATH environment variable? Did you forget to activate a virtual
environment?
db_1: command not found
ImportError:: command not found
When I try to docker-compose up the created image, the environment failed to import Django.
Don't activate the virtual environment, just add its bin directory to the start of PATH environment variable. That is generally sufficient.
You should also avoid running your image as root. Even in a container that is not a good practice.
You can use --system-site-packages to indicate venv to use the system libraries in precedence which means change this line and then indicate pip to ignore system installed libraries
RUN virtualenv --system-site-packages venv
RUN source /venv/bin/activate
RUN mkdir /code WORKDIR /code
ADD requirements.txt /code/
RUN pip install --ignore-installed -r requirements.txt

docker-compose django-postgres error

I have exactly followed this tutorial (http://docs.docker.com/compose/django/) but when doing docker-compose up I get this error:
$ docker-compose up
Recreating composeexample_db_1...
Recreating composeexample_web_1...
Attaching to composeexample_db_1, composeexample_web_1
db_1 | LOG: database system was shut down at 2015-07-27 16:17:21 UTC
db_1 | LOG: MultiXact member wraparound protections are now enabled
db_1 | LOG: database system is ready to accept connections
db_1 | LOG: autovacuum launcher started
web_1 | Performing system checks...
web_1 |
web_1 | System check identified no issues (0 silenced).
web_1 | Unhandled exception in thread started by <function wrapper at 0x7f43f0126ed8>
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 225, in wrapper
web_1 | fn(*args, **kwargs)
web_1 | File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 112, in inner_run
web_1 | self.check_migrations()
web_1 | File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 164, in check_migrations
web_1 | executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 19, in __init__
web_1 | self.loader = MigrationLoader(self.connection)
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 47, in __init__
web_1 | self.build_graph()
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 182, in build_graph
web_1 | self.applied_migrations = recorder.applied_migrations()
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
web_1 | self.ensure_schema()
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 49, in ensure_schema
web_1 | if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 162, in cursor
web_1 | cursor = self.make_debug_cursor(self._cursor())
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 135, in _cursor
web_1 | self.ensure_connection()
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
web_1 | six.reraise(dj_exc_type, dj_exc_value, traceback)
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 119, in connect
web_1 | self.connection = self.get_new_connection(conn_params)
web_1 | File "/usr/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 176, in get_new_connection
web_1 | connection = Database.connect(**conn_params)
web_1 | File "/usr/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
web_1 | conn = _connect(dsn, connection_factory=connection_factory, async=async)
web_1 | django.db.utils.OperationalError: could not connect to server: No route to host
web_1 | Is the server running on host "db" (172.17.0.19) and accepting
web_1 | TCP/IP connections on port 5432?
Now, this only happens on my Fedora 21 system(kernel=4.0.4-202.fc21.x86_64) but when I spin up an Ubuntu instance in Virtualbox, it works just fine.
Any pointers?
I've run into the same issue and FWIW if you run the same command again then it works. It's almost as if "docker-compose up" brings up two containers at the same time and the django app container tries to run while postgres container is still being set up? It's mad confusing. :*(
UPDATE:
It seems that my suspicion was right, have a read at https://github.com/docker/compose/issues/374 .
Somewhat crude but a simple workaround to this race condition is to let django app container to sleep for a few seconds before running the command, so that the services that these containers depend on, e.g. PostgreSQL, are ready to accept connections. For example:
command: bash -c "sleep 3 && python manage.py runserver 0.0.0.0:8000" under django service in your yml file for docker-compose.
Another alternative is to run the django service with gunicorn or uwsgi which doesn't seem to instantiate the django application until request is received.
Hope this helps others currently fighting with docker.