dockerdockerfilecd

Change directory command in Docker?


In docker I want to do this:

git clone XYZ
cd XYZ
make XYZ

However because there is no cd command, I have to pass in the full path everytime (make XYZ /fullpath). Any good solutions for this?


Solution

  • You can run a script, or a more complex RUN. Here is an adaptation of your example case:

    RUN git clone XYZ && \
        cd XYZ && \
        make XYZ
    

    Here is an example from a Dockerfile I've downloaded to look at previously:

    RUN cd /opt && unzip treeio.zip && mv treeio-master treeio && \
        rm -f treeio.zip && cd treeio && pip install -r requirements.pip
    

    Because of the use of '&&', it will only get to the final 'pip install' command if all the previous commands have succeeded.

    In fact, since every RUN creates a new commit & (currently) an AUFS layer, if you have too many commands in the Dockerfile, you will use up the limits, so merging the RUNs (when the file is stable) can be a very useful thing to do.