Change directory command in Docker?

 I want to use these with docker:

git clone XYZ
cd XYZ
make XYZ

But I can't as the cd command is not there. I do not wish to make xyz as the full path everytime, are there any workarounds?

Answered by Ben Butler

Easy ! Just use the WORKDIR command to change the dockerfile change directory you want to. Any other commands you use beyond this command will be executed in the directory you have set.

Here's an example:
RUN git clone XYZ
WORKDIR "/XYZ"
RUN make

It is also a better practice to make use of WORKDIR in docker.

OR

You can do this by using a much more complex RUN command, like given below:

RUN cd /opt && unzip treeio.zip && mv treeio-master treeio &&

    rm -f treeio.zip && cd treeio && pip install -r requirements.pip

And this way it only gets to the last command [ pip install ] only if the previous commands were successfully executed.

Combining the different RUN commands is recommended. This is because every time a RUN command is executed an AUFS layer and a new commit are created and there is a limit for them, so be careful about them.

Hope that helped :smile:



Your Answer

Interviews

Parent Categories