【问题标题】:How to install python libraries in docker file on ubuntu?如何在 ubuntu 的 docker 文件中安装 python 库?
【发布时间】:2022-11-15 01:11:45
【问题描述】:

我想从包含50 库的requirement.txt 文件创建一个包含python 库的docker 映像。没有面对root用户权限如何进行。这是文件:

From ubuntu:latest

RUN apt update
RUN apt install python3 -y
WORKDIR /Destop/DS

# COPY requirement.txt ./
# RUN pip install -r requirement.txt
# it contains only pandas==1.5.1

COPY script2.py ./
CMD ["python3", "./script2.py"]

它在 requiremnt.txt 命令失败

或在 docker compose 中:

version: '3.9'
services:
  pythonapp:
    build: ./
    command: python3 ./script2.py

经过 docker-compose up --bulid

我试过了:

services:
  pythonapp:
    build: ./
    command: python3 ./script2.py
    depends_on:
    - requirements

  requirements:
    image: python:3.7-alpine
    volumes:
      - pip37:/usr/local/lib/python3.7/site-packages
      - .:/
    working_dir: ./
    command: pip install -r requirement.txt

volumes:
  pip37:
    external: true

错误

未找到外部卷“pip37”

【问题讨论】:

  • 它是如何失败的?请提供完整的错误和回溯
  • 作为仅供参考,您可以只使用其中一个 python 图像,请参阅dockerhub

标签: python docker dockerfile


【解决方案1】:

对我来说,Dockerfile 中唯一的问题是RUN apt install python -y。这是Package 'python' has no installation candidate 的错误。

这是预期的,因为python 指的是 Python 的 2.x 版本,它已被弃用并且不再存在于默认的 Ubuntu 存储库中。

将您的 Dockerfile 更改为使用 Python 3.x 版对我来说效果很好。

FROM ubuntu:latest

RUN apt update
RUN apt install python3 python3-pip -y
WORKDIR /Destop/DS

COPY requirement.txt ./
RUN pip3 install -r requirement.txt

COPY script2.py ./
CMD ["python3", "./script2.py"]

为了测试我使用了requirement.txt

pandas==1.5.1

script2.py

import pandas as pd

print(pd.__version__)

通过此构建 docker 映像并从中运行容器成功执行。

docker build -t myimage .
docker run --rm myimage

【讨论】:

  • E: 无法打开锁文件 /var/lib/dpkg/lock-frontend - open (13: Permission denied) E: 无法获取 dpkg 前端锁 (/var/lib/dpkg/lock-frontend),你是 root ?运行时apt install python3 python3-pip -y
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-23
  • 1970-01-01
  • 2022-09-27
  • 2018-08-04
  • 1970-01-01
  • 1970-01-01
  • 2018-09-05
相关资源
最近更新 更多