Docker Compose启动报错:无法找到python可执行文件求助
问题:Docker Compose启动容器提示找不到Python可执行文件
报错信息
failed to create shim task: OCI runtime create failed:unable to start container process: exec: "python": executable file not found in $PATH: unknown
现有配置
Dockerfile(docker/Dockerfile-chap3)
FROM snisioi/retele:2021 LABEL org.opencontainers.image.authors="Sergiu Nisioi <sergiu.nisioi@fmi.unibuc.ro>" USER root RUN apt-get update && apt-get install -y iptables iproute2 RUN apt-get update && apt-get install -y libnetfilter-queue-dev RUN apt-get update && apt-get install -y iperf3 moreutils RUN apt-get update && apt-get install -y python COPY src/*.sh / COPY src/examples/tcp_server.py / CMD ["python", "tcp_server.py"]
docker-compose.yml 容器配置
server: build: context: . dockerfile: docker/Dockerfile-chap3 volumes: - ../capitolul3:/app/src command: python /app/src/examples/tcp_server.py 0.0.0.0 client: build: context: . dockerfile: docker/Dockerfile-chap3 volumes: - ../capitolul3:/app/src tty: true cap_add: - NET_ADMIN - NET_RAW - SYS_ADMIN networks: subnet1: ipv4_address: 172.10.0.2 depends_on: - "router" - "server" sysctls: - net.ipv4.tcp_congestion_control=reno command: python /app/src/examples/tcp_handshake.py server 0.0.0.0
解决方案
1. 修正Dockerfile,确保Python3可用
基础镜像snisioi/retele:2021基于Debian 11+,这类系统中python包默认对应Python2(已被弃用,未正确加入PATH),需安装Python3并建立软链接:
修改Dockerfile中的Python安装命令:
# 替换原python安装行 RUN apt-get update && apt-get install -y python3 python3-pip && ln -s /usr/bin/python3 /usr/bin/python
同步更新CMD命令(可选,若已建立软链接可忽略):
CMD ["python3", "tcp_server.py"]
2. 同步docker-compose.yml中的命令
将command字段里的python替换为python3,确保和容器内可执行文件一致:
# server容器 command: python3 /app/src/examples/tcp_server.py 0.0.0.0 # client容器 command: python3 /app/src/examples/tcp_handshake.py server 0.0.0.0
3. 清理缓存重新构建
避免旧构建缓存干扰,执行:
docker-compose build --no-cache
4. 验证Python路径(可选)
若仍有问题,可在Dockerfile中添加验证步骤,确认Python路径:
RUN which python3 && which python
内容的提问来源于stack exchange,提问作者ionescu radu
相关产品推荐
相关产品推荐

