Как показано на видео с меткой времени 1:29:09, когда человек запускает docker Compose Up в своем терминале, он не получает такой ошибки, как «elt-elt_script-1 завершен с кодом 0».
Однако, когда я запускаю ту же команду в своем терминале, я получаю следующее:
Код: Выделить всё
elt_script-1 exited with code 1
Код: Выделить всё
Traceback (most recent call last):
elt_script-1 | File "elt_script.py", line 54, in \
elt_script-1 | subprocess.run(dump_command, env=subprocess_env, check=True)
elt_script-1 | File "/usr/local/lib/python3.8/subprocess.py", line 516, in run
elt_script-1 | raise CalledProcessError(retcode, process.args,
elt_script-1 | subprocess.CalledProcessError: Command '\['pg_dump', '-h', 'source_postgres', '-U', 'postgres', '-d', 'source_db', '-f', 'data_dump.sql', '-w'\]' returned non-zero exit status 1
Как видно из этой ошибки, проблема возникает при запуске команды в моем скрипте Python (
Код: Выделить всё
elt_script.py). Вот скрипт:
import subprocess
import time
def wait_for_postgres(host, max_retries=5, delay_seconds=5):
retries = 0
while retries < max_retries:
try:
result = subprocess.run(
["pg_isready", "-h", host], check=True, capture_output=True, text=True)
if "accepting connections" in result.stdout:
print("Successfully connected to Postgres")
return True
except subprocess.CalledProcessError as e:
print(f"Error connecting to Postgres: {e}")
retries += 1
print(
f"Retrying in {delay_seconds} seconds... (Attempt {retries}/{max_retries})")
time.sleep(delay_seconds)
print("Max retries reached. Exiting.")
return False
if not wait_for_postgres(host="source_postgres"):
exit(1)
print("Starting ELT script...")
source_config = {
'dbname': 'source_db',
'user': 'postgres',
'password': 'secret',
'host': 'source_postgres'
}
destination_config = {
'dbname': 'destination_db',
'user': 'postgres',
'password': 'secret',
'host': 'destination_postgres'
}
dump_command = [
'pg_dump',
'-h', source_config['host'],
'-U', source_config['user'],
'-d', source_config['dbname'],
'-f', 'data_dump.sql',
'-w'
]
subprocess_env = dict(PGPASSWORD=source_config['password'])
subprocess.run(dump_command, env=subprocess_env, check=True)
load_command = [
'psql',
'-h', destination_config['host'],
'-U', destination_config['user'],
'-d', destination_config['dbname'],
'-a', '-f', 'data_dump.sql',
]
subprocess_env = dict(PGPASSWORD=destination_config['password'])
subprocess.run(load_command, env=subprocess_env, check=True)
print("Ending ELT Script...")
Код: Выделить всё
FROM python:3.8-slim
RUN apt-get update && apt-get install -y postgresql-client
COPY elt_script.py .
CMD [ "python", "elt_script.py" ]
Код: Выделить всё
version: '3'
services:
source_postgres:
image: postgres:18
ports:
- "5433:5432"
networks:
- elt_network
environment:
POSTGRES_DB: source_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: secret
volumes:
- ./source_db_init/init.sql:/docker-entrypoint-initdb.d/init.sql
destination_postgres:
image: postgres:18
ports:
- "5434:5432"
networks:
- elt_network
environment:
POSTGRES_DB: destination_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: secret
volumes:
- ./source_db_init/init.sql:/docker-entrypoint-initdb.d/init.sql
elt_script:
build:
context: ./elt
dockerfile: Dockerfile
command: ["python", "elt_script.py"]
networks:
- elt_network
depends_on:
- source_postgres
- destination_postgres
networks:
elt_network:
driver: bridge
Подробнее здесь: https://stackoverflow.com/questions/798 ... windows-11
Мобильная версия