1

Inside my Dockerfile:

ENV ENV_NAME=dev_env


RUN conda create -n ${ENV_NAME} python=3.8 pip
SHELL ["conda", "run", "-n", "${ENV_NAME}", "/bin/bash", "-c"]

This would cause error which says ${ENV_NAME} is not a virtual environment name. I would like to know how I can use a variable in a SHELL instruction. I tried to follow How can I use a variable inside a Dockerfile CMD? , but it doesn't work in my case.

CyberPlayerOne
  • 3,078
  • 5
  • 30
  • 51
  • 1
    The SHELL is set and used at build time (for RUN statements in the Dockerfile). So it doesn't make much sense to try to change it at run-time. Can you elaborate on what you're trying to achieve? – Hans Kilian Apr 24 '22 at 17:16
  • @HansKilian I'm trying to install Anaconda in Docker image, init it in bash, create a conda virtual environment, and then activate this newly created conda environment (as in the SHELL line). I'm trying to use a env variable ENV_NAME as the conda environment's name. I'm following this: https://tcoil.info/run-conda-from-virtual-environment-inside-docker-and-start-jupyterlab-with-trading-and-ml-libraries/ – CyberPlayerOne Apr 25 '22 at 06:43
  • You might be looking for [this answer](https://stackoverflow.com/a/51770027/5362795) but I have not tried it for `SHELL`, it works for `RUN`. – Nagev Oct 19 '22 at 18:34

1 Answers1

0

A Docker image contains a fixed version of some single application. That means you don't need to make every possible thing parametrizable; just pick a name and use it.

# do not set ENV ENV_NAME=...
RUN conda create -n env python=3.8 pip
SHELL ["conda", "run", "-n", "env", "/bin/sh", "-c"]

(If you can use plain pip install without a Conda wrapper, that's even easier: don't create a virtual environment and don't bother changing SHELL, just pip install things into the image's "system" Python.)

David Maze
  • 130,717
  • 29
  • 175
  • 215