Cannot execute RUN mkdir in a Dockerfile
DockerDocker Problem Overview
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?
Docker Solutions
Solution 1 - Docker
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.
Solution 2 - Docker
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.
Solution 3 - Docker
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.
Solution 4 - Docker
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
Solution 5 - Docker
Also if you want to use multiple directories and then their change owner, you can use:
RUN set -ex && bash -c 'mkdir -p /var/www/{app1,app2}' && bash -c 'chown luqo33:www-data /var/www/{app1,app2}'
Just this!