[docker] Cannot execute RUN mkdir in a Dockerfile

This is an error message I get when building a Docker image:

Step 18 : RUN mkdir /var/www/app && chown luqo33:www-data /var/www/app
---> Running in 7b5854406120 mkdir: cannot create directory '/var/www/app': No such file or directory

This is a fragment of Dockerfile that causes the error:

FROM ubuntu:14.04
RUN groupadd -r luqo33 && useradd -r -g luqo33 luqo33

<installing nginx, fpm, php and a couple of other things>

RUN mkdir /var/www/app && chown luqo33:www-data /var/www/app
VOLUME /var/www/app
WORKDIR /var/www/app

mkdir: cannot create directory '/var/www/app': No such file or directory sound so nonsensical - of course there is no such directory. I want to create it. What is wrong here?

This question is related to docker

The answer is


The problem is that /var/www doesn't exist either, and mkdir isn't recursive by default -- it expects the immediate parent directory to exist.

Use:

mkdir -p /var/www/app

...or install a package that creates a /var/www prior to reaching this point in your Dockerfile.


When creating subdirectories hanging off from a non-existing parent directory(s) you must pass the -p flag to mkdir ... Please update your Dockerfile with

RUN mkdir -p ... 

I tested this and it's correct.


You can also simply use

WORKDIR /var/www/app

It will automatically create the folders if they don't exist.

Then switch back to the directory you need to be in.


Apart from the previous use cases, you can also use Docker Compose to create directories in case you want to make new dummy folders on docker-compose up:

    volumes:
  - .:/ftp/
  - /ftp/node_modules
  - /ftp/files