2

I am a docker novice, so apologies if this is a silly question.

As background, I am using a docker image that I have no ability to edit or change. I run the container with docker run [various-args] [image-name] and the container is launched. If I subsequently run docker exec -it [ID] bash, I can get a shell going from inside the container and it successfully executes a conda environment needed for all my python code, so I can just run python script.py and everything runs okay.

I wanted to automate this process for future use, so I wanted to put these commands into a single script so that I don't need to manually type or execute anything from within the environment. The solution I thought would work was this:

docker exec -it [ID] bash -c "python script.py"

but this doesn't work, giving an import error for the python code. My assumption is that the conda environment is not activated, so I try to execute conda activate my-env, which kicks back a new error of:

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.

So, I follow the instructions and run conda init bash first to see if that helps, but this error still kicks back eventually. Ultimately, it seems like if I execute a bash shell first, then manually start running python code everything is fine, but if I try to do it all at once the conda environment cannot be set up for somme reason. Is there a way to make this work without editing the image itself, or is this something that would require rebuilding the image?

Thanks ins advance!

Josh B.
  • 157
  • 7

1 Answers1

1

The conda activate function is defined by code added to .bashrc by the conda init command. Bash will not source .bashrc unless the -l (--login) flag is used.

However, rather than bothering with shell, Conda provides a conda run command that executes within a specified environment. So, try something like

docker exec -it [ID] conda run -n my-env python script.py

For interactive scripts, one may also need some of the additional conda run flags, such as --live-stream or --no-capture-output. See conda run -h.

merv
  • 67,214
  • 13
  • 180
  • 245
  • Hi, and thank you for your answer! Sorry for responding late; I was working this solution for a bit to see if it could work. You approach doesn't spit back any errors, which is great, but the console after the docker image is created. After I `^C` to cancel out, I see that I canceled out of a conda command and get a `CondaError`. Do you now why this might be? – Josh B. Dec 20 '21 at 14:56
  • @JoshB. have you tried running with the `--live-stream` flag? Sounds to me like it is running, but you're just not seeing the I/O until you kill it (which is what `conda run` does by default). – merv Dec 20 '21 at 17:08
  • 1
    So, after some experimentation, i learned that the `--live-stream` flag is not an available flag for this version of conda within the docker image, so I just let it run for a while to see if the command was saving the files it was intending. After checking inside the container manually, I found that the command was working, even though there was no console output, but it was running exceptionally slowly (about 60x slower than if I had done this manually). Any ideas why this might be? – Josh B. Dec 28 '21 at 18:29
  • @JoshB. Not really. I’ve never had noticeable performance degradation with `conda run` but I also haven’t used it for long-running ops. Could be something specific with the old version of Conda. This probably merits a new question. – merv Dec 28 '21 at 18:34
  • 1
    So long story short, I have found that the version of `conda` within the docker image is the version _just prior_ to when the `--live-stream` flag was added, which is a bummer. This still doesn't explain why executing a bash environment within the docker then running the script gives an output but running it all from the outside doesn't, nor why it runs so slowly. I have settled on this approach for now, but ultimately I can't seem to figure out why these issues are happening. – Josh B. Jan 12 '22 at 16:10
  • Also possible to define the "conda run ..." in the **ENTRYPOINT*, as in this answer: https://stackoverflow.com/a/60148365/6812182 – Mark Loyman Oct 07 '22 at 19:34