Docker : RUN cd ... does not work as expected

Docker

Docker Problem Overview


The following Dockerfile :

FROM ubuntu:12.10 RUN mkdir tmp123 RUN cd tmp123 RUN pwd

has the output :

Uploading context 10240 bytes Step 1 : FROM ubuntu:12.10 ---> b750fe79269d Step 2 : RUN mkdir tmp123 ---> Running in d2afac8a11b0 ---> 51e2bbbb5513 Step 3 : RUN cd tmp123 ---> Running in 4762147b207c ---> 644801121b92 Step 4 : RUN pwd ---> Running in 3ed1c0f1049d / ---> eee62a068585

when built (docker build command)

it appears that RUN cd tmp123 has no effect

why ?

Docker Solutions


Solution 1 - Docker

It is actually expected.

A dockerfile is nothing more but a wrapper on docker run + docker commit.

FROM ubuntu:12.10
RUN mkdir tmp123
RUN cd tmp123
RUN pwd

Is the same thing as doing:

CID=$(docker run ubuntu:12.10 mkdir tmp123); ID=$(docker commit $CID)
CID=$(docker run $ID cd tmp123); ID=$(docker commit $CID)
CID=$(docker run $ID pwd); ID=$(docker commit $CID)

Each time you RUN, you spawn a new container and therefore the pwd is '/'.

If you feel like it, you can open an issue on github in order to add a CHDIR instruction to Dockerfile.

Solution 2 - Docker

Maybe you can try this; I am not sure and I can't try it. If it does not work, I hope you don't downvote.

Just:

RUN 'cd tmp123 ; pwd'

Instead of

RUN cd tmp123
RUN pwd

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMax L.View Question on Stackoverflow
Solution 1 - DockercreackView Answer on Stackoverflow
Solution 2 - DockerLidong GuoView Answer on Stackoverflow