By Jakub Juszczak

2017-02-15 11:33:27 8 Comments

I am trying to dockerize a php application. In the dockerfile I download the archive, extract it etc.

Everything works fine, however if a new version gets released, and I update the dockerfile I have to reinstall the application, because the config.php gets overwritten.

So I thought I can mount the file as a volume, like I do with the database.

I tried it two ways, with a volume and a direct path.


version: '2'
    build: src
      - "8080:80"
      - mysql
      -  app-conf:/var/www/html/upload
      -  app-conf:/var/www/html/config.php
      DB_TYPE: mysql
      DB_MANAGER: MysqlManager

    image: mysql:5.6
    container_name: mysql
      - mysqldata:/var/lib/mysql
      - 3306:3306


Which results in the error:

And I tried it with a given path, as a mounted volume.


However both ways are not working. With the mounted volume I see that upload gets created.

But then fails with

/var/www/html/config.php\\" caused \\"not a directory\\"\""

If I try it with /src/docker/myapp/upload/config.php:/var/www/html/config.php

Docker creates the upload folder and then a config.php folder. Not a file.

Or is there another way to persist the config?


@Daniel 2019-06-13 21:27:35

You can also use a relative path to your docker-compose.yml file like this (tested on Windows host, Linux container):

      - ./test.conf:/fluentd/etc/test.conf

@BlackStork 2017-02-15 22:06:14


If you experience a directory being created in place of the file you are trying to mount, you have probably failed to supply a valid and absolute path. This is a common mistake with a silent and confusing failure mode.

File volumes are done this way in docker (absolute path example (can use env variables), and you need to mention the file name) :

      - /src/docker/myapp/upload:/var/www/html/upload
      - /src/docker/myapp/upload/config.php:/var/www/html/config.php

You can also do:

      - ${PWD}/upload:/var/www/html/upload
      - ${PWD}/upload/config.php:/var/www/html/config.php

If you fire the docker-compose from /src/docker/myapp folder

@Jakub Juszczak 2017-02-16 10:05:49

Like I said, if I try /src/docker/myapp/upload/config.php:/var/www/html/config.php Docker creates the upload folder and then a config.php folder. Not a file.

@BlackStork 2017-02-16 17:27:43

In your post you wrote differently. If the file is there it should mount as file.

@BlackStork 2017-02-16 17:36:04

@Steve Smith 2017-08-07 16:52:16

In order for this to work I had to supply an absolute path. The tip using PWD helped a lot here, thanks!

@eKelvin 2017-10-12 08:42:24

For me is creating a folder rather than the files, but I think because that file is copied later in that path is there anyway to postpone the binding?

@cancerbero 2017-11-09 22:14:13

if /src/docker/myapp/upload/config.php doesn't exists in the host docker will create a folder... What i'm doing is creating a script that creates an empty file before running docker run - would be ideal if I can instruct docker to do it for me via cmd line args.

@dexterousashish 2019-03-01 10:23:51

Is there a way to add any other environment variable instead of $PWD, I am trying to mount a file with some different path other than current directory but it isn't working out for me .

@BlackStork 2019-03-03 10:38:54

@dexterousashish As long as docker-compose have your custom variable with absolute path you can use it in same manner, i.e. ${CUSTOM_PATH_VAR}/file.txt. Alternatively you can supply .env file in folder from where docker-compose started, docker-compose will use env vars from it automatically

@Tara Prasad Gurung 2019-04-07 06:01:02

Though it worked when the container already exist but in a fresh new instance when doing so I am getting the ERROR: Duplicate mount points: issue

@Justin Lessard 2019-02-21 20:43:02

For anyone using Windows container like me, know that you CANNOT bind or mount single files using windows container.

The following examples will fail when using Windows-based containers, as the destination of a volume or bind mount inside the container must be one of: a non-existing or empty directory; or a drive other than C:. Further, the source of a bind mount must be a local directory, not a file.

net use z: \\remotemachine\share

docker run -v z:\foo:c:\dest ...

docker run -v \\uncpath\to\directory:c:\dest ...

docker run -v c:\foo\somefile.txt:c:\dest ...

docker run -v c:\foo:c: ...

docker run -v c:\foo:c:\existing-directory-with-contents ...

It's hard to spot but it's there

Link to the Github issue regarding mapping files into Windows container

@CallMarl 2019-01-28 16:36:47

In windows, if you need the a ${PWD} env variable in your docker-compose.yml you can creat a .env file in the same directory as your docker-compose.yml file then insert manualy the location of your folder.

CMD (pwd_var.bat) :

echo PWD=%cd% >> .env

Powershell (pwd_var.ps1) :

$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'; echo "PWD=$(get-location).path" >> .env

There is more good features hear for docker-compose .env variables: especially for the COMPOSE_CONVERT_WINDOWS_PATHS env variable that allow docker compose to accept windows path with baskslash "\".

When you want to share a file on windows, the file must exist before sharing it with the container.

@Subbu M 2019-01-17 19:35:08

Use mount instead volume (-v) More info:

Example: Ensure /tmp/a.txt exists on docker host docker run -it --mount type=bind,source=/tmp/a.txt,target=/root/a.txt alpine sh

@Pool 2018-10-24 14:43:52

I had the same issue on Windows, Docker 18.06.1-ce-win73 (19507).

Removing and re-adding the shared drive via the Docker settings panel and everything worked again.

@Alex Velickiy 2018-10-13 13:56:44

I have same issue on my Windows 8.1

It turned out that it was due to case-sensitivity of path. I called docker-compose up from directory cd /c/users/alex/ and inside container a file was turned into directory.

But when I did cd /c/Users/alex/ (not Users capitalized) and called docker-compose up from there, it worked.

In my system both Users dir and Alex dir are capitalized, though it seems like only Users dir matter.

@jla 2018-07-27 13:50:07

For me, the issue was that I had a broken symbolic link on the file I was trying to mount into the container

@SangminKim 2017-11-03 15:19:01

I had been suffering from a similar issue. I was trying to import my config file to my container so that I can fix it every time I need without re-building the image.

I mean I thought the below command would map $(pwd)/ from Docker host to /root/app/ into the container as a file.

docker run -v $(pwd)/ my_docker_image

However, it always created a directory named, not a file.

while looking for clue, I found the reason(from here)

If you use -v or --volume to bind-mount a file or directory that does not yet exist on the Docker host, -v will create the endpoint for you. It is always created as a directory.

Therefore, it is always created as a directory because my docker host does not have $(pwd)/

Even if I create in docker host. $(pwd)/ just overwirte /root/app/ not exporting /root/app/

@shadi 2018-01-31 07:03:53

I just added a touch path/to/file in the Dockerfileso that when I mount it would still be a file (not a created dir)

@sdkks 2018-06-14 09:09:25

left side of : has to be full path. It works this way

Related Questions

Sponsored Content

5 Answered Questions

1 Answered Questions

[SOLVED] Docker-Compose mounting volume files on Mac but not PC

2 Answered Questions

[SOLVED] Can I have a writable Docker volume mounted under a read-only volume?

19 Answered Questions

[SOLVED] How is Docker different from a virtual machine?

0 Answered Questions

Unsupported config option for services service: 'elasticsearch'

1 Answered Questions

[SOLVED] Can we mount sub-directories of a named volume in docker?

1 Answered Questions

[SOLVED] Cannot mount volume with docker-compose

1 Answered Questions

[SOLVED] Docker link container as build argument

1 Answered Questions

Sponsored Content