•
2 min read

Docker Bind Mount Long Syntax

Table of Contents

Short Syntax

When declaring a volume bind mount the normal synatx usually goes like this:

services:
  api:
    image: node:lts
    volumes:
      - /opt/app-data:/data

This binds the /opt/app-data directory on the host to /data on in the container. We’ll call this the short syntax. There is also a longer syntax

Long Syntax

services:
  api:
    image: node:lts
    volumes:
    - type: bind
      source: /opt/app-data
      target: /data
      bind:
        create_host_path: false

I really like this longer syntax as it is a nice guardrail against mistakes. For example, let’s say you’re updating your Compose manifest and you misspell the source path as /opt/app-dtaa instead of /opt/app-data. With the short syntax, Compose will create that missing directory on the host and mount it into the container. Now your application is looking at an empty directory, and you’re left wondering where your data went. With create_host_path: false, that missing path causes an error instead, so you can catch the typo before the container starts. It won’t catch a wrong path that already exists, but it’s a small change that can save you some unnecessary troubleshooting.