The Dark Bug Returns
The Dark Bug Returns

Reputation: 111

Wordpress Docker container can't connect to database container

I am trying to migrate a worpdress site to a docker container for local development.

However every time I use docker compose I keep getting this:

MySQL Connection Error: (1045) Access denied for user 'root'@'172.22.0.3'

I've double checked the passwords and validated them via shell on the db container.

Here is my docker-compose file:

services: # configuring each container
db: # name of our mysql container
image: mysql:5.7 # which image to pull, in this case specifying v. 5.7
volumes: # data to map to the container
  - ./data:/docker-entrypoint-initdb.d # where to find our data - we'll talk more about this
restart: always # always restart the container after reboot
environment: # environment variables -- mysql options in this case
  MYSQL_ROOT_PASSWORD: *****
  MYSQL_DATABASE: **_***
  MYSQL_USER: *****
  MYSQL_PASSWORD: *****

....
wordpress:
    depends_on: # container dependencies that need to be running first
  - db
  image: wordpress:latest # image used by our container
   ports:
  - "8080:80" # setting our ports for networking
   restart: always
   environment:
     WORDPRESS_DB_HOST: db:3306 # default mysql port
     WORDPRESS_DB_PASSWORD: **** # matches $MYSQL_PASSWORD
   volumes: # this is where we tell Docker what to pay attention to
  - ./wp-content/themes/chronus:/var/www/html/wp-content/themes/chronus # mapping our custom theme to the container
  - ./wp-content/plugins:/var/www/html/wp-content/plugins # map our plugins to the container
  - ./wp-content/uploads:/var/www/html/wp-content/uploads # map our uploads to the container

Upvotes: 1

Views: 4737

Answers (1)

Maximilian C.
Maximilian C.

Reputation: 967

I am assuming you are using the official Wordpress image from docker hub. You have specifified the WORDPRESS_DB_PASSWORD flag but not the WORDPRESS_DB_USER. That means, the wordpress plugin is defaulting to root.

However, as per your comments, you entered the the password for some arbitrary user here (not the root user).

Modify the wordpress containers' environment variables as follows in your compose file and it should work:

   environment:
       WORDPRESS_DB_HOST: db:3306 # default mysql port
       WORDPRESS_DB_PASSWORD: **** # matches $MYSQL_PASSWORD
       WORDPRESS_DB_USER: **** # matches $MYSQL_USER

Upvotes: 1

Related Questions