简体   繁体   English

在 Ubuntu 主机上使用 Miniconda 环境构建和运行 Docker

[英]Docker build and run with Miniconda environments on Ubuntu host

I am in the process of creating a docker container which has a miniconda environment setup with some packages (pip and conda).我正在创建一个 docker 容器,该容器具有带有一些包(pip 和 conda)的 miniconda 环境设置。 Dockerfile : Dockerfile :

# Use an official Miniconda runtime as a parent image
FROM continuumio/miniconda3

# Create the conda environment.
# RUN conda create -n dev_env Python=3.6
RUN conda update conda -y \
    && conda create -y -n dev_env Python=3.6 pip

ENV PATH /opt/conda/envs/dev_env/bin:$PATH

RUN /bin/bash -c "source activate dev_env" \
    && pip install azure-cli \
    && conda install -y nb_conda

The behavior I want is that when the container is launched, it should automatically switch to the "dev_env" conda environment but I haven't been able to get this to work.我想要的行为是,当容器启动时,它应该自动切换到“dev_env”conda 环境,但我无法让它工作。 Logs :日志:

dparkar@mymachine:~/src/dev/setupsdk$ docker build .
Sending build context to Docker daemon   2.56kB
Step 1/4 : FROM continuumio/miniconda3
 ---> 1284db959d5d
Step 2/4 : RUN conda update conda -y     && conda create -y -n dev_env Python=3.6 pip
 ---> Using cache
 ---> cb2313f4d8a8
Step 3/4 : ENV PATH /opt/conda/envs/dev_env/bin:$PATH
 ---> Using cache
 ---> 320d4fd2b964
Step 4/4 : RUN /bin/bash -c "source activate dev_env"     && pip install azure-cli     && conda install -y nb_conda
 ---> Using cache
 ---> 3c0299dfbe57
Successfully built 3c0299dfbe57
dparkar@mymachine:~/src/dev/setupsdk$ docker run -it 3c0299dfbe57
(base) root@3db861098892:/# source activate dev_env
(dev_env) root@3db861098892:/# exit
exit
dparkar@mymachine:~/src/dev/setupsdk$ docker run -it 3c0299dfbe57 source activate dev_env
[FATAL tini (7)] exec source failed: No such file or directory
dparkar@mymachine:~/src/dev/setupsdk$ docker run -it 3c0299dfbe57 /bin/bash source activate dev_env
/bin/bash: source: No such file or directory
dparkar@mymachine:~/src/dev/setupsdk$ docker run -it 3c0299dfbe57 /bin/bash "source activate dev_env"
/bin/bash: source activate dev_env: No such file or directory
dparkar@mymachine:~/src/dev/setupsdk$ docker run -it 3c0299dfbe57 /bin/bash -c "source activate dev_env"
dparkar@mymachine:~/src/dev/setupsdk$ 

As you can see above, when I am within the container, I can successfully run "source activate dev_env" and the environment switches over.如上所示,当我在容器内时,我可以成功运行“source activate dev_env”并且环境切换。 But I want this to happen automatically when the container is launched.但是我希望在启动容器时自动发生这种情况。

This also happens in the Dockerfile during build time.这也发生在构建期间的 Dockerfile 中。 Again, I am not sure if that has any effect either.同样,我不确定这是否也有任何影响。

You should use the command CMD for anything related to runtime.您应该将命令CMD用于与运行时相关的任何事情。
Anything typed after RUN will only be run at image creation time, not when you actually run the container.RUN之后键入的任何内容只会在创建映像时运行,而不是在您实际运行容器时运行。 The shell used to run such commands is closed at the end of the image creation process, making the environment activation non-persistent in that case.用于运行此类命令的 shell 在映像创建过程结束时关闭,在这种情况下使环境激活非持久性。

As such, your additional line might look like this: CMD ["conda activate <your-env-name> && <other commands>"] where <other commands> are other commands you might need at runtime after the environment activation.因此,您的附加行可能如下所示: CMD ["conda activate <your-env-name> && <other commands>"]其中<other commands>是您在环境激活后在运行时可能需要的其他命令。

This docker build file worked for me.这个 docker 构建文件对我有用。

# start with miniconda image
FROM continuumio/miniconda3

# setting the working directory 
WORKDIR /usr/src/app

# Copy the file from your host to your current location in container
COPY . /usr/src/app

# Run the command inside your image filesystem to create an environment and name it in the requirements.yml file, in this case "myenv"
RUN conda env create --file requirements.yml

# Activate the environment named "myenv" with shell command
SHELL ["conda", "run", "-n", "myenv", "/bin/bash", "-c"]

# Make sure the environment is activated by testing if you can import flask or any other package you have in your requirements.yml file
RUN echo "Make sure flask is installed:"
RUN python -c "import flask"

# exposing port 8050 for interaction with local host
EXPOSE 8050

#Run your application in the new "myenv" environment
CMD ["conda", "run", "-n", "myenv", "python", "app.py"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM