docker-compose wordpress mysql connection refused - wordpress

I've created a small docker-compose.yml which used to work like a charm to deploy small WordPress instances. It looks like this:
wordpress:
image: wordpress:latest
links:
- mysql
ports:
- "1234:80"
environment:
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_PASSWORD: "password"
WORDPRESS_DB_HOST: mariadb
MYSQL_PORT_3306_TCP: 3306
volumes:
- /srv/wordpress/:/var/www/html/
mysql:
image: mariadb:latest
mem_limit: 256m
container_name: mariadb
environment:
MYSQL_ROOT_PASSWORD: "password"
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: "password"
volumes:
- /srv/mariadb:/var/lib/mysql
But when I start it now (maybe since docker update to Docker version 1.9.1, build a34a1d5), it fails
wordpress_1 | Warning: mysqli::mysqli(): (HY000/2002): Connection refused in - on line 10
wordpress_1 |
wordpress_1 | MySQL Connection Error: (2002) Connection refused
When I cat /etc/hosts of the wordpress_1 there are entries for MySQL:
172.17.0.10 mysql 12a564fdbc56 mariadb
and I am able to ping the MariaDB server.
When I docker-compose up, WordPress gets installed and after several restarts the MariaDB container prints:
Version: '10.0.22-MariaDB-1~jessie' socket: '/var/run/mysqld/mysqld.sock' port: 3306 mariadb.org binary distribution
Which schould indicate it to be running, isn't it?
How do I get the WordPress to be able to connect to the MariaDB container?

To fix this issue the first thing to do is:
Add the following code to wordpress & database containers (in the docker-compose file):
restart: unless-stopped
This will make sure you Database is started and intialized before wordpress container trying to connect to it. Then restart docker engine
sudo restart docker
or (for ubuntu 15+)
sudo service docker restart
Here the full configuration that worked for me, to setup wordpress with MariaDB:
version: '2'
services:
wordpress:
image: wordpress:latest
links:
- database:mariadb
environment:
- WORDPRESS_DB_USER=wordpress
- WORDPRESS_DB_NAME=mydbname
- WORDPRESS_TABLE_PREFIX=ab_
- WORDPRESS_DB_PASSWORD=password
- WORDPRESS_DB_HOST=mariadb
- MYSQL_PORT_3306_TCP=3306
restart: unless-stopped
ports:
- "test.dev:80:80"
working_dir: /var/www/html
volumes:
- ./wordpress/:/var/www/html/
database:
image: mariadb:latest
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=mydbname
- MYSQL_USER=wordpress
- MYSQL_PASSWORD=password
restart: unless-stopped
ports:
- "3306:3306"

The reason for this behaviour probably was related to a recent kernel and docker update. I recognized several other connection issues in other docker-compose setups. Therefore I restarted the server (not just the docker service) and didn't have had any issues like this ever since.

I had almost same problem, but just restarting the Wordpress container saved me:
$ docker restart wordpress
I hope this help many people.

I too had troubles here. I was using docker-compose to set up multiple wordpress websites on a single (micro) Virtual Private Server, including phpmyadmin and jwilder/nginx-proxy as a controller.
$ docker logs XXXX will help indicate areas of concern. In my case, the MariaDB databases would keep restarting all the time.
It turns out that all that stuff just wouldn't fit on a micro 512M Single CPU service. I never received error messages that told me directly that size was an issue, but after adding things up, I realized that when all the databases were starting up, I was running out of memory. An upgrade to 1Gb, 1 CPU service worked just fine.

I was using your docker-compose.yml, had the same problem. Just restarting didn't fix. After nearly an hour of researching the logs, I found the problem was: wordpress service started connecting mysql service before it had fully started. Simply adding depends_on won't help.Docker Compose wait for container X before starting Y
the work around could be start the db server before Up. When it has fully started, run docker-compose up. Or just use external service.

This simply means you are trying to connect to the wrong host. In order to use this in localhost just use the name of your service as the database host example in your case, it would be mysql you can fix this by specifying the name of the localhost with a default variable like this MYSQL_ROOT_HOST: localhost

In my case, I'm using Mysql (not MariaDb) but I had the same problem.
After upgrading the MySQL version, it's works fine.
You can see my open source docker-compose configuration: https://github.com/rimiti/wordpress-dockerized-environment

Related

Wordpress on Docker (Synology NAS) with different networks

first, sorry for my bad english, hope you can understand me.
i have the following task.
I want to run a (maybe more) wordpress installation on my synology nas. Therefore, i installed Docker and run portainer for creating some stuff.
My main idea is to create the following:
Create different container with separated wordpress installations
Create mysql container for hosting the different wordpress databases, each for every wordpress app
for the wordpress container there is a own network called "app_network" (bridge, attachable)
for the mysql container there is another network called "db_backend" (bridge, attachable)
So far, so god. At the moment i created one WP container, the mysql container and the two networks. Everything seems to be fine.
wordpress container is created with docker-compose (stack in portainer)
mysql container is created with docker-compose (stack in portainer)
I created a db for wordpress in the mysql container manually - local login on container works perfect.
the mysql container is in the network db_backend
the woordpress container is in the network app_network and additionally connected to the db_backend network (assigend ip´s looks correct)
But...if i am calling the wordpress page i got "Error establishing a database connection"
My yaml looks like this:
#mysql.yaml
version: '3.9'
services:
db:
image: mysql:latest
restart: on-failure:3
volumes:
- /volume1/docker/databases:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: mysuperstrongpassword
container_name: db_mysql
networks:
- db_backend
networks:
db_backend:
driver: bridge
external: true
#worppess.yaml
version: '3.9'
services:
#frontend
wp_app:
image: wordpress:latest
restart: on-failure:3
ports:
- '49200:80'
- '49201:443'
volumes:
- /volume1/docker/wp_app/wp_t:/var/www/html
environment:
WORDPRESS_DB_HOST: db_mysql:3306 //wrong entry? tried hostname, ip, service
WORDPRESS_DB_NAME: mydb
WORDPRESS_DB_USER: myuser
WORDPRESS_DB_PASSWORD: mypassword
networks:
- db_backend
- app_network
networks:
#172.168.29.1/24
db_backend:
driver: bridge
external: true
#172.168.30.1/24
app_network:
driver: bridge
external: true
After all i was able to read about docker, docker-networking and docker compose i thought my solution should work, all can be deployed without any errors, except the database connection error :( ...
Is the way of connection the container between the networks correct?
May i edit the wp-config.php with the information and add them to the wordpress containe?
Can anyone help?
Replace this WORDPRESS_DB_HOST: db_mysql:3306 with WORDPRESS_DB_HOST: db

Is it possible to have dockerized wordpress with same performance as a wordpress server install?

I am experimenting with a wordpress docker install.
I used the basic install with mounted volumes.
docker-compose.yml
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- ./mysql:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8001:80"
restart: always
volumes:
- ./html:/var/www/html
- ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
But after adding several plugins and a theme, wp-admin gets terribly slow. Approx 5-7 seconds TTFB. Using elementor becomes basically impossible.
Throwing hardware (it's an AWS EC2) at the server did NOT change the performance.
Is it possible to have wordpress in a performant docker setup?
First: You should probably use the mysql:latest tag. 5.7 is now an older database, latest is now 8.0.23 (community server).
Second: You should specify wordpress version, and php version and keep these updated along the way. I use image: wordpress:5.6-php7.4-apache which gives me php7 for better performance.
Once you make changes in your docker-compose.yml, run docker-compose up --build to make sure to get clean versions of everything.
Your docker-compose version could be upgraded from 3.3 to 3.8 (has nothing to do with performance, though).
Make sure to upgrade your Docker installation to the latest (19.03+ at the moment).
Compare your docker-compose to mine, which is running great with plugins:
version: "3.8"
services:
db:
image: mysql:latest
command: "--default-authentication-plugin=mysql_native_password"
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:5.6-php7.4-apache
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
WORDPRESS_CONFIG_EXTRA: |
define('WP_DEBUG', true);
error_reporting(E_ALL);
ini_set('display_errors', 1);
working_dir: /var/www/html
volumes:
- ./wp-content:/var/www/html/wp-content
- ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
volumes:
db_data: {}
Note that I use working_dir so that the directory for your docker container is set correctly. By adding wp-content to your volumes you copy wp-content into your container to persist it. Plugins are located in wp-content and this may improve your performance situation.
There is almost no cost of running docker. The biggest difference is on the networking layer, but it is reduced with host networking. The cost is not as much as you should think about it.
What is docker
In simplification docker is nothing more than process and resources isolation. All processes are running on the host machine without any virtualozation. There are linux modules responsible for isolation resources and processes. Example of modules:
Cgroup and memory resource controller in Kernel: https://www.kernel.org/doc/Documentation/cgroup-v1/memory.txt
Docker uses this to limit of CPU and memory usage for containers. More info here: https://docs.docker.com/config/containers/resource_constraints/
Linux namespaces - https://man7.org/linux/man-pages/man7/namespaces.7.html
This is another important feature of kernel used by Docker.
iptables - Iptables are commonly used to define networking layers in Docker. This is probably the biggest bottleneck for Docker.
IBM investigation in 2014
IBM did some investigation for that topic few years ago here: https://dominoweb.draco.res.ibm.com/reports/rc25482.pdf
You can find network latency for Docker NAT networking:
But let's see another graph, that show us latency for Redis. You can see that for network=host docker is almost as fast as native host.
Debugging
We cannot say what is wrong with your deployment, because picture is too big, and you provided only small part of the photo.
However you can start debugging by yourself.
Create another EC2 instance.
Install Prometheus on new Instance
On the WordPress instance install the node exporter. This will export metrics for Prometheus
Configure Prometheus to collect metrics from your Wordpress instance
Optionally install the Grafana on the Prometheus Server.
Wait a day to collect data and analyze where you are hitting the ceil.
To install Prometheus use Prometheus Get Start Docs
To install node_exporter and set the Prometheus Scraper up use this docs: https://prometheus.io/docs/guides/node-exporter/
Summary
So answer to your question is: It depends, how your application is deployed to docker. Probably few important things, that can affect your performance
CPU limit for container
Memory limit for container
Networking type
Missing capabilities
Number of application deployed on the same host
Other limits like max open files, virtual memory limit, number of processes inside container, etc...

Is restarting a website with "docker-compose up" after a host server reboot a standard practice?

I created a Wordpress website with the docker-compose.yml below.
I started it with docker-compose up -d (Note that the MySQL and Wordpress data are persisted in subdirectories of the local directory: ./mysql and ./wp).
Then let's say we reboot the host server.
How would you restart these containers/this website?
Would you just redo docker-compose up -d?
I tried, and it works indeed, but this actually recreates
new containers! Fortunately, as it reuses the same data directories and as it detects that /var/www/html is non-empty, a new Wordpress installation is not triggered, so "everything is ok", but still, isn't recreating new containers bad practice since the website already exists?
Is there a more "graceful" way to restart containers made with docker-compose after host server reboot, rather than "recreating" the containers?
version: '2'
services:
db:
image: mysql:5.7
volumes: ['./mysql:/var/lib/mysql']
restart: always
environment:
MYSQL_ROOT_PASSWORD: abcdef
MYSQL_DATABASE: wordpress
MYSQL_USER: wp
MYSQL_PASSWORD: abcabc
wordpress:
depends_on: ['db']
image: wordpress:latest
volumes: ['./wp:/var/www/html']
ports: ['8000:80']
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wp
WORDPRESS_DB_PASSWORD: abcabc
There are 2 ways to start containers which are created by docker-compose.
After rebooting the machine:
Run: docker ps -a to show all stopped-container, and you can start container manually, docker start [container_name or container_id]
Run: docker-compose start [service].
Starts existing containers for a service.
In your case:
docker-compose start db
docker-compose start wordpress
or just
docker-compose start
Starting db ... done
Starting wordpress ... done
Before rebooting, docker-compose stop.

phpmyadmin not connecting using docker---- Connection for controluser as defined in your configuration failed

I'm a newbie when it comes to Docker. For my setup I'm installing Docker for the purpose of running a MySQL WordPress on a MacOS environment.
Below is my docker-compose.yml file:
wordpress:
image: wordpress
links:
- wordpress_db:mysql
ports:
- 8080:80
wordpress_db:
image: mariadb
phpmyadmin:
image: corbinu/docker-phpmyadmin
links:
- wordpress_db:mysql
ports:
- 8181:80
mysql:
image: mysql
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=wordpress_db
- MYSQL_USER=sbroot
- MYSQL_PASSWORD=root
Is docker-compose up -d to terminal the correct approach? Also, are there any alternatives to using Docker to achieve the same result?
I don't know whether you add it or not, but you have to put the following lines on top of your docker-compose.yml file:
version: '3'
services:
What I can see in your file, two database images, 1) mysql and 2) mariaDb. You should keep only one of those for e.g. mysql for instance.
And what error do you get, when you visit link, http://localhost:8081 ?

Wordpress container just works default credentials

I've a question how exactly docker-compose handles environment variables.
services:
wp:
image: wordpress:latest
container_name: "wp"
restart: unless-stopped
links:
- wpdb
environment:
- TZ=Europe/Berlin
- WORDPRESS_DB_HOST=wpdb:3306
- WORDPRESS_DB_USER=wordpress
- WORDPRESS_DB_PASSWORD=password
- WORDPRESS_DB_NAME=wp
volumes:
- ./data:/var/www/html
labels:
- "traefik.enable=true"
- "traefik.backend=wp"
- "traefik.frontend.rule=Host:MASKED"
- "traefik.port=80"
- "traefik.docker.network=web"
networks:
- internal
- web
wpdb:
image: mariadb:latest
restart: unless-stopped
container_name: "wpdb"
environment:
- MYSQL_ROOT_PASSWORD=1234
- MYSQL_USER=wordpress
- MYSQL_PASSWORD=password
- MYSQL_DATABASE=wp
networks:
- internal
labels:
- "traefik.enable=false"
volumes:
- ./sql:/var/lib/mysql
volumes:
data:
sql:
networks:
web:
external: true
internal:
The compose file works great. The containers will be created and work perfectly.
But when I change the defaults at: WORDPRESS_DB_PASSWORD=password and MYSQL_PASSWORD=password.
The Wordpress container throws access denied for user. I also tried to kill the container and volumes.
Hopefully someone has a hint for me.
You should be doing a docker-compose down -v which would delete the named volumes declared in the volumes section. The only downside is that you would be losing all the data created by the service for the first time.
Here is how I could reproduce it -
Used your compose file as reference and on first time used the default password mentioned by you. The services come up fine, I install it and do a Ctrl+C to bring down the service. So all the MYSQL data is written into sql named volume.
When you do a Ctrl+C OR docker-compose down it only removes the containers and networks defined in the service. Not the volumes. Read more about it here
Now when you change password and bring the service back up it still uses the old volumes which has your old password.
So use a docker-compose down -v to remove the volumes too and give it a try.
Here are the steps how I reproduced it
Ctrl+C to stop all the services and then update the docker-compose.yml to update the password and do a docker-compose up again to get access denied error.
Do a docker-compose down -v to clean all the volume too and then do a docker-compose up
On doing a docker-compose down -v you will be losing all the data created by the prior service. Use it cautiously.

Resources