docker / python-docker-dev

A simple Python app for the Python Language Guide in Docker's Docs
11 stars 32 forks source link

Database Container Crashes Due to Missing Environment Variable in Tutorial compose.yaml #16

Open whokin opened 5 months ago

whokin commented 5 months ago

While following the official tutorial at the documentation hints to add the database password file as an environment variable to the server service and specify the secret file to use.

However, some users might overlook that documentation instructions and uncomment the preset code without manually adding the environment variable. Failing to modify the compose.yaml leads to the database container to fail, preventing the user to complete the tutorial.

Adding the code in the commented out section would make following the tutorial more straightforward.

Error upon launching the container

[+] Running 3/3
 ✔ Network python-docker-dev_default     Created                                                  0.1s
 ✔ Container python-docker-dev-db-1      Created                                                  0.1s
 ✔ Container python-docker-dev-server-1  Created                                                  0.1s
Attaching to db-1, server-1
db-1      |
db-1      | PostgreSQL Database directory appears to contain a database; Skipping initialization
db-1      |
db-1      | 2024-05-27 02:04:48.177 UTC [1] LOG:  starting PostgreSQL 16.3 (Debian 16.3-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
db-1      | 2024-05-27 02:04:48.179 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db-1      | 2024-05-27 02:04:48.179 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db-1      | 2024-05-27 02:04:48.188 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db-1      | 2024-05-27 02:04:48.209 UTC [16] LOG:  database system was shut down at 2024-05-27 02:03:29 UTC
db-1      | 2024-05-27 02:04:48.221 UTC [1] LOG:  database system is ready to accept connections
server-1  |  * Debug mode: off
server-1  | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
server-1  |  * Running on all addresses (0.0.0.0)
server-1  |  * Running on http://127.0.0.1:5000
server-1  |  * Running on http://172.30.0.3:5000
server-1  | Press CTRL+C to quit
^CGracefully stopping... (press Ctrl+C again to force)
[+] Stopping 2/2
 ✔ Container python-docker-dev-server-1  Stopped                                                 10.4s
 ✔ Container python-docker-dev-db-1      Stopped                                                  0.5s
canceled
whokin@brownie:~/docker_tutorial/python-docker-dev$ docker compose restart
[+] Restarting 2/2
 ✔ Container python-docker-dev-server-1  Started                                                  0.6s
 ✔ Container python-docker-dev-db-1      Started                                                  0.6s
whokin@brownie:~/docker_tutorial/python-docker-dev$ docker compose down
[+] Running 3/3
 ✔ Container python-docker-dev-server-1  Removed                                                 10.4s
 ✔ Container python-docker-dev-db-1      Removed                                                  0.5s
 ✔ Network python-docker-dev_default     Removed                                                  0.2s
whokin@brownie:~/docker_tutorial/python-docker-dev$ docker compose up
[+] Running 3/3
 ✔ Network python-docker-dev_default     Created                                                  0.1s
 ✔ Container python-docker-dev-db-1      Created                                                  0.1s
 ✔ Container python-docker-dev-server-1  Created                                                  0.1s
Attaching to db-1, server-1
db-1      |
db-1      | PostgreSQL Database directory appears to contain a database; Skipping initialization
db-1      |
db-1      | 2024-05-27 02:06:20.340 UTC [1] LOG:  starting PostgreSQL 16.3 (Debian 16.3-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
db-1      | 2024-05-27 02:06:20.341 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db-1      | 2024-05-27 02:06:20.341 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db-1      | 2024-05-27 02:06:20.349 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db-1      | 2024-05-27 02:06:20.364 UTC [16] LOG:  database system was shut down at 2024-05-27 02:06:00 UTC
db-1      | 2024-05-27 02:06:20.375 UTC [1] LOG:  database system is ready to accept connections
server-1  | Traceback (most recent call last):
server-1  |   File "<frozen runpy>", line 198, in _run_module_as_main
server-1  |   File "<frozen runpy>", line 88, in _run_code
server-1  |   File "/usr/local/lib/python3.11/site-packages/flask/__main__.py", line 3, in <module>
server-1  |     main()
server-1  |   File "/usr/local/lib/python3.11/site-packages/flask/cli.py", line 1063, in main
server-1  |     cli.main()
server-1  |   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1078, in main
server-1  |     rv = self.invoke(ctx)
server-1  |          ^^^^^^^^^^^^^^^^
server-1  |   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1688, in invoke
server-1  |     return _process_result(sub_ctx.command.invoke(sub_ctx))
server-1  |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
server-1  |   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
server-1  |     return ctx.invoke(self.callback, **ctx.params)
server-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
server-1  |   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 783, in invoke
server-1  |     return __callback(*args, **kwargs)
server-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
server-1  |   File "/usr/local/lib/python3.11/site-packages/click/decorators.py", line 92, in new_func
server-1  |     return ctx.invoke(f, obj, *args, **kwargs)
server-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
server-1  |   File "/usr/local/lib/python3.11/site-packages/click/core.py", line 783, in invoke
server-1  |     return __callback(*args, **kwargs)
server-1  |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
server-1  |   File "/usr/local/lib/python3.11/site-packages/flask/cli.py", line 911, in run_command
server-1  |     raise e from None
server-1  |   File "/usr/local/lib/python3.11/site-packages/flask/cli.py", line 897, in run_command
server-1  |     app = info.load_app()
server-1  |           ^^^^^^^^^^^^^^^
server-1  |   File "/usr/local/lib/python3.11/site-packages/flask/cli.py", line 312, in load_app
server-1  |     app = locate_app(import_name, None, raise_if_not_found=False)
server-1  |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
server-1  |   File "/usr/local/lib/python3.11/site-packages/flask/cli.py", line 218, in locate_app
server-1  |     __import__(module_name)
server-1  |   File "/app/app.py", line 12, in <module>
server-1  |     password = os.environ['POSTGRES_PASSWORD']
server-1  |                ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
server-1  |   File "<frozen os>", line 679, in __getitem__
server-1  | KeyError: 'POSTGRES_PASSWORD'
GovindHede commented 3 months ago

@whokin @nicksieger @rumpl , could you please review this pull request? I have made changes to address the database container crash issue. I would appreciate your feedback on the compose.yaml configuration and the updates to README.md. Thank you!