skip to Main Content

A Docker and Linux newbie over here –

I am trying to move a (mini)conda file using Dockerfile. I wrote the following minimal Dockerfile –

FROM ubuntu:22.04

WORKDIR /app

RUN apt-get update && apt-get install -y 
    sudo 
    git 
    wget 
    curl 
    unzip 
    gcc 
    g++


ENV PATH="/root/miniconda3/bin:${PATH}"
ARG PATH="/root/miniconda3/bin:${PATH}"

RUN mkdir -p ~/miniconda3
RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
RUN bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
RUN rm -rf ~/miniconda3/miniconda.sh
RUN ~/miniconda3/bin/conda init bash
RUN conda init
RUN conda create -n myenv python=3.8
RUN echo "conda activate myenv" >> /etc/profile
SHELL ["/bin/bash", "--login", "-c"]

RUN cd ~
RUN wget https://roboti.us/download/mjpro150_linux.zip
RUN unzip mjpro150_linux.zip
RUN mkdir ~/.mujoco
RUN mv mjpro150 ~/.mujoco
RUN wget https://roboti.us/file/mjkey.txt
RUN mv mjkey.txt ~/.mujoco
RUN rm mjpro150_linux.zip

RUN cd /$(whoami)/miniconda3/envs/myenv/lib/
RUN /bin/bash -c "mv libstdc++.so.6 libstdc++.so.6.old"

When I run it –

docker build -t tbd_image .

I get the following error –

 > [21/21] RUN /bin/bash -c "mv libstdc++.so.6 libstdc++.so.6.old":                                                                                                                                                
0.552 usage: conda [-h] [-v] [--no-plugins] [-V] COMMAND ...                                                                                                                                                       
0.552 conda: error: argument COMMAND: invalid choice: 'activate' (choose from 'clean', 'compare', 'config', 'create', 'info', 'init', 'install', 'list', 'notices', 'package', 'remove', 'uninstall', 'rename', 'run', 'search', 'update', 'upgrade', 'content-trust', 'doctor', 'repoquery', 'env')
0.581 mv: cannot stat 'libstdc++.so.6': No such file or directory
------
Dockerfile:38
--------------------
  36 |     
  37 |     RUN cd /$(whoami)/miniconda3/envs/myenv/lib/
  38 | >>> RUN /bin/bash -c "mv libstdc++.so.6 libstdc++.so.6.old"
  39 |     
--------------------
ERROR: failed to solve: process "/bin/bash --login -c /bin/bash -c "mv libstdc++.so.6 libstdc++.so.6.old"" did not complete successfully: exit code: 1

However, if I manually create a container and execute the command, it works perfect.

I went over similar posts from StackOverflow. However, they did not resolve my issue. I noticed one post saying that bash interprets * differently. But I don’t see any * in my file.

2

Answers


  1. As David said in the comment, RUN cd is not performing what you want. You would need to use WORKDIR to achieve that, but you can’t use shell expansion in the WORKDIR directive.

    So, if you don’t know the user in advance (and why it isn’t just root? BTW), you can just simply replace the last RUN directive by this one, and remove the RUN cd directive which is doing nothing:

    RUN /bin/bash -c "mv /$(whoami)/miniconda3/envs/myenv/lib/libstdc++.so.6 /$(whoami)/miniconda3/envs/myenv/lib/libstdc++.so.6.old"
    

    Other options include performing the cd in the same shell as the copy:

    RUN cd /$(whoami)/miniconda3/envs/myenv/lib/ && bash -c "mv libstdc++.so.6 libstdc++.so.6.old"
    

    Or even in the same bash command:

    RUN  /bin/bash -c " cd /$(whoami)/miniconda3/envs/myenv/lib/ && mv libstdc++.so.6 libstdc++.so.6.old"
    

    But I also wonder why you are running that as a bash command at all when you could just do:

    RUN  cd /$(whoami)/miniconda3/envs/myenv/lib/ && mv libstdc++.so.6 libstdc++.so.6.old
    
    Login or Signup to reply.
  2. TL;DR:

    Change:

    RUN cd /$(whoami)/miniconda3/envs/myenv/lib/
    RUN /bin/bash -c "mv libstdc++.so.6 libstdc++.so.6.old"
    

    For:

    RUN cd /$(whoami)/miniconda3/envs/myenv/lib/ && mv libstdc++.so.6 libstdc++.so.6.old
    

    More details:

    You have a few problems. The first one is here:

    RUN conda create -n myenv python=3.8
    

    This is interactive and expects a "y/n" answer. You can tell it to always answer "yes" by passing -y like this:

    RUN conda create -y -n myenv python=3.8
    

    Your second problem is that conda init bash added code to your /root/.bashrc. See this answer for more details on this. That code is not being executed because you have the below:

    RUN echo "conda activate myenv" >> /etc/profile
    SHELL ["/bin/bash", "--login", "-c"]
    

    This makes all future RUN calls process /etc/profile, but without first processing .bashrc which is (now) needed for conda to work properly. That’s why you are seeing all of those "usage" errors.

    (The reason .bashrc isn’t processed is because shells in docker build aren’t interactive.)

    To avoid that, just move those two lines (the profile update and setting SHELL) to the end of your file. The "usage" errors are probably harmless, though, as you aren’t really relying on conda for anything else in this specific Dockerfile.

    Finally, per "TL;DR" above, the (last) error you are having is because you shouldn’t be using cd in a single RUN line. That changes the directory for that particular RUN command, but not for any subsequent command. Look into WORKDIR if you really need that, but also consider putting more commands into a single RUN to avoid several (arguably needless) intermediate images. More on that in this other answer.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search