Search code examples
dockerdocker-composedocker-swarmdocker-volume

Using “bind” volume mount inside of Docker Swarm with a docker-compose file


My environment is using a 3 node Docker Swarm (all being managers) and I have a docker-compose.yaml that was created to deploy out to the Swarm.

Inside my docker-compose.yaml I have two services being setup, one is a MySQL instance and the other is my custom Django App.

What I am trying to do is two-fold:

  1. I need to mount a local directory (Example: /test) into the container. This file does exist on the host/node/server and I am trying to mount it to a file that exists in the container (Example: /tmp).
  2. Create a persistent database folder so that our MySQL doesn’t get destroyed when the container exits.

My issue is that I am not able to get a local host file (something in this case /test) to show inside the container. I have tried using both the long syntax and short syntax to create a “bind mount”.

Here is my docker-compose.yaml file:

version: '3.2'
services:
  project_mysql:
    environment:
      MYSQL_USER: 'project'
      MYSQL_PASSWORD: 'password1234'
    ports:
      - 3306:3306
    image: 'mysql/mysql-server'
    tty: true
    stdin_open: true
    deploy:
      mode: replicated
      replicas: 1
      restart_policy:
        condition: on-failure
      placement:
        constraints:
          - node.hostname == node1
    healthcheck:
      test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
      interval: "5s"
      timeout: "1s"

  project_web:
    image: 'localhost:5123/project_web:0.1.5'
    tty: true
    stdin_open: true
    volumes:
      - type: bind
        source: /test
        target: /tmp
    ports:
      - 8000:8000
    depends_on:
      - project_mysql
    healthcheck:
      test: ["CMD-SHELL", "nc -z 127.0.0.1 8000 || exit 1"]
      interval: "5s"
      timeout: "1s"

networks:
  projectnet:
    driver: overlay
    ipam:
      config:
        - subnet: 10.2.0.0/24

Thanks for any assistance!


Solution

  • You need to add named volumes to the docker-compose.yaml file.

    Before starting the instance, run

    docker volume create mysql-data

    Then, in docker-compose.yaml add:

    services:
      project_mysql:
        volumes:
          - mysql-data:/var/lib/mysql
    
    volumes:
      mysql-data:
        external: true
    

    If you ever kill the service, the data will still persist.