我正在尝试修改此docker compose setup 以安装许多 python 包,包括yfinance。在我看来,有三种安装包的方法,我希望能够在 docker 中使用每一种:
- 从 pip 要求文件安装
- 从 conda environment.yml 文件安装
- 通过运行安装命令(
pip install yfinance
或conda install -c conda-forge beautifulsoup4
)在环境中手动安装
以下是我尝试修改此设置时遇到的问题列表:
- pip 要求文件。- 更改此文件后,似乎没有安装软件包,而是用github 上的默认值覆盖了该文件。
- conda 环境文件 - 无法从environment.yml创建环境和安装包
- 手动安装包 - 使用
docker exec -it <containername> /bin/bash
在 bash 中找不到的 pip 和 conda 返回命令进入 docker 容器外壳。
到目前为止的结果:
在笔记本中导入时,上述所有方法都会导致错误,包括“找不到命令”或“没有名为 yfinance 的模块”。
到目前为止,我能够取得任何成功的唯一方法是在 localhost:8888 的浏览器中打开一个笔记本并创建一个新笔记本并运行
!pip install yfinance
. 但是,导入和执行以下代码也会导致错误,使我认为包或依赖项没有正确安装。将 yfinance 导入为 yf m = yf.Ticker("MSFT") m.info
这是我的 docker-compose 文件服务
jupyter:
image: cmihai/jupyter:v1
container_name: 'joshnotebook'
hostname: jupyter
restart: 'always'
ports:
- '8888:8888'
env_file:
- notebook/config/jupyter.env
volumes:
- ${USERDIR}/docker/notebook:/home/user/notebook
build: ./notebook/services/jupyter
这是我的 Dockerfile
FROM cmihai/python:v1
LABEL name="cmihai/jupyterlab" \
version="1" \
maintainer="Mihai Criveti <crivetimihai@gmail.com>" \
description="Anaconda Python with Jupyter Lab\
This container installs Anaconda Python 3 and Jupyter Lab."
SHELL ["/bin/bash", "-l", "-c"]
ENV DEBIAN_FRONTEND noninteractive
# PARAMETERS
ARG OS_PACKAGES="tzdata curl graphviz vim-nox gosu mc sqlite3 bash-completion sudo"
#ARG LATEX_PACKAGES="texlive texlive-latex-base texlive-latex-extra texlive-xetex texlive-generic-recommended texlive-fonts-extra texlive-fonts-recommended pandoc"
ARG CONDA_PACKAGES="jupyterlab pandas"
COPY condaenvironment.yml /tmp/condaenvironment.yml
# ROOT
USER root
# INSTALL OS PACKAGES
RUN apt-get update \
&& apt-get --no-install-recommends install --yes ${OS_PACKAGES} \ # ${LATEX_PACKAGES}\
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
RUN conda install -c ranaroussi yfinance
# SWITCH TO REGULAR USER
RUN mkdir /home/user/notebook && chown -R user:user /home/user/notebook \
&& echo 'user ALL=NOPASSWD: ALL' >> /etc/sudoers
USER user
# INSTALL CONDA PACKAGES
RUN . ~/.profile \
&& unset SUDO_UID SUDO_GID SUDO_USER \
&& conda install --quiet --yes ${CONDA_PACKAGES} \
&& conda install -c anaconda requests \
&& conda install -c conda-forge lxml \
&& conda install -c conda-forge beautifulsoup4 \
&& conda install -c conda-forge sqlalchemy \
&& conda install -c anaconda mysql-connector-python \
&& conda install -c conda-forge seaborn \
&& conda install -c conda-forge altair \
&& conda clean -y -all
# INSTALL PIP PACKAGES
RUN . ~/.profile \
&& python -m pip install --no-cache-dir --upgrade pip \
&& python -m pip install yfinance
# WORKDIR
WORKDIR /home/user
# JUPYTERLAB EXTENSIONS
RUN jupyter labextension install jupyterlab-drawio \
&& jupyter labextension install jupyterlab_bokeh \
&& jupyter labextension install plotlywidget \
&& jupyter labextension install @jupyterlab/plotly-extension \
&& jupyter labextension install jupyterlab-chart-editor \
&& jupyter labextension install @jupyterlab/git \
&& jupyter serverextension enable --py jupyterlab_git \
&& jupyter labextension install @jupyterlab/latex \
&& jupyter labextension install @jupyterlab/toc \
&& jupyter labextension install @oriolmirosa/jupyterlab_materialdarker \
&& jupyter labextension install @jupyterlab/geojson-extension \
&& jupyter lab build
# COMMAND and ENTRYPOINT:
COPY start-jupyter.sh /home/user/start-jupyter.sh
CMD ["/home/user/start-jupyter.sh"]