Answers: <p>If you let docker-compose manage the volumes for you, the volumes names in <code>docker-compose.yml</code> will not be the final ones created/used in docker. <code>docker-compose</code> will prepend the volumes names with the compose project name, which is by default the name of the folder holding the compose file. This is to allow separate projects to use the same names without conflict. For information, you will get the same naming convention for networks.</p>
  <p>To get your data back correctly in your compose project, you have two choices.</p>
  <h1>Prefered in your case: Move data to automated volumes.</h1>
  <p>This will preserve the default compose volume management in your future runs.</p>
  <ul>
  <li>stop any running container using any of those volumes</li>
  <li>launch a dummy container not having mysql (e.g. busybox) and mount all 4 volumes in separate identifiable paths</li>
  <li>empty all volumes created by <code>docker-compose</code></li>
  <li>copy all data from &quot;docker only&quot; volumes to &quot;compose&quot; volumes.</li>
  <li>stop and recycle your dummy container.</li>
  <li><code>docker-compose up -d</code> and enjoy.</li>
  <li>if all went good and smooth, delete the other unneeded volumes.</li>
  </ul>
  <h1>If you know what your doing: use external volumes</h1>
  <p>This will work as well but please check the <a href="https://docs.docker.com/compose/compose-file/07-volumes/#external" rel="nofollow noreferrer">documentation</a> for details. Specifically, be aware that <code>docker-compose up -d</code> will no longer try to create the unexisting volumes and fire an error and stop if they don't exist.</p>
  <p>Simply change you volume section at the end of your file to:</p>
  <pre class="lang-yaml prettyprint-override"><code>volumes:
    db-data-user:
      external:
        name: db-data-user
    db-data-system:
      external:
        name: db-data-system
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-07-10T14:45:35.380Z
Answers: Thanks! This is exactly what I needed.
Answers: David Thompson
Answers: CC BY-SA 4.0
Answers: 2019-07-10T13:46:03.153Z
Answers: 2024-08-06T17:45:38.827Z
Answers: 2024-08-06T17:45:38.827Z
Answers: Zeitounator
Body: <p>I'm trying to run a SQL Server Database in a container and have been unable to mount a volume using docker-compose. I am able to persist data using <code>docker run</code>, however. </p>
  
  <p><strong>SETUP</strong></p>
  
  <p><code>docker volume ls</code></p>
  
  <pre><code>DRIVER              VOLUME NAME
  local               db-data-system
  local               db-data-user local              
  docker-containers_db-data-system local              
  docker-containers_db-data-user
  </code></pre>
  
  <p><strong>RUNNING DOCKER FROM COMMAND LINE</strong></p>
  
  <p>I'm able to run a container using <code>docker run</code> that persists data (see testdatabase and arbq_app).</p>
  
  <pre><code>sudo docker run -e 'ACCEPT_EULA=Y' -e 'P@ssw0rd' \
    --name 'arbq-db' -e 'MSSQL_PID=Developer' -p 1401:1433 \
    -v db-data-system:/var/opt/mssql -v db-data-user:/var/opt/sqlserver \
    -d mcr.microsoft.com/mssql/server:2017-latest
  </code></pre>
  
  <p>In dBeaver: <code>SELECT name FROM sys.databases</code></p>
  
  <pre class="lang-sql prettyprint-override"><code>master
  tempdb
  model
  msdb
  testdatabase
  arbq_app
  </code></pre>
  
  <p><strong>WITH DOCKER COMPOSE</strong></p>
  
  <p><strong>docker-compose.yml:</strong></p>
  
  <pre><code>version: '3.6'
  
  services:
  
    # Another Container
    another-container:
      content: some-content 
  
    # SQL Server
    arbq-db:
      container_name: arbq-db
      image: mcr.microsoft.com/mssql/server:2017-latest
      ports:
       - "1401:1433"
      environment:
        ACCEPT_EULA: Y
        SA_PASSWORD: P@ssw0rd
        MSSQL_PID: Developer
      volumes:
        - db-data-user:/var/opt/sqlserver
        - db-data-system:/var/opt/mssql
  
  volumes:
    db-data-user:
    db-data-system:
  </code></pre>
  
  <p>I run <code>docker-compose up --build</code></p>
  
  <p>In dBeaver: <code>SELECT name FROM sys.databases</code></p>
  
  <pre class="lang-sql prettyprint-override"><code>master
  tempdb
  model
  msdb
  </code></pre>
  
  <p><code>docker inspect -f '{{ .Mounts }}' e02bd89ccc38</code></p>
  
  <pre><code>[{volume docker-containers_db-data-user /var/lib/docker/volumes/docker-containers_db-data-user/_data /var/opt/sqlserver local rw true } {volume docker-containers_db-data-system /var/lib/docker/volumes/docker-containers_db-data-system/_data /var/opt/mssql local rw true }]
  </code></pre>
  
  <p>I honestly don't know what docker-containers_db-data-user and docker-containers_db-data-system are, but they don't contain the db data I need persisted!</p>
  
  <p>What can I do to ensure that the testdatabase and arbq_app databases persist in the container when using docker-compose?</p>
  
  <p>Thanks in advance.</p>
  
Tags: <docker><docker-compose>
Title: Volumes Not Mounting with Docker-Compose
UserDisplayName: David Thompson
ThreadText: # Volumes Not Mounting with Docker-Compose
  
  (Asked by: username_0 on 2019-07-09)
  
  I'm trying to run a SQL Server Database in a container and have been unable to mount a volume using docker-compose. I am able to persist data using `docker run`, however.
  
  **SETUP**
  
  `docker volume ls`
  
  ```
  DRIVER              VOLUME NAME
  local               db-data-system
  local               db-data-user local              
  docker-containers_db-data-system local              
  docker-containers_db-data-user
  
  ```
  
  **RUNNING DOCKER FROM COMMAND LINE**
  
  I'm able to run a container using `docker run` that persists data (see testdatabase and arbq\_app).
  
  ```
  sudo docker run -e 'ACCEPT_EULA=Y' -e 'P@ssw0rd' \
    --name 'arbq-db' -e 'MSSQL_PID=Developer' -p 1401:1433 \
    -v db-data-system:/var/opt/mssql -v db-data-user:/var/opt/sqlserver \
    -d mcr.microsoft.com/mssql/server:2017-latest
  
  ```
  
  In dBeaver: `SELECT name FROM sys.databases`
  
  ```sql
  master
  tempdb
  model
  msdb
  testdatabase
  arbq_app
  
  ```
  
  **WITH DOCKER COMPOSE**
  
  **docker-compose.yml:**
  
  ```
  version: '3.6'
  
  services:
  
    # Another Container
    another-container:
      content: some-content 
  
    # SQL Server
    arbq-db:
      container_name: arbq-db
      image: mcr.microsoft.com/mssql/server:2017-latest
      ports:
       - "1401:1433"
      environment:
        ACCEPT_EULA: Y
        SA_PASSWORD: P@ssw0rd
        MSSQL_PID: Developer
      volumes:
        - db-data-user:/var/opt/sqlserver
        - db-data-system:/var/opt/mssql
  
  volumes:
    db-data-user:
    db-data-system:
  
  ```
  
  I run `docker-compose up --build`
  
  In dBeaver: `SELECT name FROM sys.databases`
  
  ```sql
  master
  tempdb
  model
  msdb
  
  ```
  
  `docker inspect -f '{{ .Mounts }}' e02bd89ccc38`
  
  ```
  [{volume docker-containers_db-data-user /var/lib/docker/volumes/docker-containers_db-data-user/_data /var/opt/sqlserver local rw true } {volume docker-containers_db-data-system /var/lib/docker/volumes/docker-containers_db-data-system/_data /var/opt/mssql local rw true }]
  
  ```
  
  I honestly don't know what docker-containers\_db-data-user and docker-containers\_db-data-system are, but they don't contain the db data I need persisted!
  
  What can I do to ensure that the testdatabase and arbq\_app databases persist in the container when using docker-compose?
  
  Thanks in advance.
  
  **Tags:** docker, docker-compose
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-07-10. Score: 3 | Accepted answer
  
  If you let docker-compose manage the volumes for you, the volumes names in `docker-compose.yml` will not be the final ones created/used in docker. `docker-compose` will prepend the volumes names with the compose project name, which is by default the name of the folder holding the compose file. This is to allow separate projects to use the same names without conflict. For information, you will get the same naming convention for networks.
  
  To get your data back correctly in your compose project, you have two choices.
  
  # Prefered in your case: Move data to automated volumes.
  
  This will preserve the default compose volume management in your future runs.
  
  * stop any running container using any of those volumes
  * launch a dummy container not having mysql (e.g. busybox) and mount all 4 volumes in separate identifiable paths
  * empty all volumes created by `docker-compose`
  * copy all data from "docker only" volumes to "compose" volumes.
  * stop and recycle your dummy container.
  * `docker-compose up -d` and enjoy.
  * if all went good and smooth, delete the other unneeded volumes.
  
  # If you know what your doing: use external volumes
  
  This will work as well but please check the [documentation](https://docs.docker.com/compose/compose-file/07-volumes/#external) for details. Specifically, be aware that `docker-compose up -d` will no longer try to create the unexisting volumes and fire an error and stop if they don't exist.
  
  Simply change you volume section at the end of your file to:
  
  ```yaml
  volumes:
    db-data-user:
      external:
        name: db-data-user
    db-data-system:
      external:
        name: db-data-system
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks! This is exactly what I needed.  
  
  ---
  
  