Docker在Ubuntu主机上的Miniconda环境中构建和运行

时间:2018-07-18 08:27:47

标签: 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 \
    && 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$ 

正如您在上面看到的,当我在容器中时,我可以成功运行“ source activate dev_env”,并且环境会切换。但是我希望在启动容器时自动发生这种情况。

在构建期间,这也会在Dockerfile中发生。同样,我不确定这是否也有效果。

2 个答案:

答案 0 :(得分:0)

对于与运行时相关的任何事情,都应使用命令modelfit1 <- lm(y~b0+b1*x+e, data=d) modelfit1 # Call: # lm(formula = y ~ b0 + b1 * x + e, data = d) # Coefficients: # (Intercept) b0 b1 x e b1:x # 0.19331 NA NA -0.06752 0.02240 NA summary(modelfit1) # Call: # lm(formula = y ~ b0 + b1 * x + e, data = d) # Residuals: # Min 1Q Median 3Q Max # -2.5006 -0.4786 -0.1425 0.6211 1.8488 # Coefficients: (3 not defined because of singularities) # Estimate Std. Error t value Pr(>|t|) # (Intercept) 0.19331 0.25013 0.773 0.450 # b0 NA NA NA NA # b1 NA NA NA NA # x -0.06752 0.21720 -0.311 0.760 # e 0.02240 0.20069 0.112 0.912 # b1:x NA NA NA NA # Residual standard error: 1.115 on 17 degrees of freedom # Multiple R-squared: 0.006657, Adjusted R-squared: -0.1102 # F-statistic: 0.05697 on 2 and 17 DF, p-value: 0.9448
CMD之后键入的任何内容都只会在映像创建时运行,而不是在您实际运行容器时运行。 在图像创建过程结束时,用于运行此类命令的外壳已关闭,从而在这种情况下无法持续进行环境激活。

这样,您的其他行可能如下所示: RUN CMD ["conda activate <your-env-name> && <other commands>"] 是激活环境后在运行时可能需要的其他命令。

答案 1 :(得分:0)

这个docker build文件对我有用。

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