Reputation:
I run docker wordpress image with command
docker run --name test-wordpress -p 8081:80 -d wordpress
MySQL 8 is on localhost on Windows 10. Database credentials are valid.
In wordpress setup I use this configuration
I get error
Error establishing a database connection
What is correct database host?
Upvotes: 6
Views: 23022
Reputation: 4298
The accepted answer is incomplete and inaccurate. You can very easily connect to MySQL running in host machine with wordpress running inside container by providing --network=host
option during docker run
.
Issue With Wordpress:
The only caveat is wordpress
can't connect with MySQL
with default setup.
In Ubuntu systems running MySQL 5.7 (and later versions), any MySQL user is set to authenticate using the auth_socket plugin
by default rather than with a password
. This allows for some greater security and usability in many cases, but it can also complicate things when you need to allow an external incompatible program like wordpress
to access the user.
In order to use a password
to connect to MySQL as user, you will need to switch its authentication method from auth_socket
to mysql_native_password
.
Solution:
Create a dedicated mysql user for wordpress database in legacy mysql_native_password
mode.
$> sudo mysql
mysql> CREATE USER 'wpuser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'wp_password';
mysql> CREATE DATABASE wordpress_db CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
mysql> GRANT ALL ON wordpress_db.* TO 'wpuser'@'localhost';
mysql> FLUSH PRIVILEGES;
Run Wordpress container:
docker run --rm --name my_wordpress --network=host -e WORDPRESS_DB_USER=wpuser -e WORDPRESS_DB_PASSWORD=wp_password -e WORDPRESS_DB_NAME=wordpress_db -e WORDPRESS_DB_HOST=127.0.0.1 wordpress
Upvotes: 3
Reputation: 13
Found a great article explaining How to setup Wordpress using Docker.
The issue is MySQL container is not accessible from the wordpress container, so to fix this we will have to create docker network and connect both the containers.
Lets create a docker network -
> docker network create --attachable wordpress-network
Now attach both the containers with following command -
> docker network connect wordpress-network mysql-container
> docker network connect wordpress-network wordpress-container
Now open the wordpress in browser and set mysql-container as Database Host
Upvotes: 1
Reputation: 4220
By default, docker will attach your new container to a bridged network. This means that addresses such as: localhost
and 127.0.0.1
only refers to the container itself. Not the host machine.
The easy was to solve this, is to wrap the MySQL database in a container of it's own. This way your containers can address eachother without issues.
If you really want to connect the service in the container with a service on the host, you will need to connect the container to the appropriate network.
First, you will need to create a network. Assuming that your local machine has a fixed IP of 192.168.0.1
, you should be able to do this with:
docker network create -d bridge --subnet 192.168.0.0/24 --gateway 192.168.0.1 dockernet
You can then:
docker run --name test-wordpress --net=dockernet -p 8081:80 -d wordpress
And you should then be able to refer to the host from inside the container by the IP: 192.168.0.1
.
The better alternative here though, is to create an application stack definition with docker-compose, that includes both the database and the wordpress application.
You can create a docker-compose.yml
file like this:
docker-compose.yml
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
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_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
And the start the stack with this:
docker-compose up
Then visit: http://localhost:8000
Notice that the database data will be stored in the docker managed volume called db_data
.
Details on installing docker-compose can be found here: https://docs.docker.com/compose/install/
docker run
The same can be achieved with just docker run
, like this:
docker volume create db_data
docker network create mysqlnet
docker run --name test-mysql -e MYSQL_ROOT_PASSWORD=somewordpress -e MYSQL_DATABASE=wordpress -e MYSQL_USER=wordpress -e MYSQL_PASSWORD=wordpress -v db_data:/var/lib/mysql --net=mysqlnet -d mysql:5.7
docker run --name test-wordpress -e WORDPRESS_DB_HOST=test-mysql:3306 -e WORDPRESS_DB_USER=wordpress -e WORDPRESS_DB_PASSWORD=wordpress -e WORDPRESS_DB_NAME=wordpress --net=mysqlnet -p 8081:80 -d wordpress:latest
You can change the mapping of the mysql datafiles to a local directory instead, and just ommit the docker volume create
statement.
Upvotes: 15