Programming

볼륨에 단일 파일을 마운트하는 방법

procodes 2020. 5. 29. 23:18
반응형

볼륨에 단일 파일을 마운트하는 방법


PHP 응용 프로그램을 고정하려고합니다. dockerfile에서 아카이브를 다운로드하고 추출하십시오.

새 버전이 출시되고 config.php가 덮어 쓰이기 때문에 응용 프로그램을 다시 설치 해야하는 dockerfile을 업데이트하면 모든 것이 잘 작동합니다.

그래서 데이터베이스와 마찬가지로 파일을 볼륨으로 마운트 할 수 있다고 생각했습니다.

볼륨과 직접 경로를 사용하여 두 가지 방법으로 시도했습니다.

도커 작성 :

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:

오류가 발생하는 원인은 다음과 같습니다.

그리고 주어진 경로로 마운트 된 볼륨으로 시도했습니다.

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

그러나 두 가지 방법 모두 작동하지 않습니다. 마운트 된 볼륨으로 업로드가 생성되는 것을 볼 수 있습니다.

그러나 실패

/var/www/html/config.php \\ "는 \\"디렉토리가 아닙니다 \\ "\" "

/src/docker/myapp/upload/config.php로 시도하면 : /var/www/html/config.php

Docker는 업로드 폴더와 config.php 폴더를 만듭니다. 파일이 아닙니다.

또는 구성을 유지하는 다른 방법이 있습니까?


TL; DR / 공지 사항 :

마운트하려는 파일 대신 디렉토리가 작성되는 경우 유효 하고 절대 경로 를 제공하지 않았을 수 있습니다 . 이는 조용하고 혼란스러운 실패 모드에서 흔히 발생하는 실수입니다.

파일 볼륨은 docker에서 이런 방식으로 수행됩니다 (절대 경로 예제 (env 변수를 사용할 수 있음). 파일 이름을 언급해야합니다).

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

당신은 또한 할 수 있습니다 :

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

/src/docker/myapp폴더 에서 docker-compose를 실행하면


나는 비슷한 문제로 고통 받고 있었다. 이미지를 다시 작성하지 않고 필요할 때마다 수정할 수 있도록 구성 파일을 컨테이너로 가져 오려고했습니다.

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.


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


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

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

Use mount (--mount) instead volume (-v)

More info: https://docs.docker.com/storage/bind-mounts/

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

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


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.


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: https://docs.docker.com/compose/reference/envvars/ 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.


Maybe this helps someone.

I had this problem and tried everything. Volume bindings looked well and even if I mounted directory (not files), I had the file names in the mounted directory correctly but mounted as dirs.

I tried to re-enable shared drives and Docker complained the firewall is active.

After disabling the firewall all was working fine.


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.

참고URL : https://stackoverflow.com/questions/42248198/how-to-mount-a-single-file-in-a-volume

반응형