Search code examples
dockerdocker-composepermissions

Docker can't access mounted volume when build, but can when run


So this is a bit weird. I try this docker compose:

version: "3.8"
services:
  sharif-judge:
    image: liuyang1204/docker-sharif-judge
    ports:
      - 80:80
    depends_on:
      - db
    environment:
      - DB_HOST=db
      - DB_PORT=5432
      - DB_NAME=postgres
      - DB_USER=postgres
      - DB_PASSWORD=postgres
    volumes:
      - ./site:/var/www/html/
      - ./tester-data:/data/tester/
      - ./assignments-data:/data/assignments/
    networks:
      - sharif-judge-net
  db:
    image: postgres:13-alpine
    volumes:
      - ./db-data:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - 5432
    networks:
      - sharif-judge-net
# volumes:
#   db-data:/online-judge/db-data
#   tester-data:/online-judge/tester-data
#   assignments-data:./assignments-data
#   site:./site
networks:
  sharif-judge-net:

At first I want to mount /var/www/html to my local. And I suppose that I can do it this way. I try at first and found nothing in site. The docker image suppose to create a codeigniter project there but I guess it can't access the path. Weirdly, I don't see any permission error in terminal. It just not there. I give 755 with sudo chmod -R 755 site also sudo chmod -R 755 . so docker can access it but it's not working, same issue.

So I try to use docker exec to see what happen in the container. I try to echo a file to it echo "Hello, world" > output.txt. And it works just fine, and a file shows in destination and in my local mounted path. So it's not permission error.

Can anyone explain why this is happen? Or how to solve so docker can just build file to it?

Edit: Just like David in the comment explained, I guess I got it wrong about mounting. I try to use bind instead but it still the same:

version: "3.8"
services:
  sharif-judge:
    image: liuyang1204/docker-sharif-judge
    ports:
      - 80:80
    depends_on:
      - db
    environment:
      - DB_HOST=db
      - DB_PORT=5432
      - DB_NAME=postgres
      - DB_USER=postgres
      - DB_PASSWORD=postgres
    volumes:
      - type: bind
        source: ./site
        target: /var/www/html/
      - type: bind
        source: ./tester-data
        target: /data/tester/
      - type: bind
        source: ./assignments-data
        target: /data/assignments/
    networks:
      - sharif-judge-net
  db:
    image: postgres:13-alpine
    volumes:
      - type: bind
        source: ./db-data
        target: /var/lib/postgresql/data
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - 5432
    networks:
      - sharif-judge-net
networks:
  sharif-judge-net:

I also chown in the root using

sudo chown -R $(whoami):$(whoami) .

Incase ownership is the problem


Solution

  • well this post answer mine: Mount docker host volume but overwrite with container's contents

    Seems like I misunderstood the concept of docker volumes, I thought that it mount it first and then the container use it to write data. Some how I saw in bitnami mariadb docker can do that. I tried that before and the docker compose create a new folder and then write data in it