Docker WordPress connection to the database server on the localhost


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

  • Database name: wordpress (not exist yet)
  • Username: root
  • Password: ***
  • Database host: localhost || || host.docker.internal

I get error

Error establishing a database connection

What is correct database host?


By default, docker will attach your new container to a bridged network. This means that addresses such as: localhost and 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.

Connect to MySQL database on the host

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, you should be able to do this with:

docker network create -d bridge --subnet --gateway 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:

Create stack with wordpress and MySQL

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:


version: '3.3'

     image: mysql:5.7
       - db_data:/var/lib/mysql
     restart: always
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

       - db
     image: wordpress:latest
       - "8000:80"
     restart: always
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       WORDPRESS_DB_NAME: wordpress
    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:

Start both containers with just 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.

Answered By – Andreas Lorenzen

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published