5

I'm trying to run my image like this:

version: '3.8'
services:
  gamit:
    image: myimage
    volumes:
      - app:/app
volumes:
  app:
    driver: local
    driver_opts:
      type: none
      device: ./app
      o: bind

At first my goal was to have an app directory near my docker-compose.yml file that (app directory) should persist an app directory from inside a container (it already contains some important data). My deploy scenario assumes that this directory is initially empty on host and then keeps up with changes from inside container. When I was running it with docker compose up my app directory near the docker-compose.yml remained empty. I noticed that /var/lib/docker/volumes/myimage/_data was created and populated with data from container (I assumed from another stackoverflow questions that my device: ./app option sets the location of host directory). Then I've removed /var/lib/docker/volumes/myimage/ directory and now it throws me an error docker failed to mount local volume mount no such file or directory. My general goal is to initially run docker compose up with an empty app directory, populate it from inside the container and then have all changes from inside the container in it.

r69
  • 69
  • 1
  • 1
  • 4
  • You shouldn't usually change anything in `/var/lib/docker`, and deleting the volume directory there almost certainly caused the error you see now. Can you use the shorter `volumes: ['./app:/app']` bind-mount syntax instead? – David Maze Mar 30 '22 at 16:22
  • Is this off topic ? You may ask on serverfault.com –  Mar 30 '22 at 16:34
  • @DavidMaze Have tried to run with shorter `volumes: ['./app:/app']` bind-mount syntax. It has created an empty `app` directory near my `docker-compose.yml` file and `app` directory inside the container became empty. My goal is to have this directory with data from inside the container. – r69 Mar 30 '22 at 16:45
  • I finally found the answer here [enter link description here](https://stackoverflow.com/questions/70326574/named-volume-with-local-bind-defined-in-docker-compose-not-working-but-working-wx#new-answer) – huy88 Aug 21 '22 at 03:19

1 Answers1

6

In ubuntu 20.04, Docker version 20.10.17 and Docker compose version v2.6.0

I try to use: ${PWD}/app instead of ./app. like this:

version: '3.8'
services:
  gamit:
    image: myimage
    volumes:
      - app:/app
volumes:
  app:
    driver: local
    driver_opts:
      type: none
      device: ${PWD}/app
      o: bind

It works fine for me. As I know for the local volume In Docker, you need to have an absolute path for mounting. ${PWD} environment variable shows the current path of your project. But sometimes ${PWD} in some versions of Docker or some versions of Docker Compose do not work well!

There is suitable information at https://docs.docker.com/engine/install/linux-postinstall/

hossein emami
  • 176
  • 2
  • 9