Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mount a single file in a volume

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.

docker-compose:

version: '2' services:   app:     build: src     ports:       - "8080:80"     depends_on:       - mysql     volumes:       -  app-conf:/var/www/html/upload       -  app-conf:/var/www/html/config.php     environment:       DB_TYPE: mysql       DB_MANAGER: MysqlManager    mysql:     image: mysql:5.6     container_name: mysql     volumes:       - mysqldata:/var/lib/mysql     ports:       - 3306:3306     environment:       MYSQL_ROOT_PASSWORD:       MYSQL_DATABASE:       MYSQL_USER:       MYSQL_PASSWORD:  volumes:   mysqldata:   app-conf: 

Which results in the error:

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

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

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

But then it 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?

like image 877
Jakub Juszczak Avatar asked Feb 15 '17 11:02

Jakub Juszczak


People also ask

How do you add volume to a mount?

To mount a drive in an empty folder by using the Windows interface. In Disk Manager, right-click the partition or volume that has the folder in which you want to mount the drive. Click Change Drive Letter and Paths and then click Add. Click Mount in the following empty NTFS folder.

How do I run a specific file in Docker?

How to Use the docker run Command. The basic syntax for the command is: docker run [OPTIONS] IMAGE [COMMAND] [ARG...] You can run containers from locally stored Docker images.

Can data volumes can be mounted in read only mode?

Use a read-only volume Remember that multiple containers can mount the same volume, and it can be mounted read-write for some of them and read-only for others, at the same time.


2 Answers

TL;DR/Notice:

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) :

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

You can also do:

    volumes:       - ${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

like image 108
BlackStork Avatar answered Nov 25 '22 14:11

BlackStork


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)/config.py from Docker host to /root/app/config.py into the container as a file.

docker run -v $(pwd)/config.py:/root/app/config.py my_docker_image 

However, it always created a directory named config.py, 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)/config.py.

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

like image 35
SangminKim Avatar answered Nov 25 '22 13:11

SangminKim