No module named conda in Docker - python

I am trying to create a docker image with miniconda3 intalled. Instead of using directly the base image offered in docker hub, I want to start from scratch by creating my own Dockerfile and putting there the commands of the Dockerfile of the continuumio/miniconda3 image, which are:
FROM debian:latest
# $ docker build . -t continuumio/miniconda3:latest -t continuumio/miniconda3:4.5.11
# $ docker run --rm -it continuumio/miniconda3:latest /bin/bash
# $ docker push continuumio/miniconda3:latest
# $ docker push continuumio/miniconda3:4.5.11
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
ENV PATH /opt/conda/bin:$PATH
RUN apt-get update --fix-missing && \
apt-get install -y wget bzip2 ca-certificates curl git && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-4.5.11-Linux-x86_64.sh -O ~/miniconda.sh && \
/bin/bash ~/miniconda.sh -b -p /opt/conda && \
rm ~/miniconda.sh && \
/opt/conda/bin/conda clean -tipsy && \
ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh && \
echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc && \
echo "conda activate base" >> ~/.bashrc
ENV TINI_VERSION v0.16.1
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /usr/bin/tini
RUN chmod +x /usr/bin/tini
ENTRYPOINT [ "/usr/bin/tini", "--" ]
CMD [ "/bin/bash" ]
Building and running the container works just fine. For reference, here is the output of conda info in the container:
(base) root#def48bd1ed5d:/# conda info
active environment : base
active env location : /opt/conda
shell level : 1
user config file : /root/.condarc
populated config files :
conda version : 4.5.11
conda-build version : not installed
python version : 3.7.0.final.0
base environment : /opt/conda (writable)
channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/free/linux-64
https://repo.anaconda.com/pkgs/free/noarch
https://repo.anaconda.com/pkgs/r/linux-64
https://repo.anaconda.com/pkgs/r/noarch
https://repo.anaconda.com/pkgs/pro/linux-64
https://repo.anaconda.com/pkgs/pro/noarch
package cache : /opt/conda/pkgs
/root/.conda/pkgs
envs directories : /opt/conda/envs
/root/.conda/envs
platform : linux-64
user-agent : conda/4.5.11 requests/2.19.1 CPython/3.7.0 Linux/4.4.0-187-generic debian/10 glibc/2.28
UID:GID : 0:0
netrc file : None
offline mode : False
The problem appears whenever I try to use conda to install a module, for example conda install jupyter -y. The process starts and at some point during the installation I get this error:
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Traceback (most recent call last):
File "/opt/conda/bin/conda", line 7, in <module>
from conda.cli import main
ModuleNotFoundError: No module named 'conda'
(base) root#def48bd1ed5d:/#
After this, it seems the installation is corrupeted. If I try to use the conda command to call for exmaple conda info again, I get the same error:
(base) root#def48bd1ed5d:/# conda info
Traceback (most recent call last):
File "/opt/conda/bin/conda", line 7, in <module>
from conda.cli import main
ModuleNotFoundError: No module named 'conda'
(base) root#def48bd1ed5d:/#

miniconda3 version specified in installation line inside the Dockerfile is the not the latest one
That Dockerfile that you used to build local image will install miniconda3-4.5.11 not the latest version. You can find it here:
...
RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-4.5.11-Linux-x86_64.sh -O ~/miniconda.sh && \
/bin/bash ~/miniconda.sh -b -p /opt/conda &&
...
And also in this way with docker:
$ docker build --tag miniconda3:test .
$ docker docker run -i -t miniconda3:test /bin/bash
$ docker history --no-trunc miniconda3:test | grep Miniconda3
/bin/sh -c wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-4.5.11-Linux-x86_64.sh -O ~/miniconda.sh && ...
Ok, now let's look at official continuumio/miniconda3:
$ docker run -i -t continuumio/miniconda3 /bin/bash
and then:
$ docker history --no-trunc continuumio/miniconda3 | grep Miniconda3
/bin/sh -c wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh && ...
As you can see continuumio/miniconda3 image from DockerHub installs latest miniconda3 4.8.2 and not the 4.5.11 version. Thus, your local image built from that Dockerfile will produce container with miniconda3:4.5.11.
Change in python version breaks conda
Now, let's figure out why conda fails. First build and run:
$ docker build --tag miniconda3:test .
$ docker docker run -i -t miniconda3:test /bin/bash
Get some info:
(base) root#61cafd17d954:/# conda info
active environment : base
active env location : /opt/conda
shell level : 1
user config file : /root/.condarc
populated config files :
conda version : 4.5.11
conda-build version : not installed
python version : 3.7.0.final.0
base environment : /opt/conda (writable)
channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/free/linux-64
https://repo.anaconda.com/pkgs/free/noarch
https://repo.anaconda.com/pkgs/r/linux-64
https://repo.anaconda.com/pkgs/r/noarch
https://repo.anaconda.com/pkgs/pro/linux-64
https://repo.anaconda.com/pkgs/pro/noarch
package cache : /opt/conda/pkgs
/root/.conda/pkgs
envs directories : /opt/conda/envs
/root/.conda/envs
platform : linux-64
user-agent : conda/4.5.11 requests/2.19.1 CPython/3.7.0 Linux/5.4.0-48-generic debian/10 glibc/2.28
UID:GID : 0:0
netrc file : None
offline mode : False
Well, we have conda:4.5.11 with python:3.7.0.
Now, we are going to install jupyter, for example:
(base) root#61cafd17d954:/# conda install jupyter
You may notice, that this installation will update python:
The following packages will be UPDATED:
...
python: 3.7.0-hc3d631a_0 --> 3.8.5-h7579374_1
...
If you proceed, this will update python and will break conda:
...
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Traceback (most recent call last):
File "/opt/conda/bin/conda", line 7, in <module>
from conda.cli import main
ModuleNotFoundError: No module named 'conda'
This is quite known issue and you can find more info on this issue in How does using conda to install a package change my python version and remove conda? answer, and in official conda repo: No module named conda.cli.main #2463.
Update conda or use Dockerfile for the miniconda3:latest
There are 3 possible solutions to this issue:
Edit your Dockerfile by replacing this line:
RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-4.5.11-Linux-x86_64.sh -O ~/miniconda.sh && \
with
RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh && \
Use the latest official Dockerfile from ContinuumIO, you can find it here.
Update conda inside container before usage:
(base) root#61cafd17d954:/# conda update conda

Related

How can I setup a persistence Python virtual environment in a Dockerfile?

I'm building Python 3.7.4 (It's a hard requirement for other software) on a base Ubuntu 20.04 image using a Dockerfile. I'm following this guide.
Everything works fine if I run the image and follow the guide, but I want to setup my virtual environment in the Dockerfile and have the pip requirements persistent when running the image.
Here's the relevant part of my Dockerfile:
...
RUN echo =============== Building and Install Python =============== \
&& cd /tmp \
&& wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz \
&& tar xvf ./Python-3.7.4.tgz \
&& cd Python-3.7.4 \
&& ./configure --enable-optimizations --with-ensurepip=install \
&& make -j 8 \
&& sudo make install
ENV VIRTUAL_ENV=/opt/python-3.7.4
ENV PATH="$VIRTUAL_ENV:$PATH"
COPY "./hourequirements.txt" /usr/local/
RUN echo =============== Setting up Python Virtual Environment =============== \
&& python3 -m venv $VIRTUAL_ENV \
&& source $VIRTUAL_ENV/bin/activate \
&& pip install --upgrade pip \
&& pip install --no-input -r /usr/local/hourequirements.txt
...
The Dockerfile builds without errors, but when I run the image the environment doesn't exist and python 3.7.4 doesn't show any of the installed requirements.
How can I install Python modules in the virtual environment using PIP in the Dockerfile and have them persist when the docker image runs?
Usual find answer just after post.
I changed:
ENV PATH="$VIRTUAL_ENV:$PATH"
to:
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
in Dockerfile and started working correctly.

"python3": executable file not found in $PATH: unknown." Getting this error while running a dockerfile for a bioinformatics tool

I am trying to create a dockerfile for my NGS tools.
I am working on a cluster and I had installed miniconda3 locally.
Firstly I created a base dockerfile containing the path of miniconda bin and build it.
After that I created the dockerfile for my tool.
This is what my base docker file looks like:
FROM centos:7
ENV PATH="/home/nirmal/new-cluster/miniconda/bin:$PATH"
RUN conda install -y mamba -c conda-forge
RUN conda init bash
RUN echo "conda activate base" >> ~/.bashrc
And this is what my docker image for the tool looks like:
FROM base:latest
RUN echo "source activate mitozEnv" > ~/.bashrc
ENV PATH="/home/nirmal/new-cluster/miniconda/envs/mitozEnv/bin:$PATH"
VOLUME /nfs_master/nirmal/simulated_reads/25K/Hiseq
WORKDIR /nfs_master/nirmal/simulated_reads/25K/Hiseq
CMD ["/bin/bash"]
This is the script that I used:
fq1=sim_hiseq_exponential_R1.fastq
fq2=sim_hiseq_exponential_R1.fastq
outprefix=mitoz
time docker run -v $PWD:$PWD -w $PWD --rm mitoz:latest \
python3 /home/nirmal/tools/MitoZ/version_2.3/release_MitoZ_v2.3/MitoZ.py all \
--genetic_code 2 \
--clade Chordata \
--insert_size 300 \
--thread_number 4 \
--fastq1 $fq1 \
--fastq2 $fq2 \
--outprefix $outprefix \
--fastq_read_length 126 \
1>m.log 2>m.err
But when I tried running it with a small test data I got this error:
docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "python3": executable file not found in $PATH: unknown.
Thanks in advance for your help.

How to solve CDK CLI version mismatch

I'm getting following error:
This CDK CLI is not compatible with the CDK library used by your application. Please upgrade the CLI to the latest version.
(Cloud assembly schema version mismatch: Maximum schema version supported is 8.0.0, but found 9.0.0)
after issuing cdk diff command.
I did run npm install -g aws-cdk#latest after which I've successfully installed new versions of packages: Successfully installed aws-cdk.assets-1.92.0 aws-cdk.aws-apigateway-1.92.0 aws-cdk.aws-apigatewayv2-1.92.0 ... etc. with pip install -r requirements.txt
However after typing cdk --version I'm still getting 1.85.0 (build 5f44668).
My part of setup.py is as follows:
install_requires=[
"aws-cdk.core==1.92.0",
"aws-cdk.aws-ec2==1.92.0",
"aws-cdk.aws_ecs==1.92.0",
"aws-cdk.aws_elasticloadbalancingv2==1.92.0"
],
And I'm stuck now, as downgrading packages setup.py to 1.85.0 throwing ImportError: cannot import name 'CapacityProviderStrategy' from 'aws_cdk.aws_ecs'.
Help :), I would like to use newest packages version.
I encountered this issue with a typescript package, after upgrading the cdk in package.json. As Maciej noted upgrading did not seem to work. I am installing the cdk cli with npm, and an uninstall followed by an install fixed the issue.
npm -g uninstall aws-cdk
npm -g install aws-cdk
So I've fixed it, but is too chaotic to describe the steps.
It seems like there are problems with the symlink
/usr/local/bin/cdk
which was pointing to version 1.85.0 and not the one I updated to 1.92.0.
I removed the aws-cdk from the node_modules and installed it again, then removed the symlink /usr/local/bin/cdk and recreated it manually with
ln -s /usr/lib/node_modules/aws-cdk/bin/cdk /usr/local/bin/cdk
nothing helps for mac OS except this command:
yarn global upgrade aws-cdk#latest
For those coming here that are not using a global install (using cdk from node_modules) and using a mono-repo. This issue is due to the aws-cdk package of the devDependencies not matching the version of the dependencies for the package.
I was using "aws-cdk": "2.18.0" in my root package.json but all my packages were using "aws-cdk-lib": "2.32.1" as their dependencies. By updating the root package.json to use "aws-cdk": "2.31.1" this solved the issue.
I have been experiencing this a few times as well, so i am just dropping this solution which helps me resolves version mismatches, particularly on existing projects.
For Python:
What you need to do is modify the setup.py to specify the latest version.
Either, implicitly
install_requires=[
"aws-cdk.core",
"aws-cdk.aws-ec2",
"aws-cdk.aws_ecs",
"aws-cdk.aws_elasticloadbalancingv2"
],
or explicitly;
install_requires=[
"aws-cdk.core==1.xx.x",
"aws-cdk.aws-ec2==1.xx.x",
"aws-cdk.aws_ecs==1.xx.x",
"aws-cdk.aws_elasticloadbalancingv2==1.xx.x"
],
Then from project root, run;
setup.py install
For TypeScript:
Modify package.json;
"dependencies": {
"#aws-cdk/core" : "latest",
"source-map-support": "^0.5.16"
}
Then run from project root:
npm install
I hope this helps! Please let me know if I need to elaborate or provide more details.
Uninstall the CDK version:
npm uninstall -g aws-cdk
Install specfic version which your application is using. For ex: CDK 1.158.0
npm install -g aws-cdk#1.158.0
If you've made it down here there are 2 options to overcome this issue.
Provision a cloud9 env and run the code there.
Run the app in a container.
Move the cdk app into a folder so that you have a parent folder to put a dockerfile a makefile and a dockercompose file.
fire up docker desktop
cd into the root folder from the terminal and then run make
Dockerfile contents
FROM ubuntu:20.04 as compiler
WORKDIR /app/
RUN apt-get update -y \
&& apt install python3 -y \
&& apt install python3-pip -y \
&& apt install python3-venv -y \
&& python3 -m venv venv
ARG NODE_VERSION=16
RUN ls
RUN apt-get update
RUN apt-get install xz-utils
RUN apt-get -y install curl
RUN apt-get update -y && \
apt-get upgrade -y && \
apt-get install -y && \
curl -sL https://deb.nodesource.com/setup_$NODE_VERSION.x | bash - && \
apt install -y nodejs
RUN apt-get install -y python-is-python3
RUN npm install -g aws-cdk
RUN python -m venv /opt/venv
docker-compose.yaml contents
version: '3.6'
services:
cdk-base:
build: .
image: cdk-base
command: ${COMPOSE_COMMAND:-bash}
volumes:
- .:/app
- /var/run/docker.sock:/var/run/docker.sock #Needed so a docker container can be run from inside a docker container
- ~/.aws/:/root/.aws:ro
Makefile content
SHELL=/bin/bash
CDK_DIR=cdk_app/
COMPOSE_RUN = docker-compose run --rm cdk-base
COMPOSE_UP = docker-compose up
PROFILE = --profile default
all: pre-reqs synth
pre-reqs: _prep-cache container-build npm-install
_prep-cache: #This resolves Error: EACCES: permission denied, open 'cdk.out/tree.json'
mkdir -p cdk_websocket/cdk.out/
container-build: pre-reqs
docker-compose build
container-info:
${COMPOSE_RUN} make _container-info
_container-info:
./containerInfo.sh
clear-cache:
${COMPOSE_RUN} rm -rf ${CDK_DIR}cdk.out && rm -rf ${CDK_DIR}node_modules
cli: _prep-cache
docker-compose run cdk-base /bin/bash
npm-install: _prep-cache
${COMPOSE_RUN} make _npm-install
_npm-install:
cd ${CDK_DIR} && ls && python3 -m venv .venv && source .venv/bin/activate && pip install -r requirements.txt && npm -v && python --version
npm-update: _prep-cache
${COMPOSE_RUN} make _npm-update
_npm-update:
cd ${CDK_DIR} && npm update
synth: _prep-cache
${COMPOSE_RUN} make _synth
_synth:
cd ${CDK_DIR} && source .venv/bin/activate && pip install -r requirements.txt && cdk synth --no-staging ${PROFILE} && cdk deploy --require-approval never ${PROFILE}
bootstrap: _prep-cache
${COMPOSE_RUN} make _bootstrap
_bootstrap:
cd ${CDK_DIR} && source .venv/bin/activate && pip install -r requirements.txt && cdk bootstrap ${PROFILE}
deploy: _prep-cache
${COMPOSE_RUN} make _deploy
_deploy:
cd ${CDK_DIR} && cdk deploy --require-approval never ${PROFILE}
destroy:
${COMPOSE_RUN} make _destroy
_destroy:
cd ${CDK_DIR} && source .venv/bin/activate && pip install -r requirements.txt && cdk destroy --force ${PROFILE}
diff: _prep-cache
${COMPOSE_RUN} make _diff
_diff: _prep-cache
cd ${CDK_DIR} && cdk diff ${PROFILE}
test:
${COMPOSE_RUN} make _test
_test:
cd ${CDK_DIR} && npm test
This worked for me in my dev environment since I had re-cloned the source, I needed to re-run the npm install command. A sample package.json might look like this (update the versions as needed):
{
"dependencies": {
"aws-cdk": "2.27.0",
"node": "^16.14.0"
}
}
I changed typescript": "^4.7.3" version and it worked.
Note: cdk version 2("aws-cdk-lib": "^2.55.0")

Cant run 2 conda commands in Dockerfile

I have a docker file like :
FROM conda/miniconda3-centos7
WORKDIR /tmp
COPY app/ /tmp
RUN conda install gcc_linux-64
RUN conda install gxx_linux-64
CMD ["python", "Hello_World.py"]
The code gets stuck after the first RUN conda command. The error i get is :
WARNING: The conda.compat module is deprecated and will be removed in a future release.
==> WARNING: A newer version of conda exists. <==
current version: 4.6.11
latest version: 4.9.2
Please update conda by running
$ conda update -n base -c defaults conda
Removing intermediate container 277edb28a107
---> e6b51d71eac0
Step 7/8 : RUN conda install gxx_linux-64
---> Running in 94166fbfff2a
Traceback (most recent call last):
File "/usr/local/bin/conda", line 12, in <module>
from conda.cli import main
ModuleNotFoundError: No module named 'conda'
The command '/bin/sh -c conda install gxx_linux-64' returned a non-zero code: 1
Can you please suggest?
Adding conda update -n base -c defaults conda in your Dockerfile solves the mentioned problem.
You could also consider using && for optimizing the creation of docker images. Read more about it here.
An optimized Dockerfile would be:
FROM conda/miniconda3-centos7
WORKDIR /arnav
COPY app/ /arnav
RUN conda update -n base -c defaults conda \
&& conda install gcc_linux-64 && conda install gxx_linux-64
CMD ["python", "Hello_World.py"]

installing yfinance python package in docker container

I am trying to modify this docker compose setup to install a number of python packages including yfinance. In my mind there are three ways to install packages and i'd like to be able to use each one in docker:
install from pip requirements file
install from conda environments.yml file
install manually in environment by running install command (pip install yfinance or conda install -c conda-forge beautifulsoup4)
Here is a list of the problems I am having trying to modify this setup:
pip requirements file. - After altering this file the packages don't appear to be installed and instead the file is overridden with defaults on github.
conda environment file - Unable to create environment and install packages from environment.yml
manual install of package - entering a docker container shell using docker exec -it <containername> /bin/bash results in pip and conda returning command not found in bash.
Results so far:
All of the above methods have resulted in errors including 'command not found' or 'no module named yfinance' when importing in a notebook.
The only way I've been able to have any success so far is opening a notebook in the browser at localhost:8888 and creating a new notebook and running !pip install yfinance. However importing and executing the following code also results in an error making me think the package or dependencies haven't installed correctly.
import yfinance as yf
m = yf.Ticker("MSFT")
m.info
Here is my docker-compose file service
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
Here is my 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"]

Categories

Resources