skip to Main Content

I have the following docker-compose

version: "3.9"
services:
   foo:
      image: an_image
      volumes:
         - source: ./logs
           target: /tmp/data
           type: bind

I am using 3.9 docker-compose syntax with 2.24 docker-compose library. Because of this bug I have to use the long syntax for mounting volumes.

My goal is the following: The logs directory does not exist in the host system. Nevertheless, I want to create this directory and mount it on the docker container. With the old binary version this was working (I was using the compact syntax ./logs:/tmp/data) but not anymore and not with the long syntax.

The error I am getting is

Error response from daemon: invalid mount config for type "bind": bind source path does not exist: /host_mnt/Users/user/Workspace/an-app/e2e/logs

I am running docker-compose from the an-app directory

docker-compose -f ./e2e/docker-compose.e2e.yml up

2

Answers


  1. In Docker Compose, when using the long syntax for bind mounts, the source path must exist on the host system; Docker will not create it for you. This behavior is different from the short syntax, where Docker can create the directory if it does not exist.

    To work around this, you can ensure that the directory exists on the host before starting your services with Docker Compose.

    Login or Signup to reply.
  2. For that, you can use create_host_path, like so:

    version: "3.9"
    services:
       foo:
          image: an_image
          volumes:
             - source: ./logs
               target: /tmp/data
               type: bind
               bind:
                   create_host_path: true
    

    Reference: Long syntax:

    create_host_path: Creates a directory at the source path on host if there is nothing present. Compose does nothing if there is something present at the path. This is automatically implied by short syntax for backward compatibility with docker-compose legacy.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search