我创建了一个小的docker-compose.yml
,曾经像魅力一样工作,以部署小型WordPress实例.它看起来像这样:
wordpress: image: wordpress:latest links: - mysql ports: - "1234:80" environment: WORDPRESS_DB_USER: wordpress WORDPRESS_DB_NAME: wordpress WORDPRESS_DB_PASSWORD: "password" WORDPRESS_DB_HOST: mariadb MYSQL_PORT_3306_TCP: 3306 volumes: - /srv/wordpress/:/var/www/html/ mysql: image: mariadb:latest mem_limit: 256m container_name: mariadb environment: MYSQL_ROOT_PASSWORD: "password" MYSQL_DATABASE: wordpress MYSQL_USER: wordpress MYSQL_PASSWORD: "password" volumes: - /srv/mariadb:/var/lib/mysql
但是当我现在开始它时(可能是因为Docker更新到Docker版本1.9.1,构建a34a1d5
),它失败了
wordpress_1 | Warning: mysqli::mysqli(): (HY000/2002): Connection refused in - on line 10 wordpress_1 | wordpress_1 | MySQL Connection Error: (2002) Connection refused
当我有猫/etc/hosts
的wordpress_1
条目有MySQL:
172.17.0.10 mysql 12a564fdbc56 mariadb
我能够ping MariaDB服务器.
当我docker-compose up
安装WordPress并在几次重启后,MariaDB容器打印:
Version: '10.0.22-MariaDB-1~jessie' socket: '/var/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution
哪个schould表明它正在运行,不是吗?
如何让WordPress能够连接到MariaDB容器?
此行为的原因可能与最近的内核和docker更新有关.我在其他docker-compose设置中认识到了其他一些连接问题.因此,我重新启动了服务器(不仅仅是docker服务),从那时起就没有任何类似的问题.
要解决这个问题,首先要做的是:
将以下代码添加到wordpress和数据库容器中(在docker-compose文件中):
restart: unless-stopped
这将确保在wordpress容器尝试连接之前启动并初始化数据库.然后重启docker引擎
sudo restart docker
或(对于ubuntu 15+)
sudo service docker restart
这里有完整的配置,对我来说,使用MariaDB设置wordpress:
version: '2' services: wordpress: image: wordpress:latest links: - database:mariadb environment: - WORDPRESS_DB_USER=wordpress - WORDPRESS_DB_NAME=mydbname - WORDPRESS_TABLE_PREFIX=ab_ - WORDPRESS_DB_PASSWORD=password - WORDPRESS_DB_HOST=mariadb - MYSQL_PORT_3306_TCP=3306 restart: unless-stopped ports: - "test.dev:80:80" working_dir: /var/www/html volumes: - ./wordpress/:/var/www/html/ database: image: mariadb:latest environment: - MYSQL_ROOT_PASSWORD=password - MYSQL_DATABASE=mydbname - MYSQL_USER=wordpress - MYSQL_PASSWORD=password restart: unless-stopped ports: - "3306:3306"