简体   繁体   English

Windows 7上的Docker-compose:使用Yaml文件时如何导入数据库转储?

[英]Docker-compose on Windows 7: how to import a db dump when using a yaml file?

I'm started from here: https://docs.docker.com/compose/wordpress/ 我从这里开始: https//docs.docker.com/compose/wordpress/

I got this running well on my windows 7 我在Windows 7上运行良好

version: '2'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: wordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_PASSWORD: wordpress

  phpmyadmin:
    container_name: phpmyadmin
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin:latest
    environment:
      MYSQL_ROOT_PASSWORD: "mysql_root_password"
      PMA_ARBITRARY: 1
    restart: always
    ports:
     - 8080:80
    volumes:
     - /sessions
volumes:
    db_data:

My question is: how can I import a db-dump from a Windows Machime? 我的问题是:如何从Windows Machime导入db-dump? My goal is to work locally on a dump of the production (I've already have the dump in the same folder of my docker-compose.yaml file). 我的目标是在生产的转储上本地工作(我已经将转储放在docker-compose.yaml文件的同一文件夹中)。

If you bash into the MySQL container and the DB was created and the data imported then your first issue is fix. 如果您bash到MySQL容器中并且创建了数据库并导入了数据,那么您的第一个问题就是解决。

Regarding the second one I'm not able to login from within phpMyAdmin and because I am not seeing the service listed on your docker-compose.yml I would recommend to install and configure it following the docs at phpmyadmin . 关于第二个, I'm not able to login from within phpMyAdmin并且因为我没有在docker-compose.yml上看到列出的服务, docker-compose.yml我建议根据phpmyadmin上的文档进行安装和配置。 Very important read the ENV variables here since you need to setup them in order to allow the phpMyAdmin container read/write from/to the MySQL container. 非常重要的一点是在此处阅读ENV变量因为您需要对其进行设置,以便允许phpMyAdmin容器从MySQL容器读取/向MySQL容器写入。

Ex (from here ): 例如(从这里开始 ):

phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    environment:
     - PMA_ARBITRARY=1
    restart: always
    ports:
     - 8080:80
    volumes:
     - /sessions

You're missing this part on your Dockerfile or maybe you don't get just yet how Docker works at all which lead me to recommend you to read a bit about it. 您在Dockerfile上缺少这部分,或者也许您还没有完全了解Docker的工作方式,这促使我建议您阅读一下。

UPDATE UPDATE

Since the answer is non longer related to phpMyAdmin and now turns into docker volumes under Windows here is the updated answer. 由于答案不再与phpMyAdmin有关,现在变成Windows下的docker volumes ,因此这里是更新的答案。

Mount volumes on Windows work as in Linux, ex: Windows上的安装卷与Linux中的安装卷相同,例如:

php-fpm:
    build: docker/php-fpm
    volumes:
        - ./sources:/data/www

The service definition above will work on both OS, you can try it here . 上面的服务定义在两个OS上都可以使用,您可以在此处尝试。 I am using latest Docker for Windows (the Beta tagged as RCXX) 我正在使用Windows的最新Docker(Beta标记为RCXX)

Now a workaround for your case could be either mount a host volume where you have the .sql dump file or use the COPY command from Docker to move the file inside the container and then use RUN or a bash script to import it back to the DB. 现在,针对您的情况的解决方法可能是在主机卷上安装了.sql转储文件,或者使用Docker的COPY命令将文件移至容器内,然后使用RUN或bash脚本将其导入回数据库。

One way that you can do this is to mount a folder with the dump into the directory /docker-entrypoint-initdb.d/ when using the official mysql image. 一种执行此操作的方法是,使用官方的MySQL映像时,将带有转储的文件夹安装到目录/docker-entrypoint-initdb.d/

This is a pretty good solution for working in a team, where everyone should be working off the same base db. 对于每个人都应该在同一个基础数据库上工作的团队来说,这是一个很好的解决方案。 You can read more about that in the documentation. 您可以在文档中阅读有关此内容的更多信息 This will only occur on the first run of the container. 这只会在容器的第一次运行中发生。 If you delete the container, it will do a fresh import. 如果删除容器,它将重新导入。 This has it's uses as well. 它也有它的用途。

More often, I do as some of the other answers suggest: 我经常会像其他答案所建议的那样做:

docker-compose exec db mysql -u root -p DB_NAME < DUMP_FILE_IN_CONTAINER

It's important to note, that you still need to mount the dump file in the container! 需要特别注意的是,您仍然需要在容器中挂载转储文件! It will not work for a file on the host machine! 它不适用于主机上的文件!

You don't necessarily need to mount an entire folder (though you can - all scripts in the above-mentioned folder will execute in alphabetical order). 您不一定需要安装整个文件夹(尽管您可以安装-上述文件夹中的所有脚本都将按字母顺序执行)。 You can just mount your dump file like this: 您可以像这样挂载转储文件:

services:
  db:
  image: mysql:5.7
  volumes:
    - db_data:/var/lib/mysql
    - ./path/to/dump/on/host.sql:/docker-entrypoint-initdb.d/anyname.sql
  restart: always
  environment:
    MYSQL_ROOT_PASSWORD: wordpress
    MYSQL_DATABASE: wordpress
    MYSQL_USER: wordpress
    MYSQL_PASSWORD: wordpress

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

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