【发布时间】:2022-12-20 04:07:37
【问题描述】:
我是 Docker 的新手,作为学习练习,我想通过 Docker 映像制作一个自定义 Python 包。该包名为hashtable-nicolerg,包含一个HashTable类,可以使用from hashtable_nicolerg.hashtable import HashTable导入。
创建一个安装了额外 Python 包的图像很简单:
- 编写 Dockerfile
# Dockerfile FROM python:3 RUN pip install --no-cache-dir hashtable-nicolerg - 构建图像
docker build -t python-hashtable .然而,我意识到的目标并不是 Docker 图像的丰富用例,而是使用户能够在容器的 Python 提示符启动后立即创建
HashTable实例.具体来说,这是当前的行为:
$ docker run -it python-hashtable Python 3.11.0 (main, Nov 15 2022, 19:58:01) [GCC 10.2.1 20210110] on linux Type "help", "copyright", "credits" or "license" for more information. >>> hash_table = HashTable(capacity=100) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'HashTable' is not defined >>> from hashtable_nicolerg.hashtable import HashTable >>> hash_table = HashTable(capacity=100)这是期望的行为:
$ docker run -it python-hashtable Python 3.11.0 (main, Nov 15 2022, 19:58:01) [GCC 10.2.1 20210110] on linux Type "help", "copyright", "credits" or "license" for more information. >>> hash_table = HashTable(capacity=100)我不希望我想象中的用户每次从该图像运行容器时都必须输入
from hashtable_nicolerg.hashtable import HashTable。那么,我是否可以在我的 Docker 镜像中有效地运行from hashtable_nicolerg.hashtable import HashTable,这样用户就不必手动导入这个模块了?同样,我意识到这不是 Docker 镜像最流行的用例。我将此作为练习来了解有关 Python 和 Docker 的更多信息。我将不胜感激任何帮助!
【问题讨论】:
标签: python docker python-import