Wordpress: Not connecting to the linked database

Created on 12 Jan 2016  Â·  9Comments  Â·  Source: docker-library/wordpress

Steps to reproduce:

  1. create a new docker-compose.yml
  2. paste the example from the website and save:

wordpress:
image: wordpress
links:
- db:mysql
ports:
- 8080:80

db:
image: mariadb
environment:
MYSQL_ROOT_PASSWORD: example

  1. $ docker-compose up
  2. wait a little
  3. go to the exposed web front end

Error establishing a database connection

I created a more detailed explanation (plus full log) here:
http://stackoverflow.com/questions/34746853/docker-wordpress-container-cant-connect-to-database-container

EDIT: I just tried using:
image: wordpress:4.4

and the exmaple did work so it seems to be a bug introduced between 4.4 and 4.4.1

Most helpful comment

Since you have the services section you'll also need a version: '2' and have to give the password, user, and database to WordPress too.

version: '2'
services:
  wordpress:
    image: wordpress
    volumes:
      - ./src:/var/www/html
    environment:
      WORDPRESS_DB_HOST: wp_db
      WORDPRESS_DB_USER: my-user
      WORDPRESS_DB_PASSWORD: my-password
      WORDPRESS_DB_NAME: my-db
    ports:
      - "8080:80"
  wp_db:
    image: mysql:5.5
    environment:
      MYSQL_ROOT_PASSWORD: some-secure-pw
      MYSQL_USER: my-user
      MYSQL_PASSWORD: my-password
      MYSQL_DATABASE: my-db

All 9 comments

Can you try doing "docker-compose pull" and giving it another shot?

Yep it works!

Thanks.
I'll close the issue.

Sorry to keep asking on a closed issue but I assumed that you did update the image and that what solved was that pull did pull a new image but I've just checked the repo and seen that there haven't been new pushes for 3 days…

What did docker-compose pull do?
In what was that different than removing the image and making docker-compose up with "wordress:latest"?

I'd like to know for next time.

Thanks!

If that fixed it, then docker-compose pull grabbed the new image from the hub. Doing an rm and up would have only used the image you had locally, unless you also did docker rmi wordpress so that it would have to pull the image. docker-compose rm only deletes containers and not images.

It was a bug with the line endings and was fixed in this repo in https://github.com/docker-library/wordpress/pull/117, but was not added to the build server until https://github.com/docker-library/official-images/pull/1328 was merged. The docker-library/official-images repo is what controls the builds and pushes to the hub.

I have tried removing my local wordpress image and running docker-compose pull but I am still experiencing this issue. Here is my docker-compose.yml -

services:
  wordpress:
    image: wordpress
    volumes:
      - ./src:/var/www/html
    links:
      - wp_db:mysql
    ports:
      - "8080:80"
  wp_db:
    image: mysql:5.5
    environment:
      MYSQL_ROOT_PASSWORD: some-secure-pw
      MYSQL_USER: my-user
      MYSQL_PASSWORD: my-password
      MYSQL_DATABASE: my-db

Since you have the services section you'll also need a version: '2' and have to give the password, user, and database to WordPress too.

version: '2'
services:
  wordpress:
    image: wordpress
    volumes:
      - ./src:/var/www/html
    environment:
      WORDPRESS_DB_HOST: wp_db
      WORDPRESS_DB_USER: my-user
      WORDPRESS_DB_PASSWORD: my-password
      WORDPRESS_DB_NAME: my-db
    ports:
      - "8080:80"
  wp_db:
    image: mysql:5.5
    environment:
      MYSQL_ROOT_PASSWORD: some-secure-pw
      MYSQL_USER: my-user
      MYSQL_PASSWORD: my-password
      MYSQL_DATABASE: my-db

@yosifkit
Sorry to keep asking on a closed issue but my problem is not resolved till now. My script is:

version: '3.7'

services:
  # Database
  db:
    image: mysql
    restart: always
    volumes:
      - db_data:/var/lib/mysql
    ports:
        - '8200:3306'
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    networks:
      - wpsite
  # phpmyadmin
  phpmyadmin:
    depends_on:
      - db
    image: phpmyadmin/phpmyadmin
    restart: always
    ports:
      - '8201:80'
    environment:
        PMA_HOST: db
        PMA_PASSWORD: password
        MYSQL_ROOT_PASSWORD: password
    networks:
      - wpsite
  # Wordpress
  wordpress:
    depends_on:
      - db
    image: wordpress
    ports:
      - '8202:80'
    restart: always
    volumes: ['./:/var/www/html']
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
    networks:
      - wpsite
networks:
  wpsite:
volumes:
  db_data:

I am trying it to host it in another computer but whenever I go to port 8201 and try to login with root and password it says cant connect and in port 8202 it says cant connect to database

@ayushpadia, most likely you need to give mysql longer to come up and when using mysql:latest you are probably running into https://github.com/docker-library/mysql/issues/409.

For further debugging, you might want to try the Docker Community Forums, the Docker Community Slack, or Stack Overflow.

@ayushpadia Try this.

kill and remove the previous running container

docker down

in docker-compose.yml Replace all properties under db service with the code below.

  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    container_name: db
    restart: always
    environment:
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
      MYSQL_ROOT_HOST: '%'
      MYSQL_DATABASE: wordpress
      MYSQL_ROOT_PASSWORD: password`

The reason why wordpress service can't connect is that you changed the default port of the database without telling the * WORDPRESS_DB_HOST* for the new port so it should be like this db:8200. or you can leave the default port by not defining it.

Was this page helpful?
0 / 5 - 0 ratings