<p>Define the volume in the following way </p>

<pre class="lang-yaml prettyprint-override"><code>volumes:
  - ${local_path}:/data
</code></pre>

<p>or using docker volumes </p>

<pre class="lang-yaml prettyprint-override"><code>version: '3.7'

volumes:
  db-data:

services:
  one:
    image: alpine
    command: sh -c 'echo one &gt;&gt; /data/db.txt &amp;&amp; cat /data/db.txt | wc -l'
    volumes:
      - db-data:/data
</code></pre>

CC BY-SA 4.0
2020-03-06T07:26:57.613Z
2020-03-06T07:26:57.613Z
Al-waleed Shihadeh
<p>I have a bit of hard time understanding the way <code>docker-compose</code> handles auto-managed volumes. Here is an example <code>docker-compose.yml</code></p>

<pre class="lang-yml prettyprint-override"><code>version: '3.7'
services:
  one:
    image: alpine
    command: sh -c 'echo one &gt;&gt; /data/db.txt &amp;&amp; cat /data/db.txt | wc -l'
    volumes:
      - /data
</code></pre>

<p>And here is what I get (<code>docker-compose</code> logs stripped out)</p>

<pre class="lang-bash prettyprint-override"><code>$ docker-compose up
1 # as expected

$ docker-compose start
$ docker-compose up
3 # as expected

$ docker-compose run one
1 # why ? I expected 4
</code></pre>

<p>So it seems the volume is not shared between containers of the same service (in this example, the service <code>one</code>). Actually a new volume seems to be created every time <code>run</code> is used</p>

<pre class="lang-bash prettyprint-override"><code>test $(docker volume ls | wc -l) -eq 0 # ensure clean env

repeat 10 docker-compose up
test $(docker volume ls | wc -l) -eq 1 # works

docker-compose down &amp;&amp; docker volume prune -f # cleanup

repeat 10 docker-compose run one
test $(docker volume ls | wc -l) -eq 1 # fails, result is 10 instead
</code></pre>

<p><a href="https://docs.docker.com/compose/compose-file/#short-syntax-3" rel="nofollow noreferrer">The <code>docker-compose</code> documentation</a> does not really help, the only explanation seems to be</p>

<blockquote>
  <p>Just specify a path and let the Engine create a volume</p>
  
  <pre class="lang-yml prettyprint-override"><code>volumes:
  - /var/lib/mysql
</code></pre>
</blockquote>

<p>I do not encounter this issue when using named volumes (<em>e.g.</em> <code>data:/data</code>). So <strong>why is a new volume created for new containers of the same service ?</strong> And what is the point of such behavior to be the default ?</p>

<p><em>N.B. I know I should use explicitly defined volumes instead, but I want to understand the default behavior, and why it is this way</em></p>

<blockquote class="spoiler">
  <p> <code>docker</code> and <code>docker-compose</code> version info <a href="https://pastebin.com/8TuFCg2i" rel="nofollow noreferrer">https://pastebin.com/8TuFCg2i</a></p>
</blockquote>

<docker><docker-compose><containers><volume>
Auto-managed docker-compose volume not shared among containers
michaeldel
# Auto-managed docker-compose volume not shared among containers

(Asked by: username_0 on 2020-02-15)

I have a bit of hard time understanding the way docker-compose handles auto-managed volumes. Here is an example docker-compose.yml
version: '3.7'
services:
  one:
    image: alpine
    command: sh -c 'echo one >> /data/db.txt && cat /data/db.txt | wc -l'
    volumes:
      - /data

And here is what I get (docker-compose logs stripped out)
$ docker-compose up
1 # as expected

$ docker-compose start
$ docker-compose up
3 # as expected

$ docker-compose run one
1 # why ? I expected 4

So it seems the volume is not shared between containers of the same service (in this example, the service one). Actually a new volume seems to be created every time run is used
test $(docker volume ls | wc -l) -eq 0 # ensure clean env

repeat 10 docker-compose up
test $(docker volume ls | wc -l) -eq 1 # works

docker-compose down && docker volume prune -f # cleanup

repeat 10 docker-compose run one
test $(docker volume ls | wc -l) -eq 1 # fails, result is 10 instead

The docker-compose documentation does not really help, the only explanation seems to be

Just specify a path and let the Engine create a volume
volumes:
  - /var/lib/mysql


I do not encounter this issue when using named volumes (e.g. data:/data). So why is a new volume created for new containers of the same service ? And what is the point of such behavior to be the default ?
N.B. I know I should use explicitly defined volumes instead, but I want to understand the default behavior, and why it is this way

 docker and docker-compose version info https://pastebin.com/8TuFCg2i

**Tags:** docker, docker-compose, containers, volume


---

## 1 answers

### Answer by username_1 on 2020-03-06. Score: 1

Define the volume in the following way 
volumes:
  - ${local_path}:/data

or using docker volumes 
version: '3.7'

volumes:
  db-data:

services:
  one:
    image: alpine
    command: sh -c 'echo one >> /data/db.txt && cat /data/db.txt | wc -l'
    volumes:
      - db-data:/data


---

