How to remove directories and files in another layer using Docker?
mkdir dir &amp;&amp; cd dir &amp;&amp; wget http://google.com &amp;&amp; rm -rf dir doesn't do what you think it does.

Let's break it down:


mkdir dir creates dir.
cd dir changes directory into it.
wget http://google.com downloads google.com inside dir/.
rm -rf dir attempts to delete a directory called dir that's in the current directory.


The problem is that there is not dir in the current directory, because you're already cded into it.  And since you've added the -f flag, the command doesn't produce an error.  This is one of the reasons why you shouldn't use force flags without being very sure you need them.

Since you didn't use the WORKDIR command, the directory change is discarded at the end of the RUN.  Thus, the next line executes in the original working directory, where it tries to mkdir dir but, as we've previously discussed, that directory still exists with the wgetted contents inside it.