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

Docker在Ubuntu主机上使用Miniconda环境构建和运行,docker,ubuntu-18.04,miniconda,Docker,Ubuntu 18.04,Miniconda,我正在创建一个docker容器,该容器有一个miniconda环境设置和一些包(pip和conda)。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 \

我正在创建一个docker容器,该容器有一个miniconda环境设置和一些包(pip和conda)。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
我想要的行为是,当容器启动时,它应该自动切换到“dev_env”conda环境,但我一直无法让它工作。日志:

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$ 
正如您在上面所看到的,当我在容器中时,我可以成功地运行“sourceactivatedev_env”,并且环境切换。但我希望在容器启动时自动执行此操作


在构建期间,Dockerfile中也会发生这种情况。同样,我也不确定这是否有任何影响。

您应该使用命令
CMD
处理与运行时相关的任何内容。
RUN
之后键入的任何内容都将仅在图像创建时运行,而不是在您实际运行容器时运行。 用于运行此类命令的shell将在映像创建过程结束时关闭,在这种情况下,环境激活是非持久的

因此,您的附加行可能如下所示:

CMD[“康达激活&&”]

其中
是环境激活后运行时可能需要的其他命令。

此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"]