Docker Wordpress not connecting with docker Mysql Compose - mysql

I am trying to set up multiple WordPress sites being hosting on the same server using docker using nginx reverse proxy server. I have a network setup with the nginx proxy listening on port 80 called nginx-proxy. I confirmed they are all on the same docker network. However, I am still getting WordPress is unable to communicate with the database. I can confirm that the credentials are correct and that MySQL is running on the other docker container.
With my testing, the forwarding proxy is working if I update the virtual host and open it in the web browser it does open the error message on that page. I can ping both the containers from within their shell. I am not sure what I am doing wrong I am newer to dockers networking within version 3. I don't believe docker has differenced between platforms, but in case it does I am running it on Mac OS.
I know there are other posts similar to this using Link and version 2 however, I was not able to find a version 3 with information that fixed my issue.
version: "3.3"
services:
db_node_domain:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: PASSWORD
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: PASSWORD
container_name: wordpress_db
wordpress:
depends_on:
- db_node_domain
image: wordpress:latest
expose:
- 80
restart: always
environment:
VIRTUAL_HOST: domain.com
WORDPRESS_DB_HOST: db_node_domain:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: PASSWORD
container_name: wordpress
volumes:
db_data: {}
networks:
default:
external:
name: nginx-proxy
Please let me know if any more information is required. Thank you for any assistance you might be able to provide.

I don't see any problem with your docker-compose file, also it's working on my local machine which is macOS.
One thing, 'wordpress' container will fail to bring up several times with MySQL Connection Error: (2002) Connection refused on the first bootup due to MySQL initialization. It should connect to 'wordpress_db' after a few retrials.
If it doesn't connect even after retrials, can you attach logs of 'wordpress' container?

You can use this as your docker compose file below.
Reference:
https://github.com/docker/awesome-compose/tree/master/wordpress-mysql
version: '3.7'
services:
db:
image: mysql:8.0.19
command: '--default-authentication-plugin=mysql_native_password'
restart: always
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
- MYSQL_ROOT_PASSWORD=somewordpress
- MYSQL_DATABASE=wordpress
- MYSQL_USER=wordpress
- MYSQL_PASSWORD=wordpress
wordpress:
image: wordpress:latest
ports:
- 80:80
restart: always
environment:
- WORDPRESS_DB_HOST=db
- WORDPRESS_DB_USER=wordpress
- WORDPRESS_DB_PASSWORD=wordpress
- WORDPRESS_DB_NAME=wordpress
volumes:
db_data:

I have isolated the issue. It was the first time I was playing the docker volumes. I forgot to clear out the volumes when I updated the database information. So I was connecting to the old database with the new information which was leading to the error (This is why your don't try to learn something new late into the night on little sleep). Once I removed the volumes recomposed the docker containers it works perfectly.
Thank you for every one who assisted me with this.

Related

MySQL with docker-compose ports doesn't work with network

I am not sure if this is a bug, or a feature. But I have been running this docker-compose setup for my local development databases several months now and since I have updated Docker and Lando it stopped working.
As you can see I use it for some php sites managed with Lando. Since the last update either the ports or the network directive is not working on the MySQL container.
If I use it with the snippet below the ports directive is not working. Meaning I can not connect from QueryPie on localhost:3306, but can connect from another lando docker container with mysql_db as host.
If I comment out the networks directive, the ports directive is
working. But the networks directive obviously not. Meaning I can connect from QueryPie on localhost:3306, but I can not connect from another lando docker container with mysql_db as host.
Before the update they worked nice together. So I could open MySQL in QueryPie and access the databases from the Lando containers.
version: '3'
services:
mysql:
container_name: mysql_db
restart: always
image: mysql:latest
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=secret
volumes:
- ./mysql-data:/var/lib/mysql
networks:
- lando_bridge_network
networks:
lando_bridge_network:
external: true
My docker engine is 19.03.8 and my docker-compose version is 1.25.5.
So I solved it by removing the networks directive and only exposing port 3306 with ports. The PHP sites running on lando/docker I the internal docker IP to connect:
define('DB_HOST', 'host.docker.internal');
In QueryPie (running on the host) I can connect with localhost:3306
version: '3'
services:
mysql:
container_name: mysql_db
restart: always
image: mysql:latest
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=secret
volumes:
- ./mysql-data:/var/lib/mysql

Can't migrate existing MySQL Wordpress database to Docker with docker-compose

I'm trying to migrate a pre-existing Wordpress install from an exported SQL file to a local Docker development environment. I'm somewhat new to Docker, but I have gone through some tutorials and documentation. The problem appears to be that the "wordpress" and "phpmyadmin" services cannot access the database.
I did a search & replace in Vim on the SQL file to replace instances of the original URL with "http://localhost:8000". Then I used docker-compose.
# docker-compose.yml
version: "3.7"
services:
db:
image: mysql:5.7.29
volumes:
- ./dbdata-import/:/docker-entrypoint-initdb.d/ # Where my exported SQL file is stored
# I also tried -./dbdata-import/thedata.sql:/docker-entrypoint-initdb.d/thedata.sql
- ./dbdata:/var/lib/mysql # So local database changes persist
ports:
- "3306:3306"
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wp_database
MYSQL_USER: wp_username
MYSQL_PASSWORD: wp_password
wordpress:
depends_on:
- db
image: wordpress:php7.3-apache
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wp_username
WORDPRESS_DB_PASSWORD: wp_password
WORDPRESS_DB_NAME: wp_database
WORDPRESS_TABLE_PREFIX: wp_ #Tried without and without this
WORDPRESS_DEBUG: 1
volumes:
- ./wp-vol:/var/www/html
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin:4.9.4
container_name: phpmyadmin
environment:
PMA_HOST: db
PMA_USER: admin
PMA_PASSWORD: phpmyadmin_password
restart: always
ports:
- "8080:80"
It might be worth noting that I use this fix so I can still use OpenVPN. Basically, I created a subnet by running docker network create localdev --subnet 10.0.1.0/24. I also added this file next to my docker-compose.yml:
# docker-compose.override.yml
version: '3.7'
networks:
default:
external:
name: localdev
When I access http://localhost:8000, I don't get anything and the browser times out. When I access http://localhost:8080 for PHPMyAdmin, I get the error:
MySQL said: Documentation
Cannot connect: invalid settings.
mysqli_real_connect(): (HY000/1045): Access denied for user 'admin'#'10.0.1.3' (using password: YES)
phpMyAdmin tried to connect to the MySQL server, and the server rejected the connection. You should check the host, username and password in your configuration and make sure that they correspond to the information given by the administrator of the MySQL server.
It seems odd this would be a credential issue. I pulled the Wordpress database information straight from wp-config.php on the host server. I also tested the database, username and password by signing into the MySQL CLI on the host server.
I used docker-compose down -v to delete volumes after I finished and docker volumes ls appears to be empty. So I don't think this is an issue with /docker-entrypoint-initdb.d/ not running because MySQL already initalized. However, I'm not sure.
I've been troubleshooting this for awhile now. I've done an almost identical Docker setup without /docker-entrypoint-initdb.d to create fresh Wordpress installs. That works fine. I could really use some help. I'm currently running Debian 10. Thanks.
UPDATE: I'm still having issues. I verified that I still have the same issues when I shutdown OpenVPN, remove docker-compose.override.yml and remove the localdev network. I get all the same problems. The only difference is that PHPMyAdmin gives me a different IP address after "admin#", which is expected.
I logged into my MySQL container using docker exec -it. Running MySQL CLI with my username and password worked. The tables looked like all the data was imported by docker-entrypoint-initdb.d. So the issue doesn't appear to be with docker-entrypoint-initdb.d, but rather the wordpress and phpmyadmin services can't access the database.
UPDATE 2: I fixed MyPHPAdmin. I didn't realize that PMA_USER and PMA_PASSWORD need to match the Wordpress database. I also needed PMA_HOST to include the port number:
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin:4.9.4
environment:
PMA_HOST: db:3306
PMA_USER: wp_username
PMA_PASSWORD: wp_password
restart: always
ports:
- "8080:80"
I still need help with Wordpress.

Docker error connecting to database - using nginx, mysql and virtual hosts for many other websites

I am new to Docker. I have overtaken another guys business and I need to exchange a running website content with a new one. The running content is a typo3 website and the new one is Wordpress based. The servers handles many different Websites with different urls managed by a nginx reverse proxy.
My problem: I cannot connect to the new database.
My compose file:
version: "2"
services:
#Database
db:
image: mysql
volumes:
- /srv/db/production/website/wordpress/current:/var/lib/mysql
restart: always
networks:
- proxy-tier
- post
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: wordpress_website
MYSQL_USER: wordpress_website
MYSQL_PASSWORD: password
#Wordpress
wordpress:
depends_on:
- db
image: wordpress
restart: always
networks:
- proxy-tier
- post
volumes:
- /srv/www/production/website/wordpress/current:/var/www/html
environment:
- VIRTUAL_HOST=website.com
- VIRTUAL_NETWORK=nginx-proxy
- VIRTUAL_PORT=80
#- VIRTUAL_PORT=443
- LETSENCRYPT_HOST=website.com
- LETSENCRYPT_EMAIL=email#website.com
- WORDPRESS_DB_USER=wordpress_website
- WORDPRESS_DB_PASSWORD=password
- WORDPRESS_DB_NAME=wordpress_website
networks:
proxy-tier:
external:
name: nginxproxy_default
post:
external:
name: mail_frontend
Info:
the networks have been made prior to my work so I think they are
working.
I can perfectly connect to the new database within the terminal (it
is just completely empty)
The wp-config data is correct but the Host just says "mysql".
I have checked with other working docker+mysql+wordpress files and
they have the same host.
I have tried to set different port like ports:- "3307:3306" but it
did not help.
What am I doing wrong? I really need help because I am lost.
Thank you!
$ docker-compose down
$ docker ps -a ##check that the containers are not running
Clean the volumes of the "db" and start again
If the containers are OK, you shuld try again to connect to a host port and see the DB from there.
In the container "db" ->
ports:
- "3309:3306"
- "portHOST:porContainer"
Bye.

docker-compose run two instance of mysql

I want to run two instances of mysql using docker-compose.
I'm running MySQL in a Docker container and I have another Docker container running a python script to access the MySQL database.
One works fine on port 3306. In order to get two working, I thought I would just run the other one on a different port. But when I change it to a different port (e.g. 6603), but when I do, I get the below error:
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'mysql:6603' (111 Connection refused)
I have read every question on s.o. I can find that seems relevant but none of the solutions work. I feel certain the fix will involve changing a line or two of configuration but I've spent many hours on this so far so any help would be greatly appreciated.
The docker-compose script is below (works fine if 6603 is replaced with 3306).
server:
build:
context: ../
dockerfile: Docker/ServerDockerfile
ports:
- "8080:8080"
links:
- mysql:mysql
volumes:
- ../py:/app
tty: true
mysql:
image: mysql
expose:
- "6603"
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: project
volumes:
- ./MySQL:/docker-entrypoint-initdb.d
- ./MySQL/data:/var/lib/mysql
And it is being accessed like this:
cnx = mysql.connector.connect(user='root', password='password',
host='mysql',
port="6603",
database='project')
Try to specify another port for MySQL by modifying its my.cnf file.
Have eventually found a couple of ways that work. The neatest one is for each app to create a network and connect the containers to it.
If each app uses a different network then mysql can run on 3306 on that Docker network and can be accessed on mysql://3306 from app1 and mysql2://3306 from app2. (Assuming you name you give the mysql service for app 2 is mysql2).
The Docker file with the new lines is below:
server:
build:
context: ../
dockerfile: Docker/ServerDockerfile
ports:
- "8080:8080"
volumes:
- ../py:/app
tty: true
networks:
-net
mysql2:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: project
volumes:
- ./MySQL:/docker-entrypoint-initdb.d
- ./MySQL/data:/var/lib/mysql
networks:
-net
networks:
net:
driver: bridge
The Docker file for the second app is identical except the names are different (I put a 2 after each for simplicity).
server2:
build:
context: ../
dockerfile: Docker/ServerDockerfile
ports:
- "8081:8080"
volumes:
- ../py:/app
tty: true
networks:
-net2
mysql2:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: project
volumes:
- ./MySQL:/docker-entrypoint-initdb.d
- ./MySQL/data:/var/lib/mysql
networks:
-net2
networks:
net2:
driver: bridge

Docker Wordpress keeps redirecting to online version of site

I'm trying to set up docker wordpress as my dev environment on Ubuntu 17.10. I've made a copy of the db and placed it into the docker mysql service on port 8080 (using a wp plugin, which changes the home and site url)
I've arrived at the docker-compose.yml file below, but everytime I go to localhost:8000 or port 80 I get redirected to the original site online. I'm at a loss as to know what is wrong?
version: '3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: rootPword
MYSQL_DATABASE: xyz_wp
MYSQL_USER: xyz_2015
MYSQL_PASSWORD: userPword
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
# generally need to use non-default values
WORDPRESS_DB_HOST: db:3306
# next line often not in tutorials - https://stackoverflow.com/questions/46117771/issue-getting-docker-to-access-my-database-properly-with-wordpress
WORDPRESS_DB_NAME: xyz_wp
WORDPRESS_DB_USER: xyz_2015
WORDPRESS_DB_PASSWORD: userPword
WORDPRESS_TABLE_PREFIX: "af_"
working_dir: /var/www/html
volumes:
- /home/simon/code/wp_af2015/wp-content:/var/www/html/wp-content
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
environment:
MYSQL_USERNAME: root
MYSQL_ROOT_PASSWORD: rootPword
restart: always
ports:
- 8080:80
links:
- db
volumes:
db_data:
I think there is a little misunderstanding here.
when you add : after the domain it specifies the port.
From the Screenshot of PhpMyAdmin, it seems you set the Wordpress on port 8000. Because after :, 8000 has been set.
From your docker file, there is a mistake, because from what you written, it is wrong.
The ports should be or :80 or :8000, but not both.
If you want your docker to listen to localhost port 8000 (your WP),
then on your docker config, you should set:
ports:
- "8000"
Restart Docker, and it should work as expected.
A day later I tried to reach the dev site using an incognito window and everything worked! So the solve was to delete my browsing data from my normal browser. I do not understand what happened but the issue is fixed