问题描述
我试图弄清楚如何使用 docker-compose.yml 和从 sql 转储导入的 2 个数据库来实现 docker.
I'm trying to figure out how to implement docker using docker-compose.yml with 2 databases imported from sql dumps.
httpd:
container_name: webserver
build: ./webserver/
ports:
- 80:80
links:
- mysql
- mysql2
volumes_from:
- app
mysql:
container_name: sqlserver
image: mysql:latest
ports:
- 3306:3306
volumes:
- ./sqlserver:/docker-entrypoint-initdb.d
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: dbname1
MYSQL_USER: dbuser
MYSQL_PASSWORD: dbpass
mysql2:
extends: mysql
container_name: sqlserver2
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: dbname2
MYSQL_USER: dbuser
MYSQL_PASSWORD: dbpass
app:
container_name: webdata
image: php:latest
volumes:
- ../php:/var/www/html
command: "true"
以上返回以下内容:
Kronos:mybuild avanche$ ./run.sh
Creating sqlserver
Creating webdata
Creating sqlserver2
ERROR: for mysql2 driver failed programming external connectivity on endpoint sqlserver2 (6cae3dfe7997d3787a8d59a95c1b5164f7431041c1394128c14e5ae8efe647a8): Bind for 0.0.0.0:3306 failed: port is already allocated
Traceback (most recent call last):
File "<string>", line 3, in <module>
File "compose/cli/main.py", line 63, in main
AttributeError: 'ProjectError' object has no attribute 'msg'
docker-compose returned -1
基本上,我试图在单个 docker compose 文件中设置我的整个堆栈,创建 2 个数据库并导入相应的 sql 转储.有人有什么建议吗?
Basically, I'm trying to get my whole stack setup in a single docker compose file, create 2 databases and import the respective sql dumps.Anyone have any suggestions?
推荐答案
作为对任何可能调查此问题的其他人的更新.
Just as an update to anyone else who may look into this.
我通过删除解决了这个问题:
I solved this by removing:
MYSQL_DATABASE: dbname
来自docker-compose.yml,并将相关的create database 语句直接添加到传递给docker-entrypoint-initdb.d 的sql 文件中.
from docker-compose.yml and adding the relevant create database statements directly to the sql file being passed to docker-entrypoint-initdb.d.
在那个阶段,sql命令是在root下执行的,所以你还需要添加一条语句,为你要使用的数据库用户授予相关权限.
At that stage, sql commands are performed under root, so you'll also need to add a statement to grant relevant permissions to the database user you want to use.
这篇关于docker-compose 与多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!