70

I cannot create a directory with the mkdir command in a container with dockerfile.

My Dockerfile file is simply ;

FROM php:fpm

WORKDIR /var/www/html

VOLUME ./code:/var/www/html

RUN mkdir -p /var/www/html/foo

In this way I created a simple php: fpm container. and I wrote to create a directory called foo.

docker build -t phpx .

I have built with the above code.

In my docker-compose file as follows.

version: '3'

services:
web:
container_name: phpx
build : .
ports:
- "80:80"
volumes:
- ./code:/var/www/html

later; run the following code and I entered the container kernel.

docker exec -it phpx /bin/bash

but there is no a directory called foo in / var / www / html.

I wonder where I'm doing wrong. Can you help me?

Spartan Troy
  • 949
  • 2
  • 9
  • 13

4 Answers4

84

The reason is that you are mounting a volume from your host to /var/www/html. Step by step:

  1. RUN mkdir -p /var/www/html/foo creates the foo directory inside the filesystem of your container.
  2. docker-compose.yml ./code:/var/www/html "hides" the content of /var/www/html in the container filesystem behind the contents of ./code on the host filesystem.

So actually, when you exec into your container you see the contents of the ./code directory on the host when you look at /var/www/html.

Fix: Either you remove the volume from your docker-compose.yml or you create the foo-directory on the host before starting the container.

Additional Remark: In your Dockerfile you declare a volume as VOLUME ./code:/var/www/html. This does not work and you should probably remove it. In a Dockerfile you cannot specify a path on your host.

Quoting from docker:

The host directory is declared at container run-time: The host directory (the mountpoint) is, by its nature, host-dependent. This is to preserve image portability. since a given host directory can’t be guaranteed to be available on all hosts. For this reason, you can’t mount a host directory from within the Dockerfile. The VOLUME instruction does not support specifying a host-dir parameter. You must specify the mountpoint when you create or run the container.

Fabian Braun
  • 3,612
  • 1
  • 27
  • 44
10

I am able to create a directory inside the 'workdir' for docker as follows:

Dockerfile content

COPY src/ /app
COPY logging.conf /app
COPY start.sh   /app/
COPY Pipfile /app/
COPY Pipfile.lock /app/
COPY .env /app/
RUN mkdir -p /app/logs
COPY logs/some_log.log /app/logs/

WORKDIR /app

I have not mentioned the volume parameter in my 'docker-compose.yaml' file

So here is what I suggest: Remove the volume parameter from the 'Dockerfile' as correctly pointed by the Fabian Braun.

FROM php:fpm
RUN mkdir -p /var/www/html/foo
WORKDIR /var/www/html

And remove the volume parameter from the docker-compose file. It will work. Additionally, I would like to know how you tested of there is a directory named 'foo'. Docker-compose file content

version: '3'
services:
  web:
  build:
    context: .
    dockerfile: Dockerfile # The name of your docker file
  container_name: phpx
  ports:
    - "80:80"
1

I've built this on M1 macbook. This one works for me.

docker build -t emr6.10_custom .
FROM 755674844232.dkr.ecr.us-east-1.amazonaws.com/spark/emr-6.10.0:latest 

USER root

RUN yum update -y && \
    yum install -y java-11-amazon-corretto && \
    yum clean all;
ENV JAVA_HOME /usr/lib/jvm/java-11-amazon-corretto.aarch64
RUN update-alternatives --set java $JAVA_HOME/bin/java
USER hadoop:hadoop

After building the image, we should check the java directory path. Because it is varies from your system.

docker run --rm -ti emr6.10_custom:latest /bin/bash

And then, you can fix docker properly.

Jaden Park
  • 11
  • 1
-1

You can use the SHELL instruction of Dockerfile.

ENV HOME /usr/local

SHELL ["/bin/sh", "-c"]
RUN mkdir  $HOME/logs
Panda1667075
  • 147
  • 1
  • 10