【发布时间】:2018-03-16 16:54:11
【问题描述】:
我确定这是一个重复的主题,但我根本无法完成:我喜欢在运行时将我的数据库转储恢复到 MySQL 容器,而不修改 docker-compose.yml文件。
Dockerfile
FROM php:5.4.45-apache
RUN apt-get update
RUN docker-php-ext-install mysql mysqli
docker-compose.yml
version: '2'
services:
php_service:
container_name: my_php
# Use Dockerfile in this dir to build the image
build: .
# Stop containers always after exiting.
restart: always
ports:
# 'localhost' does not works from the host, but the IP of docker itself
# (192.168.99.100 for example - shown on the top of the console)
- "80:80"
- "443:443"
environment:
# Pass variables
- API_TOKEN=xxxx
volumes:
# The current directory will be mounted to '/var/www/html'
# WORKS ONLY IN USER'S DIR ON WINDOWS (~/Downloads for example)
- .:/var/www/html
# See https://hub.docker.com/_/mysql/ for additional information.
# To open up console, run `docker exec -it my_mysql bash`.
# To restore a dump `docker exec -i my_mysql /usr/bin/mysql -u root
# --password=test_pass DATABASE < DUMP.sql` should work, but it never did.
mysql_service:
container_name: my_mysql
# Use an existing image
image: mysql:5.6
restart: always
ports:
# Let it accessible for other apps (mysql on host, IDE, etc.)
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: 'test_pass' # TODO: Change this
MYSQL_USER: 'test'
MYSQL_PASS: 'pass'
volumes:
# Named volumes (my-datavolume) has to be listed in the "volumes"
# section - I don't know how it works or what is it doing at all...
# (-_-')
- my-datavolume:/var/lib/mysql
volumes:
my-datavolume:
重现步骤:
- 在 Windows 7 主机上启动 Docker Toolbox
docker-compose up- 打开一个新的 Docker Toolbox 终端
docker exec my_msql /usr/bin/mysql -u root --password=test_pass -e 'CREATE DATABASE testdb;'docker exec -i my_mysql /usr/bin/mysql -u root --password=test_pass testdb < dump_on_host.sqldocker exec -it my_mysql /usr/bin/mysql -u root --password=test_pass testdbmysql> SHOW TABLES;
数据库为空。似乎它什么也没做,因为终端响应太快了。我通过在我的主机上安装 MySQL 尝试了转储,它可以恢复。
【问题讨论】:
-
你能得到像
docker exec -i my_mysql wc < dump_on_host.sql这样的正确输出吗? -
感谢您的建议 - 它说输入为空 (
0 0 0)。 -
假设文件确实存在,也许管道没有工作?试试
cat dump_on_host.sql | docker exec ... -
不行,试了没成功。
-
cat dump_on_host.sqlin Docker Toolbox (aka: host) 显示整个文件,所以它不是空的。
标签: mysql docker containers restore dump