Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose mysql container denies access to wordpress container

I have a problem with mysql 5.7 container denying access to wordpress container. I'm using docker-compose and I'm running docker on Mac OSX. Docker should be on latest version available.

Here's my docker-compose.yml

version: '2'

services:
  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    container_name: wordpress
    ports:
      - "8000:80"
      - "443:443"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_NAME: blog
      WORDPRESS_DB_USER: blog_admin
      WORDPRESS_DB_PASSWORD: userpasswd
    networks:
      - wordpress_net
  db:
    image: mysql:5.7
    container_name: db
    ports:
      - "3306:3306"
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: rootpasswd
      MYSQL_DATABASE: blog
      MYSQL_USER: blog_admin
      MYSQL_PASSWORD: userpasswd
    networks:
      - wordpress_net
networks:
  wordpress_net:
volumes:
  db_data:

Logs from db container are:

2017-05-12T23:28:06.138429Z 321 [Note] Access denied for user 'blog_admin'@'172.19.0.3' (using password: YES)

Logs from wordpress container are:

MySQL Connection Error: (1045) Access denied for user 'blog_admin'@'172.19.0.3' (using password: YES)
Warning: mysqli::mysqli(): (HY000/1045): Access denied for user 'blog_admin'@'172.19.0.3' (using password: YES) in - on line 22

docker ps:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                        NAMES
1b02f0146fe7        wordpress:latest    "docker-entrypoint..."   25 minutes ago      Up 26 seconds       0.0.0.0:443->443/tcp, 0.0.0.0:8000->80/tcp   wordpress
5d932ed6c269        mysql:5.7           "docker-entrypoint..."   25 minutes ago      Up 25 minutes       0.0.0.0:3306->3306/tcp                       db

What have I tried:

  1. Restarting docker host.
  2. docker-compose rm -v and then docker-compose up -d again.
  3. Logging in with those user credentials and root credentials outside of wordpress container.
  4. Removing docker images and pulling them again from scratch.
  5. Using root credentials in WORDPRESS_DB_HOST, WORDPRESS_DB_USER

I can see all the env vars for db when I connect to db container. Wordpress container keeps restarting it self. I saw one answer on stack overflow which recommended flushing privileges and setting new user account but I want to know if I'm doing something wrong that could cause this problem to appear again on other machine.

like image 882
Matus Kacmar Avatar asked May 12 '17 23:05

Matus Kacmar


People also ask

Can I run WordPress in a container?

You can use Docker Compose to easily run WordPress in an isolated environment built with Docker containers.

How do I Dockerize my WordPress site?

Running WordPress in Docker requires two separate containers: a web container, running Apache and PHP, and a database container, hosting MySQL. You must also set up Docker volumes for the WordPress data directories. These store your configuration files and uploaded media so they persist across container restarts.


1 Answers

Change:

WORDPRESS_DB_USER: blog_admin
WORDPRESS_DB_PASSWORD: userpasswd

To:

WORDPRESS_DB_USER: root
WORDPRESS_DB_PASSWORD: rootpasswd

And then:

docker-compose up -d --build

Your username Blog_admin doesn't have access to create database.

like image 131
gobliggg Avatar answered Oct 30 '22 19:10

gobliggg