简体   繁体   English

在Docker Swarm中使用“绑定”卷安装与docker-compose文件

[英]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. 我的环境使用的是3节点Docker Swarm(都是管理员),我创建了一个docker-compose.yaml来部署到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. 在我的docker-compose.yaml中,我正在设置两个服务,一个是MySQL实例,另一个是我的自定义Django应用。

What I am trying to do is two-fold: 我想做的事有两个方面:

  1. I need to mount a local directory (Example: /test) into the container. 我需要在容器中挂载本地目录(例如:/ test)。 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). 该文件确实存在于主机/节点/服务器上,并且我试图将其安装到容器中存在的文件中(示例:/ tmp)。
  2. Create a persistent database folder so that our MySQL doesn't get destroyed when the container exits. 创建一个持久数据库文件夹,以使我们的MySQL在容器退出时不会被破坏。

My issue is that I am not able to get a local host file (something in this case /test) to show inside the container. 我的问题是我无法获取本地主机文件(在本例中为/ test)显示在容器内。 I have tried using both the long syntax and short syntax to create a “bind mount”. 我尝试使用长语法和短语法来创建“绑定安装”。

Here is my docker-compose.yaml file: 这是我的docker-compose.yaml文件:

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! 感谢您的协助!

You need to add named volumes to the docker-compose.yaml file. 您需要将命名卷添加到docker-compose.yaml文件。

Before starting the instance, run 在启动实例之前,运行

docker volume create mysql-data

Then, in docker-compose.yaml add: 然后在docker-compose.yaml添加:

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. 如果您终止了该服务,则数据仍将保留。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM