Issue
I use a Dockerfile to create an apache2 ubuntu image. When it haves to create the directories it fails saying that some of these directories already exists, but in fact not at all. Dockerfile :
FROM ubuntu:latest
MAINTAINER Bernard Tapis
RUN apt-get -yqq update && apt-get install -yqq apache2
WORKDIR /var/www/html
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid
ENV APACHE_RUN_DIR /var/run/apache2
ENV APACHE_LOCK_DIR /var/lock/apache2
RUN mkdir p $APACHE_RUN_DIR $APACHE_LOCK_DIR $APACHE_LOG_DIR
ENTRYPOINT ["/usr/sbin/apache2"]
CMD ["-D", "FOREGROUND"]
EXPOSE 80
The error I get when executing docker image build -t quentinneves/apache .
Step 11/14 : RUN mkdir p $APACHE_RUN_DIR $APACHE_LOCK_DIR $APACHE_LOG_DIR
---> Running in 1d72a8fd64c0
mkdir: cannot create directory '/var/log/apache2': File exists
The command '/bin/sh -c mkdir p $APACHE_RUN_DIR $APACHE_LOCK_DIR $APACHE_LOG_DIR' returned a non-zero code: 1
But when I use ls -lsa /var/log/
:
root@VIR1-DOCKER:~/Docker/Apache# ls -lsa /var/log/
total 1576
4 drwxr-xr-x 5 root root 4096 Jun 1 13:42 .
4 drwxr-xr-x 11 root root 4096 Jun 1 13:35 ..
16 -rw-r--r-- 1 root root 13515 Jun 13 07:33 alternatives.log
4 drwxr-xr-x 2 root root 4096 Jun 13 07:33 apt
12 -rw-r----- 1 root adm 8773 Jun 6 15:17 auth.log
4 -rw------- 1 root utmp 768 Jun 6 07:06 btmp
76 -rw-r----- 1 root adm 70771 Jun 13 08:05 daemon.log
108 -rw-r----- 1 root adm 107936 Jun 6 07:05 debug
268 -rw-r--r-- 1 root root 269141 Jun 13 07:33 dpkg.log
8 -rw-r--r-- 1 root root 32032 Jun 6 07:05 faillog
4 drwxr-xr-x 3 root root 4096 Jun 1 13:39 installer
356 -rw-r----- 1 root adm 363005 Jun 13 08:05 kern.log
12 -rw-rw-r-- 1 root utmp 292292 Jun 6 15:09 lastlog
252 -rw-r----- 1 root adm 255033 Jun 13 08:05 messages
428 -rw-r----- 1 root adm 435499 Jun 13 08:05 syslog
4 drwxr-x--- 2 root adm 4096 Jun 6 07:01 unattended-upgrades
16 -rw-rw-r-- 1 root utmp 15744 Jun 6 15:10 wtmp
Surprise it's not there ! Before that it was /var/lock/apache2
that caused this issue, but it somehow passed and now /var/log/apache2
just won't be created.
If I delete this folder creation and create it manually, would it works for creating a new image for docker ? And do you know why it thinks that there's already a folder like that ?
Solution
Your problem is that you're creating directories without protecting against already-existence of them.
You've defined APACHE_LOG_DIR = APACHE_RUN_DIR, so, your RUN mkdir p $APACHE_RUN_DIR $APACHE_LOCK_DIR $APACHE_LOG_DIR
command try to create it twice and it fails.
When you check directory existence, probably you're accesing a container started from another image different than you've tried to build with this docker.
To protect that, add exit 0
at the end of RUN mkdir
, or change one ENV:
ENV APACHE_LOG_DIR /var/log/apache2
ENV APACHE_PID_FILE /var/run/apache2.pid
ENV APACHE_RUN_DIR /var/run/apache2 <--- the same than APACHE_LOG_DIR
ENV APACHE_LOCK_DIR /var/lock/apache2
RUN mkdir -p $APACHE_RUN_DIR $APACHE_LOCK_DIR $APACHE_LOG_DIR; exit 0
Answered By - Alejandro Galera Answer Checked By - Cary Denson (WPSolving Admin)