Operation of the Mkdir Command with Dockerfile

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?

2 Answers

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.

1

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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest