How to lazy mount a named volume in docker compose?

I’m trying to create two instances: a MySQL server and a Samba server. I want to share files from the Samba server to the MySQL server. As I have seen from the docs, Docker provides named volumes which can work under the CIFS protocol. In Docker Compose, the volumes are created on top of the build, as I can understand. How can I initialize it lazily?

For example i have the given docker compose:

services:
  samba:
    container_name: samba
    env_file:
      - .env
    build:
      dockerfile: DockerFile.samba
      target: samba
      args:
        TOTAL_SAMBA_USERS: ${TOTAL_SAMBA_USERS}
        SAMBA_USER_1: ${SAMBA_USER_1}
        SAMBA_PASSWORD_1: ${SAMBA_PASSWORD_1}
        SAMBA_USER_2: ${SAMBA_USER_2}
        SAMBA_PASSWORD_2: ${SAMBA_PASSWORD_2}
        COMMON_NAME_CA: ${COMMON_NAME_CA}
        COMMON_NAME_SERVER: ${COMMON_NAME_SERVER}
        COMMON_NAME_CLIENT: ${COMMON_NAME_CLIENT}
        COUNTRY: ${COUNTRY}
        STATE: ${STATE}
        LOCALITY: ${LOCALITY}
        ORGANIZATION: ${ORGANIZATION}
        ORGANIZATIONAL_UNIT: ${ORGANIZATIONAL_UNIT}
        EMAIL_ADDRESS: ${EMAIL_ADDRESS}
        KEYSTORE_PASSWORD: ${KEYSTORE_PASSWORD}
    ports:
      - 445:445
      - 139:139
    volumes:
      - ./smb.conf:/etc/samba/smb.conf
    healthcheck:
      test: ["CMD", "smbclient", "//localhost/cert_share", "-U", "admin%1234", "-c", "ls"]
      interval: 2s
      timeout: 10s
      retries: 3


  mysql-db:
    container_name: mysql-db
    env_file:
      - .env
    build:
      dockerfile: DockerFile.mysql
      target: mysql
      args:
        MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
        MYSQL_USER: ${MYSQL_USER}
        MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    ports:
      - 3306:3306
    volumes:
      - data:/var/lib/cert
    depends_on:
      samba:
        condition: service_healthy


volumes:
  data:
    driver_opts:
      type: cifs
      o: username=admin,password=1234
      device: //localhost/cert_share/ 

I was expecting Docker to mount accordingly based on the dependencies specified with depends_on. However, as you can see below, I receive an error message from the daemon:

 ✔ Container samba            Created                                                                                                  
 ⠋ Container mysql-db         Creating                                                                                                 0.0s 
Error response from daemon: failed to populate volume: error while mounting volume '/var/lib/docker/volumes/ssl-sample_data/_data': failed to mount local volume: mount //127.0.0.1/cert_share/:/var/lib/docker/volumes/ssl-sample_data/_data, data: username=admin,password=********: connection refused

You have the samba container in the same project so you can just use a simple named volume mounted to both containers, so why do you need the cifs volume? That’s the point of using these volumes it doesn’t require any network filesystem. If the files are not on the Docker image, but generated when the container starts, a bind mount is enough too. I wrote about volumes in my blogpost if you want to learn more about it.

I need cifs volume because I generate some self-signed certificates in the Samba server as a multistage build which I want to share with more than one service.
For sure this can be done with a simple named volume, what could not be done is that I want to share this certification with the host for local development and for production with more than 10 testers in order to connect with clients like dbeaver,workbench etc…
As you can understand I use TLS setup and I’m trying to share certificates via samba share.

But it can be done. It is in the blogpost I linked. Just use a named volume with a custom source path and it will be available in the source folder even if it was generated inside the container.