You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker启动加载PostgreSQL数据报错及数据库未填充问题

Docker启动PostgreSQL加载数据失败问题

报错信息

python: can't open file '/apps/load_data.py': [Errno 2] No such file or directory

问题背景

本地load_data.py文件路径正确,但数据库始终未被填充。Dockerfile中已将该脚本复制到容器内./load_data.py、./src/scripts/data/load_data.py路径,甚至专门创建apps目录并复制到apps/load_data.py,但全程未在配置中使用过/apps路径。另外,通过PostgreSQL容器的命令行或bash手动加载数据是可行的。

相关配置文件

Dockerfile

# Utilise une image officielle de Python comme image de base
FROM python:3.11-slim

# Définit le répertoire de travail dans le conteneur
WORKDIR /usr/src

# Create the data folder (shouldn't have to do that)
RUN mkdir data
RUN mkdir apps

# Installe les dépendances nécessaires pour psycopg2
RUN apt-get update && apt-get install -y libpq-dev gcc

# Copie les scripts dans le conteneur
COPY ../src/scripts ./scripts

# Copie les fichiers de données dans le conteneur
COPY ../data ./data

# Copier load_data.py
COPY ./src/scripts/load_data.py ./apps/load_data.py

# Copier load_data.py
COPY ./src/scripts/load_data.py ./load_data.py

# Copie le script wait_for_docker.sh dans le répertoire de travail
COPY wait_for_docker.sh ./
RUN chmod +x wait_for_docker.sh

# Installe les dépendances Python via pip (si requirements.txt est présent)
# COPY ./requirements.txt ./requirements.txt
# RUN pip install --no-cache-dir -r requirements.txt

# Définit la commande par défaut pour exécuter le script Python après PostgreSQL est disponible
CMD ["./wait_for_docker.sh", "postgres", "python", "./scripts/data/load_data.py"]

docker-compose.yml

version: '3.11'

services:
  postgres:
    image: postgres:13
    container_name: postgres_container
    environment:
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: admin
      POSTGRES_DB: tsunamis_
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ../src/scripts/data/init.sql:/docker-entrypoint-initdb.d/init.sql  # Mount init.sql to initialize DB
    ports:
      - "5432:5432"
    healthcheck:  # Healthcheck to ensure PostgreSQL is ready
      test: ["CMD-SHELL", "pg_isready -U admin"]
      interval: 10s
      retries: 5

  python-script:
    build:
      context: ..  # The context should be at the root of the directory containing docker-compose.yml
      dockerfile: Dockerfile
    container_name: python_script_container
    depends_on:
      postgres:
        condition: service_healthy  # Wait until PostgreSQL is healthy
    volumes:
      - ../src/scripts:/usr/src/scripts  # Mount the scripts directory
      - ../data:/usr/data  # Mount the CSV files directory
    environment:
      DB_NAME: tsunamis_
      DB_USER: admin
      DB_PASSWORD: admin
      DB_HOST: postgres
      DB_PORT: 5432
    command: ["sh", "-c", "sleep 50 && python load_data.py"]  # Execute the load_data.py script after startup

  pgadmin:
    image: dpage/pgadmin4:latest
    container_name: pgadmin_container
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@admin.com
      PGADMIN_DEFAULT_PASSWORD: admin
    ports:
      - "8080:80"
    depends_on:
      postgres:
        condition: service_healthy  # Wait until PostgreSQL is healthy
    volumes:
      - pgadmin_data:/var/lib/pgadmin

volumes:
  postgres_data:
  pgadmin_data:

问题排查与解决方案

核心问题分析

  1. 命令路径不匹配:docker-compose中python-script服务执行的python load_data.py,默认在容器工作目录/usr/src下查找文件,但通过volume挂载../src/scripts:/usr/src/scripts后,load_data.py实际位于/usr/src/scripts/load_data.py,而非当前工作目录。
  2. 脚本内部硬编码路径:报错指向/apps/load_data.py,但配置中从未使用该路径,大概率是load_data.py脚本内部存在硬编码的路径引用,导致执行时找不到文件。
  3. 冗余等待逻辑:已配置depends_on等待PostgreSQL健康状态,无需额外sleep 50,反而可能引发不必要的延迟或路径问题。

具体修复方案

方案1:修正命令中的脚本路径

将docker-compose.yml中python-script的command修改为:

command: ["python", "./scripts/load_data.py"]

利用容器工作目录/usr/src的相对路径,直接指向挂载后的脚本位置。

方案2:检查并修正脚本内部路径

打开本地load_data.py脚本,查找是否存在/apps/load_data.py这类硬编码路径,替换为容器内实际存在的路径(比如./scripts/load_data.py或/usr/src/scripts/load_data.py)。

方案3:移除冗余挂载(可选)

如果不需要实时同步本地脚本到容器,可以删除python-script服务中的volumes配置,直接使用Dockerfile复制到/usr/src/load_data.py的文件,保持命令为python load_data.py即可。

方案4:优化等待逻辑

删除sleep 50,直接执行脚本,依赖PostgreSQL的健康检查确保服务就绪:

command: ["python", "./scripts/load_data.py"]

内容的提问来源于stack exchange,提问作者Sarah LENET

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 14:02:04