简体   繁体   English

如何使用 mysql 图像创建 docker-compose 文件以将所有数据保存在卷内

[英]How to create docker-compose file with mysql image to save all data inside volume

I'm trying to write docker-compose file for my Spring boot app for deploy this app on another pc.我正在尝试为我的 Spring 启动应用程序编写 docker-compose 文件,以便在另一台电脑上部署此应用程序。 Here's mine docker-compose file.这是我的 docker-compose 文件。

version: "3.7"
services:
  db:
    image: messor2000/mysql
    ports:
      - "3308:3306"
    environment:
      MYSQL_DATABASE: web_library
      MYSQL_USER: user
      MYSQL_PASSWORD: password
      MYSQL_ROOT_PASSWORD: password
    volumes:
      - /var/lib/docker/volumes/e24e69d7db152cea837b440ff02baa627d9d3e44ff9ad4c6ca3e6d1ac09f6a4e/_data

It works almost the way I want it to.它几乎按照我想要的方式工作。 I write docker-compose up command on another pc my container with the base is starting(with my bd and all tables in it), but the data from the tables were not transferred.我在另一台电脑上写了 docker-compose up 命令,我的容器与基础正在启动(我的 bd 和其中的所有表),但表中的数据没有传输。 As I know data inside my mysql tables show must be prescribed in volumes: , if this is correct please advise how to correctly spell let to my data.据我所知,我的 mysql 表中的数据必须按卷规定:,如果这是正确的,请告知如何正确拼写我的数据。 In my situation I have such config in my container mounts:在我的情况下,我的容器安装中有这样的配置:

"Mounts": [
            {
                "Type": "volume",
                "Name": "e24e69d7db152cea837b440ff02baa627d9d3e44ff9ad4c6ca3e6d1ac09f6a4e",
                "Source": "/var/lib/docker/volumes/e24e69d7db152cea837b440ff02baa627d9d3e44ff9ad4c6ca3e6d1ac09f6a4e/_data",
                "Destination": "/var/lib/mysql",
                "Driver": "local",
                "Mode": "",
                "RW": true,
                "Propagation": ""
            }
        ],

So with path show I write to my docker compose file, to solve my problem?因此,使用路径显示我写入我的 docker 撰写文件,以解决我的问题? I also tried to write in volumes: -/var/lib/mysql Thank you very much我也试过分卷写:-/var/lib/mysql 非常感谢

You should almost never directly refer to paths in /var/lib/docker .您几乎不应该直接引用/var/lib/docker中的路径。 These are in a part of the filesystem managed by Docker and the exact paths and filesystem contents are subject to change between Docker releases.这些位于 Docker 管理的文件系统的一部分中,确切的路径和文件系统内容可能会在 Docker 版本之间发生变化。

If you're trying to directly transplant the database data from one machine to another, you can use a bind mount to store the data directly in a host directory.如果您尝试将数据库数据从一台机器直接移植到另一台机器,则可以使用绑定挂载将数据直接存储在主机目录中。 This exposes you to some potential permission problems, and it can be slower on some host operating systems, but it is otherwise very straightforward.这会使您面临一些潜在的权限问题,并且在某些主机操作系统上可能会变慢,但在其他方面非常简单。

version: '3.8'
services:
  db:
    # user: 1000  # result of `id -u`, may be needed to give R/W access
    volumes:
      - ./dbdata:/var/lib/mysql
      # ^^ host system relative path
here$ docker-compose down
here$ tar cvf myapp.tar docker-compose.yml dbdata
here$ scp myapp.tar there:
here$ ssh there
there$ tar xzf myapp.tar
there$ docker-compose up -d

You can also use a Docker named volume , in which case Docker manages the storage for you.您还可以使用名为 volume的 Docker ,在这种情况下 Docker 会为您管理存储。 While the Docker documentation is extremely enthusiastic about named volumes, tasks like backing up and restoring named volumes require complex steps with temporary containers.虽然 Docker 文档对命名卷非常感兴趣,但备份恢复命名卷等任务需要使用临时容器执行复杂的步骤。

version: '3.8'
services:
  db:
    volumes:
      - dbdata:/var/lib/mysql
      # ^^^^^^ no slashes; matches top-level volumes: block
volumes:
  dbdata:
here$ docker-compose down
here$ docker-compose run -w /var/lib/mysql db \
> tar cf - . \
> > dbdata.tar
here$ scp docker-compose.yml dbdata.tar there:
here$ ssh there
there$ docker-compose run -w /var/lib/mysql db \
> tar xf - . \
> < dbdata.tar
there$ docker-compose up -d

In both cases, note that we've completely ignored the /var/lib/docker directory.在这两种情况下,请注意我们完全忽略了/var/lib/docker目录。 Also note that the volumes: mount explicitly names the /var/lib/mysql directory in the container as the mount point.另请注意, volumes: mount 将容器中的/var/lib/mysql目录明确命名为挂载点。

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

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