Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 349
Spartan Troy Avatar asked Oct 11 '18 07:10

Spartan Troy


People also ask

What is mkdir in Dockerfile?

The command RUN mkdir -p /var/www/new_directory allows you to create a directory named new_directory inside the Docker file system that we will eventually build using an image built using the above Docker file.


1 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.

like image 79
Fabian Braun Avatar answered Sep 19 '22 03:09

Fabian Braun