-2

My docker-compose.yml

version: "3.1"
services:
    www:
        build:
            context: .
            dockerfile: Dockerfile.lamp
        ports: 
            - "${WEBSERVER_PORT}:80"
        volumes:
            - ./www:/var/www/html/
        links:
            - db
        networks:
            - default
    db:
        image: mysql:latest
        ports: 
            - "3306:3306"
        command: --default-authentication-plugin=mysql_native_password
        environment:
            MYSQL_DATABASE: ${MYSQL_DATABASE}
            MYSQL_USER: ${MYSQL_USER}
            MYSQL_PASSWORD: ${MYSQL_PASSWORD}
            MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD} 
        volumes:
            - ./sql:/docker-entrypoint-initdb.d
            - ./conf:/etc/mysql/conf.d
            - mysql_vol:/var/lib/mysql
        networks:
            - default
    phpmyadmin:
        image: phpmyadmin/phpmyadmin
        links: 
            - db:db
        ports:
            - ${PHPMYADMIN_PORT}:80
        environment:
            MYSQL_USER: ${MYSQL_USER}
            MYSQL_PASSWORD: ${MYSQL_PASSWORD}
            MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD}
            UPLOAD_LIMIT: 64M 
volumes:
    mysql_vol:
        external: false

dockerfile:

FROM php:8.1-apache
RUN a2enmod rewrite 
RUN docker-php-ext-install pdo pdo_mysql    

# Install system dependencies
RUN apt-get update && apt-get install -y \
      git \
      libicu-dev \
      zlib1g-dev \
      g++\
      libpq-dev \
      libmcrypt-dev \
      git \
      zip \
      unzip

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install mysqli
RUN docker-php-ext-configure intl \
    && docker-php-ext-install intl

However, after running "docker compose up -d", the folder "mysql_vol" stays empty and the result of "docker volume inspect test_project_mysql_vol" yields:

[
    {
        "CreatedAt": "2023-02-15T16:07:08+01:00",
        "Driver": "local",
        "Labels": {
            "com.docker.compose.project": "test_project",
            "com.docker.compose.version": "2.15.1",
            "com.docker.compose.volume": "mysql_vol"
        },
        "Mountpoint": "/var/lib/docker/volumes/test_project_mysql_vol/_data",
        "Name": "test_project_mysql_vol",
        "Options": null,
        "Scope": "local"
    }
]

How could it be that, while setting external: false, the mountpoint still is not in the folder as the docker-compose.yml file, but still in /var/lib/docker/volumes?

Mart
  • 475
  • 4
  • 21

1 Answers1

1

If you are trying to refer to a local directory, you need to write ./mysql_vol:

db:
    image: mysql:latest
    ports: 
        - "3306:3306"
    command: --default-authentication-plugin=mysql_native_password
    environment:
        MYSQL_DATABASE: ${MYSQL_DATABASE}
        MYSQL_USER: ${MYSQL_USER}
        MYSQL_PASSWORD: ${MYSQL_PASSWORD}
        MYSQL_ROOT_PASSWORD: ${MYSQL_PASSWORD} 
    volumes:
        - ./sql:/docker-entrypoint-initdb.d
        - ./conf:/etc/mysql/conf.d
        - ./mysql_vol:/var/lib/mysql
    networks:
        - default

If you write mysql_vol without the leading ./, you are referring to a named Docker volume which will be allocated automatically by Docker from its own storage.

The external keyword is used when you want to refer to an existing named volume that is not managed by docker compose. Setting external: false is a no-op because that's the default.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • Thanks! What I want to achieve is having the volume in my host folder, and when using ./mysql_vol rather than a volume, I cannot login. All files are generated in mysql_vol but Icannot authenticate into phpmyadmin. – Mart Feb 15 '23 at 15:45
  • That seems like a different issue, and might be worth a separate question. – larsks Feb 15 '23 at 15:46